Accessing Linux File Systems on RHEL 8 (RedHat Enterprise 8)

mohammadalwan

Mohammad-Alwan

Posted on September 26, 2022

Accessing Linux File Systems on RHEL 8 (RedHat Enterprise 8)

Disk Partitions

Normally, you do not make the entire storage device into one file system. Storage devices are typically divided up into smaller chunks called partitions.
Partitions are block devices in their own right. 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. Paravirtualized storage devices have a similar naming system.Partitions allow you to compartmentalize a disk: the various partitions can be formatted with different file systems or used for different purposes.
Blok Device Naming
Image description

Tools for Managing Partitions on Linux

There are several command line interface (CLI) based tools that are commonly used to view drive and partition information and to manage partitions.

To get an overview of local and remote file system devices and the amount of free space available, run the df(disk free) command.

$ df
Enter fullscreen mode Exit fullscreen mode

Example:Image description For more detailed information about space used by a certain directory tree, use the du(disk usage) command.

# du /directory-name/directory-name
Enter fullscreen mode Exit fullscreen mode

Example :Image description The du command has -h and -H options to convert the output to human-readable format.
Example :Image description

Mounting and Unmounting File Systems

Mounting File Systems Manually
before that, use the lsblk command to list the specific block device details or all available devices.Image description

Mounting by Block Device Name
The following example mounts the file system in the /dev/sdb1 partition on the directory /mnt/data.

# mount /dev/sdb1 /mnt/data
Enter fullscreen mode Exit fullscreen mode

To mount a file system, the destination directory must already exist.

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.
Image description
Mount the file system by the UUID of the file system.

# mount UUID="959f776a-84bd-4cfb-a9c9-e13623930202" /mnt/data
Enter fullscreen mode Exit fullscreen mode

Unmounting File Systems
To unmount a file system, the umount command expects the mount point as an argument.

# umount /mnt/data
Enter fullscreen mode Exit fullscreen mode

For the umount command to succeed, all processes needs to stop accessing data under the mount point.
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.

# lsof /mnt/data
Enter fullscreen mode Exit fullscreen mode

Once the processes are identified, an action can be taken, such as waiting for the process to complete or sending a SIGTERM or SIGKILL signal to the process. In this case, it is sufficient to change the current working directory to a directory outside the mount point.

Locating Files on the System

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
Enter fullscreen mode Exit fullscreen mode

Example of searching for a file with the name passwd.
Image description
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
Enter fullscreen mode Exit fullscreen mode

Output :
Image description

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.
To search for files by file name, use the -name FILENAME option. For example, to search for files named sshd_config starting from the / directory, run the following command:
Image description

In the following example, search for files starting in the / directory that end in .txt:
Image description
To search for files in the /etc/ directory that contain the word, pass, anywhere in their names on host, run the following command:
Image description
To perform a case-insensitive search for a given file name, use the -iname option, followed by the file name to search.

#find / -iname '*messages*'
Enter fullscreen mode Exit fullscreen mode

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

$ find -user user
Enter fullscreen mode Exit fullscreen mode

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

$ find -group user
Enter fullscreen mode Exit fullscreen mode

Search for files owned by user ID 1000 in the /home/user directory on host.

$ find -uid 1000
Enter fullscreen mode Exit fullscreen mode

Search for files owned by group ID 1000 in the /home/user directory on host.

$ find -gid 1000
Enter fullscreen mode Exit fullscreen mode

The -user, and -group options can be used together to search files where file owner and group owner are different.
Example :

# find / -user root -group mail
Enter fullscreen mode Exit fullscreen mode

The -perm option is used to look for files with a particular set of permissions. Permissions can be described as octal values, with some combination of 4, 2, and 1 for read, write, and execute. Permissions can be preceded by a / or - sign.

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

To search the files with a size more than 10 gigabytes.

$ find -size +10G
Enter fullscreen mode Exit fullscreen mode

Searching Files Based on Modification Time
The -mmin option, followed by the time in minutes, searches for all files that had their content changed at n minutes ago in the past.It also supports fractional values when used with ranges (+n and -n).

To find all files that had their file content changed 120 minutes ago on host, run:

# find / -mmin 120
Enter fullscreen mode Exit fullscreen mode

In this example, files that were modified more than 200 minutes ago are listed.

# find / -mmin +200
Enter fullscreen mode Exit fullscreen mode

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 Example ; Search for all directories in the /etc directory on host.
# find /etc -type d
Enter fullscreen mode Exit fullscreen mode

Search for all soft links on host.

# find / -type l
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
mohammadalwan
Mohammad-Alwan

Posted on September 26, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related