Linux File System
Tags: linux, cloud
Published: 2022-09-26 11:19:08

- Examining File Systems
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.
The following example displays the file systems and mount points on host.
```
[user@host ~]$ df
Filesystem     1K-blocks    Used Available Use% Mounted on
devtmpfs          912584       0    912584   0% /dev
tmpfs             936516       0    936516   0% /dev/shm
tmpfs             936516   16812    919704   2% /run
tmpfs             936516       0    936516   0% /sys/fs/cgroup
/dev/vda3        8377344 1411332   6966012  17% /
/dev/vda1        1038336  169896    868440  17% /boot
tmpfs             187300       0    187300   0% /run/user/1000
```
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:

```
[root@host ~]# du /usr/share
...output omitted...
176 /usr/share/smartmontools
184 /usr/share/nano
8 /usr/share/cmake/bash-completion
8 /usr/share/cmake
356676  /usr/share
```

- Identifying the Block Device
Use the lsblk command to list the details of a specified block device or all the available devices.

```
[root@host ~]# lsblk
NAME                      MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
vda                       253:0    0   12G  0 disk
├─vda1                    253:1    0    1G  0 part /boot
├─vda2                    253:2    0    1G  0 part [SWAP]
└─vda3                    253:3    0   11G  0 part /
vdb                       253:16   0   64G  0 disk
└─vdb1                    253:17   0   64G  0 part
```
- Mounting by Block Device Name
The following example mounts the file system in the /dev/vdb1 partition on the directory /mnt/data.

```
[root@host ~]# mount /dev/vdb1 /mnt/data
```
- Mounting by File-system UUID
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.

```
[root@host ~]# lsblk -fp
NAME        FSTYPE LABEL UUID                                 MOUNTPOINT
/dev/vda                                                      
├─/dev/vda1 xfs          23ea8803-a396-494a-8e95-1538a53b821c /boot
├─/dev/vda2 swap         cdf61ded-534c-4bd6-b458-cab18b1a72ea [SWAP]
└─/dev/vda3 xfs          44330f15-2f9d-4745-ae2e-20844f22762d /
/dev/vdb
└─/dev/vdb1 xfs          46f543fd-78c9-4526-a857-244811be2d88
```
- Unmounting File Systems
To unmount a file system, the umount command expects the mount point as an argument.

```
[root@host ~]# umount /mnt/data
```
- Searching for Files
This section discusses two commands that can search for files in the file-system hierarchy.

1. The locate command searches a pregenerated index for file names or file paths and returns the results instantly.

1. The find command searches for files in real time by crawling through the file-system hierarchy.

Locating Files by Name
The locate command finds files based on the name or path to the file.
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.

```
[root@host ~]# updatedb
```
The locate command restricts results for unprivileged users.
Search for files with passwd in the name or path in directory trees readable by user on host.

```
[user@host ~]$ locate passwd
/etc/passwd
/etc/passwd-
/etc/pam.d/passwd
/etc/security/opasswd
/usr/bin/gpasswd
/usr/bin/grub2-mkpasswd-pbkdf2
/usr/bin/lppasswd
/usr/bin/passwd
...output omitted...
```
Searching for Files in Real Time
The first argument to the find command is the directory to search.
For example, to search for files named sshd_config starting from the / directory, run the following command:

```
[root@host ~]# find / -name sshd_config
/etc/ssh/sshd_config
```

- Searching Files Based on Ownership or Permission
Search for files owned by user in the /home/user directory on host.

```
[user@host ~]$ find -user user
.
./.bash_logout
./.bash_profile
./.bashrc
./.bash_history
```

- Searching Files Based on Size
The example below shows how to search for files with a size of 10 megabytes, rounded up.

```
[user@host ~]$ find -size 10M
```

- Searching Files Based on Modification Time
To find all files that had their file content changed 120 minutes ago on host, run:

```
[root@host ~]# find / -mmin 120
```

- Searching Files Based on File Type
Use the following list to pass the required flags to limit the scope of search:

- f, for regular file

- d, for directory

- l, for soft link

- b, for block device
Search for all directories in the /etc directory on host.

```
[root@host ~]# find /etc -type d
/etc
/etc/tmpfiles.d
/etc/systemd
/etc/systemd/system
/etc/systemd/system/getty.target.wants
...output omitted...
```
Generate a list of all block devices in the /dev directory on host:

```
[root@host ~]# find /dev -type b
/dev/vda1
/dev/vda
```