LINUX FILE SYSTEM
Published: 2022-09-25 13:08:06

## **_Examining File Systems_**

To get an overview of local and remote file system devices and the amount of free space available, run the **_df_** command. When the **_df_** command is run without arguments, it reports total disk space, used disk space, free disk space, and the percentage of the total disk space used on all mounted regular file systems. It reports on both local and remote file systems.

The following example displays the file systems and mount points on host.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cmzfq5j2rnlvoimin2hh.png)

The partitioning on the host system shows two physical file systems, which are mounted on / and /boot. This is common for virtual machines. The tmpfs and devtmpfs devices are file systems in system memory. All files written into tmpfs or devtmpfs disappear after system reboot.

To improve readability of the output sizes, there are two different human-readable options: **_-h_** or **_-H_**. The difference between these two options is that -h reports in KiB (210), MiB (220), or GiB (230), while the **_-H_** option reports in SI units: KB (103), MB (106), or GB (109). Hard drive manufacturers usually use SI units when advertising their products.

For more detailed information about space used by a certain directory tree, use the **_du_** command. The **_du_** command has _**-h**_ and **_-H_** options to convert the output to human-readable format. The du command shows the size of all files in the current directory tree recursively.

Show a disk usage report for the _/usr/share_ directory on _host_:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jfwga1jj6e8kqonbom10.png)

## **_Identifying the Block Device_**

A hot-pluggable storage device, whether a hard disk drive (HDD) or solid-state device (SSD) in a server caddy, or a USB storage device, might be plugged into a different port each time they are attached to a system.

Use the **_lsblk_** command to list the details of a specified block device or all the available devices.

The **_lsblk -fp_** command lists the full path of the device, along with the UUIDs and mount points, as well as the type of file system in the partition. If the file system is not mounted, the mount point will be blank.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eickj54ehg09tx8nder3.png)

## Unmounting File Systems

The shutdown and reboot procedures unmount all file systems automatically. As part of this process, any file system data cached in memory is flushed to the storage device thus ensuring that the file system suffers no data corruption.

```
#umount /mnt/data
```

Unmounting is not possible if the mounted file system is in use. For the **_umount_** command to succeed, all processes needs to stop accessing data under the mount point.

In the example below, the **_umount_** fails because the file system is in use (the shell is using /mnt/data as its current working directory), generating an error message.

```
[root@host ~]# cd /mnt/data
[root@host data]# umount /mnt/data
umount: /mnt/data: target is busy.
```

The **_lsof_** command lists all open files and the process accessing them in the provided directory. It is useful to identify which processes currently prevent the file system from successful unmounting.

```
[root@host data]# lsof /mnt/data
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
bash    1593 root  cwd    DIR 253,17        6  128 /mnt/data
lsof    2532 root  cwd    DIR 253,17       19  128 /mnt/data
lsof    2533 root  cwd    DIR 253,17       19  128 /mnt/data
```
**_Locating Files by Name_**

The locate command finds files based on the name or path to the file. It is fast because it looks up this information from the mlocate database. However, this database is not updated in real time, and it must be frequently updated for results to be accurate. This also means that locate will not find files that have been created since the last update of the database.

The locate database is automatically updated every day. However, at any time the root user can issue the **_updatedb_** command to force an immediate update.

```
# updatedb
```

The locate command restricts results for unprivileged users. In order to see the resulting file name, the user must have search permission on the directory in which the file resides.

Search for files with passwd in the name or path in directory trees readable by user on host

```
$ locate passwd
```

The **_-i_** option performs a case-insensitive search. With this option, all possible combinations of upper and lowercase letters match the search.

```
$ locate -i messages
```

The **_-n_** option limits the number of returned search results by the locate command. The following example limits the search results returned by locate to the first five matches:

```
$ locate -n 5 snow.png
```

## Searching for Files in Real Time

The **_find_** command locates files by performing a real-time search in the file-system hierarchy. It is slower than locate, but more accurate. It can also search for files based on criteria other than the file name, such as the permissions of the file, type of file, its size, or its modification time.

The **_find_** command looks at files in the file system using the user account that executed the search. The user invoking the find command must have read and execute permission on a directory to examine its contents.

The first argument to the **_find_** command is the directory to search. If the directory argument is omitted, find starts the search in the current directory and looks for matches in any subdirectory.

To search for files by file name, use the _-name_ FILENAME option. With this option, find returns the path to files matching FILENAME exactly. For example, to search for files named sshd_config starting from the / directory, run the following command:

```
# find / -name sshd_config
/etc/ssh/sshd_config
```

## THANKYOU