Linux File System
Published: 2022-09-26 15:25:41


A Linux file system is a structured collection of files on a disk drive or a partition. Linux file system is generally a built-in layer of a Linux operating system used to handle the data management of the storage.

#Mounting and Unmounting file system 

Mounting a filesystem simply means making the particular filesystem accessible at a certain point in the Linux directory tree. When mounting a filesystem it does not matter if the filesystem is a hard disk partition, CD ROM, floppy, or USB storage device

Before   access the files on a file system, you need to mount the file system. Mounting a file system attaches that file system to a directory (mount point) and makes it available to the system. The root (/) file system is always mounted. Any other file system can be connected or disconnected from the root (/) file system.

```
student@servera# mount /dev/vdb1 /mnt/data
```
When you mount a file system, any files or directories in the underlying mount point directory are unavailable as long as the file system is mounted. These files are not permanently affected by the mounting process, and they become available again when the file syste yum is unmounted. However, mount directories are typically empty, because usually do not want to obscure existing files.

A common reason for file systems to fail to unmount is that a Bash shell is using the mount point or a subdirectory as a current working directory. Use the cd command to change out of the file system to resolve this problem.

```
[root@servera]# cd
```
```
[root@server ~]# umount /mnt/data
```
o list the details of a specific block device or all available devices use the *lsblk* command.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.
*List format
The Isblk command will display the output in a tree-like format. However, you can change this by using the-l argument as:
```
[root@servera]# lsblk -l
```
Information about specific devices To list information about a particular block device, specify the device name in the lsblk command as:
```
[root@servera]# lsblk /dev/sda1
```
#Disk partition 
A partition is a storage device that is divided into small pieces 
For example, one partition can contain user home directories while another can contain system data and logs. If a user fills up the home directory partition with data, the system partition may still have space available.
On SATA-attached storage, the first partition on the first disk is /dev/sda1. The third partition on the second disk is /dev/sdb3, and so on whereas 
An NVMe-attached SSD device names its partitions differentlythe first partition on the first disk is /dev/nvme0p1. The third partition on the second disk is /dev/nvme1p3, and so on.

*To searches a pregenerated index for file names or file paths and returns the results instantly use *locate* command 
updatedb creates and updates the database of file names used by locate .
 updatedb generates a list of files similar to the output of find and then uses utilities for optimizing the database for performance. updatedb is often run periodically as a cron job and configured with environment variables or command options.
```
[root@servera ~]#updatedb
```
*Search for files with passwd 
```
[student@servera ~]$ locate passwd
```
The *find* command searches for files in real time by crawling through the file-system hierarcy
#Searching for Files in Real Time
To perform a case-insensitive search for a given file name, use the -iname option, followed by the file name to search

```
[root@servera ~]# find / -iname '*messages*'
```
#Searching Files Based on Ownership or Permission

*Search for files owned by user in the /home/user directory on host

```
$ find -user user 
```
*Search for files owned by the group user  in the /home/user  directory on host.
```
$ find -group user
```
*Search for files owned by user ID 1000 in the /home/user directory on host.
```
$ find -uid 1000
```
*Search for files owned by group ID 1000 in the /home/user directory on host.
```
$ find -gid 1000
```
The -user, and -group options can be used together to search files where file owner and group owner are different. The example below list files that are both owned by user root and affiliated with group mail.

```
# find / -user root -group mail 
```
To match any file in the /home/user directory for which others have at least read access on host, run:
```
$ find -perm -004
```
To use a more complex example, the following command matches any file for which the user has read, write, and execute permissions, members of the group have read and write permissions, and others have read-only access:

```
# find /home -perm 764
```
#Searching Files Based on Size
 Use the following list as the units with the -size option:
   k, for kilobyte
   M, for megabyte
   G, for gigabyte
*The example below shows how to search for files with a size of 10 megabytes, rounded up
```
$ find -size 10M
```
*To search the files with a size more than 10 gigabytes.

```
$ find -size +10G
```
To list all files with a size less than 10 kilobytes.
```
$ find -size -10k
```
#Searching Files Based on Modification Time
To find all files that had their file content changed 120 minutes ago on host, run:

```
# find / -mmin 120
```
*Searching Files Based on File Type
The -type option in the find command limits the search scope to a given 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.

```
# find /etc -type d
```
*Search for all soft links on host.
```
# find / -type l
```
*Generate a list of all block devices in the /dev directory on host:

```
# find /dev -type b
```