# Filesystem and storage device management

## Device directory `/dev`

If you list all the things out it will show you all the devices on the computer

You may as well see **sda1, sda2, sda3, sdb and sdb1**, which are hard drive and its partitions and a USB flash drive and its partitions

```bash
$ cd /dev
$ ls -l
drwxr-xr-x 2 root root       40 Jun 18 15:38 block
drwxr-xr-x 2 root root      120 Jun 18 15:38 bsg
crw------- 1 root root   5,   1 Jun 18 15:38 console
crw------- 1 root root  10, 123 Jun 18 15:38 cpu_dma_latency
crw-rw-rw- 1 root root  10, 125 Jun 18 15:38 dxg
lrwxrwxrwx 1 root root       13 Jun 18 15:38 fd -> /proc/self/fd
crw-rw-rw- 1 root root   1,   7 Jun 18 15:38 full
crw-rw-rw- 1 root root  10, 229 Jun 18 15:38 fuse
crw------- 1 root root  10, 228 Jun 18 15:38 hpet
crw------- 1 root root 229,   0 Jun 18 15:38 hvc0
crw------- 1 root root 229,   1 Jun 18 15:38 hvc1
crw--w---- 1 root tty  229,   2 Jun 18 15:38 hvc2
brw------- 1 root root   7,   0 Jun 18 15:38 loop0
crw------- 1 root root  10, 237 Jun 18 15:38 loop-control
```

### Naming system

Note that on linux it’s sorted alphabetically

| Device File | Description         |
| ----------- | ------------------- |
| sda         | 1st SATA hard drive |
| sdb         | 2nd SATA hard drive |
| sdc         | 3rd SATA hard drive |
| sdd         | 4th SATA hard drive |

For partition-wise it will add a number after the device file, let’s say you have some partitions on sda so the first partition would be sda1 and so on

## Viewing spaces

To view the disk spaces and how much it left we use `fdisk`

This will list all the partitions of all devices

```bash
$ fdisk -l

```

## Character and Block Devices

Noticed that when out list out the `/dev` directory you will see new file types `c` and `b`

```bash
brw------- 1 root root   7,   0 Jun 18 15:38 loop0
crw------- 1 root root  10, 237 Jun 18 15:38 loop-control
```

### Character devices

This kind of devices are external devices that interact with the system by sending and receiving data character by character

Devices like **Keyboards, Mice, Sound Cards, Serial Ports and etc are character devices.**

### Block devices

Unlike character devices they communicate in blocks of data means sending multiple bytes at the time

Devices like **hard drives(HDD and SSD), DVD drive and USB flash drives**

Here’s how to list them using `lsblk`

```bash
$ lsblk
NAME MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
sda    8:0    0 388.4M  1 disk
sdb    8:16   0   186M  1 disk
sdc    8:32   0     2G  0 disk [SWAP]
sdd    8:48   0     1T  0 disk /mnt/wslg/distro
```

## Mounting and unmounting

The two main **mount point** are `/mnt` and `/media`

When automatically mounted it will be on `/media`

### Mounting devices manually

To mount device we use `mount` command and here’s the syntax

```bash
$ mount <device_to_mount> <directory_to_mount_it_on>
$ mount /dev/sdb1 /mnt
```

### Unmounting devices

Use `umount` to unmount the device think of it like **eject** it’s the same thing

```bash
$ umount <mounted_device>
$ umount /dev/sdb1
```

## Monitoring filesystem

To monitor filesystems we solely use `df` command

```bash
$ df
rootfs            3999444      2664   3996780   1% /
C:\             487684904 297407428 190277476  61% /mnt/c #I’m using wsl sry about it
D:\             976741372 472423220 504318152  49% /mnt/d
E:\             976755708 588669860 388085848  61% /mnt/e
```

## USB devices

We will use `lsusb` and it will list ALL USB buses and devices connected to them

Flags `-v` and `-t` are common uses for it

```bash
$ lsusb -t
/:  Bus 04.Port 1: Dev 1, Class=root_hub, Driver=xhci_hcd/4p, 5000M
/:  Bus 03.Port 1: Dev 1, Class=root_hub, Driver=xhci_hcd/12p, 480M
/:  Bus 02.Port 1: Dev 1, Class=root_hub, Driver=xhci_hcd/4p, 10000M
/:  Bus 01.Port 1: Dev 1, Class=root_hub, Driver=xhci_hcd/1p, 480M
    |__ Port 1: Dev 2, If 0, Class=Hub, Driver=hub/4p, 480M
        |__ Port 3: Dev 4, If 0, Class=Human Interface Device, Driver=usbhid, 12M
        |__ Port 3: Dev 4, If 1, Class=Human Interface Device, Driver=usbhid, 12M
        |__ Port 4: Dev 5, If 0, Class=Wireless, Driver=btusb, 12M
```

### Error handling

To check for errors we simply use `fsck` command to check and fix the filesystem error and you need to first **unmount** the device and specify the device or else get a error message

```bash
$ fsck
~~
/dev/sda1 is mounted
e2fsck: Cannot continue, aborting.
```

```bash
$ umount /dev/sdb1
$ fsck -p /dev/sdb1 #the -p flag is to automatically fix issues on that device
~~
Checking file system on /dev/sdb1.
File system version 1.0
Sector size 512 bytes
Cluster size 32 KB
Volume size 7648 MB
Used space 1265 MB
Available space 6383 MB
Totally 20 directories and 111 files.
File system checking finished. No errors found.
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://vix-w1zzer.gitbook.io/vixwizzer/notes/linux/filesystem-and-storage-device-management.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
