Accessing Linux File Systems
Accessing Linux 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
Show a report on the file systems on the host system with all units converted to human-readable
format:
[user@host ~]$ df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 892M 0 892M 0% /dev
tmpfs 915M 0 915M 0% /dev/shm
tmpfs 915M 17M 899M 2% /run
tmpfs 915M 0 915M 0% /sys/fs/cgroup
Show a disk usage report in human-readable format for the /usr/share directory on host:
Use the lsblk command to list the details of a specified block device or all the available
devices.
The following example mounts the file system in the /dev/vdb1 partition on the directory
/mnt/data.
To unmount a file system, the umount command expects the mount point as an argument.
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.
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.
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.
[root@host data]# cd
[root@host ~]# umount /mnt/data
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.
A new partition with a file system has been added to the second disk (/dev/vdb) on servera.
Mount the newly available partition by UUID at the newly created mount point
/mnt/newspace.
1. Use the su - command to switch to root, as the root user can only manually mount a
device.
[student@servera ~]$ su -
Use the lsblk command with the -fp option to discover the UUID of the device,
/dev/vdb1.
Mount the file system by using UUID on the /mnt/newspace directory. Replace the
UUID with that of the /dev/vdb1 disk from the previous command output.
1. Use the umount command to unmount /mnt/newspace while the current directory on
the shell is still /mnt/newspace. The umount command fails to unmount the device.
2. [root@servera newspace]# umount /mnt/newspace
umount: /mnt/newspace: target is busy.
The locate command searches a pregenerated index for file names or file paths and
returns the results instantly.
The find command searches for files in real time by crawling through the file-system
hierarchy.
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.
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.
The -i option performs a case-insensitive search. With this option, all possible combinations
of upper and lowercase letters match the search.
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:
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. 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:
In the following example, search for files starting in the / directory that end in .txt:
To search for files in the /etc/ directory that contain the word, pass, anywhere in their
names on host, run the following command:
o perform a case-insensitive search for a given file name, use the -iname option, followed by
the file name to search. To search files with case-insensitive text, messages, in their names in
the / directory on host, run the following command:
Search for files owned by the group user in the /home/user directory on host.
Search for files owned by user ID 1000 in the /home/user directory on host.
Search for files owned by group ID 1000 in the /home/user directory on host.
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.
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:
To match files for which the user has at least write and execute permissions, and the group
has at least write permissions, and others have at least read access:
To match files for which the user has read permissions, or the group has at least read
permissions, or others have at least write access:
The find command can look up files that match a size specified with the -size option,
followed by a numerical value and the unit. 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.
Store the search result of all files in the /usr/share directory that is greater than 50 MB and
less than 100 MB in the /mnt/freespace/search2.txt file.
To find all files that had their file content changed 120 minutes ago on host, run:
The + modifier in front of the amount of minutes looks for all files in the / that have been
modified more than n minutes ago. In this example, files that were modified more than
200 minutes ago are listed.
The - modifier changes the search to look for all files in the / directory which have been
changed less than n minutes ago. In this example, files that were modified less than
150 minutes ago are listed.
[root@host ~]# find / -mmin -150
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:
d, for directory
he -links option followed by a number looks for all files that have a certain hard link count.
The number can be preceded by a + modifier to look for files with a count higher than the
given hard link count. If the number is preceded with a - modifier, the search is limited to all
files with a hard link count that is less than the given number.
Search for all regular files with more than one hard link on host: