0% found this document useful (0 votes)
163 views8 pages

Accessing Linux File Systems

This document provides instructions for summarizing Linux file systems and locating files on the system. It describes how to use the df command to display file systems and disk usage, mount and unmount file systems, and use lsblk to view block devices. It also explains how to use the locate and find commands to search for files by name, permissions, ownership and other criteria. Key commands covered include df, du, mount, umount, lsblk, locate, find, and lsof.

Uploaded by

pmmanick
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
163 views8 pages

Accessing Linux File Systems

This document provides instructions for summarizing Linux file systems and locating files on the system. It describes how to use the df command to display file systems and disk usage, mount and unmount file systems, and use lsblk to view block devices. It also explains how to use the locate and find commands to search for files by name, permissions, ownership and other criteria. Key commands covered include df, du, mount, umount, lsblk, locate, find, and lsof.

Uploaded by

pmmanick
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

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 for the /usr/share directory on host:

[root@host ~]# du /usr/share

Show a disk usage report in human-readable format for the /usr/share directory on host:

[root@host ~]# du -h /var/log

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

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


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.

[root@host ~]# lsblk -fp

Mount the file system by the UUID of the file system.

[root@host ~]# mount UUID="46f543fd-78c9-4526-a857-244811be2d88" /mnt/data

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

[root@host ~]# 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.

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

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 -

 Create the /mnt/newspace directory.

[root@servera ~]# mkdir /mnt/newspace

 Use the lsblk command with the -fp option to discover the UUID of the device,
/dev/vdb1.

[root@servera ~]# lsblk -fp /dev/vdb


NAME FSTYPE LABEL UUID MOUNTPOINT
/dev/vdb
└─/dev/vdb1 xfs a04c511a-b805-4ec2-981f-42d190fc9a65

 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.

[root@servera ~]# mount UUID="a04c511a-b805-4ec2-981f-42d190fc9a65"


/mnt/newspace

 Verify that the /dev/vdb1 device is mounted on the /mnt/newspace directory.

[root@servera ~]# lsblk -fp /dev/vdb


NAME FSTYPE LABEL UUID MOUNTPOINT
/dev/vdb
└─/dev/vdb1 xfs a04c511a-b805-4ec2-981f-42d190fc9a65 /mnt/newspace

 Change to the /mnt/newspace directory and create a new directory,


/mnt/newspace/newdir, with an empty file, /mnt/newspace/newdir/newfile.

1. Change to the /mnt/newspace directory.

[root@servera ~]# cd /mnt/newspace

2. Create a new directory, /mnt/newspace/newdir.

[root@servera newspace]# mkdir newdir

3. Create a new empty file, /mnt/newspace/newdir/newfile.

[root@servera newspace]# touch newdir/newfile


 Unmount the file system mounted on the /mnt/newspace directory.

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.

3. Change the current directory on the shell to /root.


4. [root@servera newspace]# cd
[root@servera ~]#

5. Now, successfully unmount /mnt/newspace.

[root@servera ~]# umount /mnt/newspace

Locating Files on the System

 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.

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.

[root@host ~]# 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.

[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

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

[user@host ~]$ 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:

[user@host ~]$ 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.

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:

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

In the following example, search for files starting in the / directory that end in .txt:

[root@host ~]# find / -name '*.txt'

To search for files in the /etc/ directory that contain the word, pass, anywhere in their
names on host, run the following command:

[root@host ~]# find /etc -name '*pass*

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:

[root@host ~]# find / -iname '*messages*'

Searching Files Based on Ownership or Permission


The find command can search for files based on their ownership or permissions. Useful
options when searching by owner are -user and -group, which search by name, and -uid
and -gid, which search by ID.

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

[user@host ~]$ find -user user

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

[user@host ~]$ find -group user

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

[user@host ~]$ find -uid 1000

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

[user@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.

[root@host ~]# find / -user root -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:

[root@host ~]# find /home -perm 764

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:

[root@host ~]# find /home -perm -324

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:

[root@host ~]# find /home -perm /442


Searching Files Based on Size

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.

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

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

[user@host ~]$ find -size +10G

To list all files with a size less than 10 kilobytes.

[user@host ~]$ find -size -10k

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.

[root@serverb ~]# find /usr/share -size +50M -size 100M > \


/mnt/freespace/search2.txt

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

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.

[root@host ~]# find / -mmin +200

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

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.

[root@host ~]# find /etc -type d

Search for all soft links on host.

[root@host ~]# find / -type l

Generate a list of all block devices in the /dev directory on host:

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

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:

[root@host ~]# find / -type f -links +1

You might also like