Access Liunx file system
Identify file systems and devices
Storage management concepts
Files on a Linux server are accessed according to the file system hierarchy. This file system hierarchy test is assembled from the file systems provided by the storage devices available to the system. Each file system is a formatted storage device that can be used to store files.
File systems and mount points
To make the contents of a file system available in the file system hierarchy, it must be mounted into an empty directory. After mounting, if you use ls to list the directory, you will see the contents of the mounted file system and can access and use these files normally.
File systems, storage and block devices
In Linux, low-level access to storage devices is provided by a special type of file called a block device. Before mounting these block devices, they must be formatted using the file system. Block device files are stored in /dev
the directory along with other device files.
disk partition
You don't usually set up an entire storage device as a file system. Storage devices are usually divided into smaller chunks called partitions.
Partitions are used to divide a hard drive: different parts can be formatted with different file systems or used for other purposes.
logical volume
Another way to organize disks and partitions is through Logical Volume Management (LVM). Through lvm, one or more block devices can be aggregated into an entire storage pool, called a volume group. A volume group's disk space is allocated to one or more logical volumes, functionally equivalent to partitions that reside on physical disks.
Check file system
Use the df command to view local and remote file system devices and available space.
[root@servera ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
devtmpfs 874504 0 874504 0% /dev
tmpfs 894592 0 894592 0% /dev/shm
tmpfs 894592 8900 885692 1% /run
tmpfs 894592 0 894592 0% /sys/fs/cgroup
/dev/mapper/rhel-root 17811456 1888092 15923364 11% /
/dev/nvme0n1p1 1038336 225396 812940 22% /boot
tmpfs 178916 0 178916 0% /run/user/0
For detailed information about the space used by a particular directory tree, use the du command. The du command has -h and -H options to convert the output into a readable form. The du command recursively displays the size of all files in the current directory tree.
du -h dirname
Mount and unmount file systems
Manually mount file system
File systems residing on available storage devices need to be mounted before they can be accessed. The mount command runs the root user to manually mount the entire file system.
mount 要挂载的文件系统 指定在文件系统层次结构中用于挂载点的目录
Identify block devices
Each connection to the storage device may be plugged into a different port.
lsblk lists details of a specified block device or all available devices.
[root@servera ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 19G 0 part
├─centos_servera-root 253:0 0 17G 0 lvm /
└─centos_servera-swap 253:1 0 2G 0 lvm [SWAP]
sdb 8:16 0 20G 0 disk
sr0 11:0 1 918M 0 rom
Mount by block device name
[root@servera ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 19G 0 part
├─centos_servera-root 253:0 0 17G 0 lvm /
└─centos_servera-swap 253:1 0 2G 0 lvm [SWAP]
sdb 8:16 0 20G 0 disk
└─sdb1 8:17 0 20G 0 part
sr0 11:0 1 918M 0 rom
To mount a file system, the target directory must exist
[root@servera ~]# mount /dev/sdb1 /mnt/data/
Unmount by file system UUID
A stable identifier UUID associated with the file system is part of the file system. As long as the system is not rebuilt, the UUID remains unchanged.
[root@servera ~]# lsblk -fp
NAME FSTYPE LABEL UUID MOUNTPOINT
/dev/sda
├─/dev/sda1 xfs f817436a-8545-4281-9824-d6e702ed8a87 /boot
└─/dev/sda2 LVM2_member Z3H2lv-Wy4E-dThI-BW4F-QGho-V1HW-d5GnNW
├─/dev/mapper/centos_servera-root
xfs b26176ca-72a1-4982-acb1-af8c367203ec /
└─/dev/mapper/centos_servera-swap
swap efad3a2a-eab4-489b-9570-2da62da5d1bc [SWAP]
/dev/sdb
└─/dev/sdb1 ext4 051cef52-7ecd-4438-adf7-3f4171e9fc71
/dev/sr0 iso9660 CentOS 7 x86_64 2018-11-25-21-21-31-00
Mount based on UUID
[root@servera ~]# mount UUID="051cef52-7ecd-4438-adf7-3f4171e9fc71" /mnt/data
[root@servera ~]# lsblk -fp | grep /dev/sdb1
└─/dev/sdb1 ext4 051cef52-7ecd-4438-adf7-3f4171e9fc71 /mnt/data
Unmount file system
The shutdown and reboot process automatically unmounts all file systems. Any file system data cached in memory is flushed to the storage device, ensuring that the file system does not suffer data corruption.
To unmount a file system, use the umount command with the mount point as argument.
[root@servera ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 19G 0 part
├─centos_servera-root 253:0 0 17G 0 lvm /
└─centos_servera-swap 253:1 0 2G 0 lvm [SWAP]
sdb 8:16 0 20G 0 disk
└─sdb1 8:17 0 20G 0 part /mnt/data
sr0 11:0 1 918M 0 rom
[root@servera ~]# umount /mnt/data
[root@servera ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 19G 0 part
├─centos_servera-root 253:0 0 17G 0 lvm /
└─centos_servera-swap 253:1 0 2G 0 lvm [SWAP]
sdb 8:16 0 20G 0 disk
└─sdb1 8:17 0 20G 0 part
sr0 11:0 1 918M 0 rom
Find files on your system
Search files
- locate searches for the file name or file path in all pre-generated files and returns the results
- find searches by crawling the entire file system hierarchy
Find files by name
locate finds files based on file name or path. This method is faster because it looks up this information from the mlocate database. However, the database is not updated in real time, but the root user can force updates via updatedb.
If you don’t have the locate command, please download it.
sudo yum install mlocate
[root@servera ~]# updatedb
[root@servera ~]# locate passwd
/etc/passwd
/etc/passwd-
/etc/pam.d/passwd
/etc/security/opasswd
/usr/bin/gpasswd
[root@servera ~]# locate image -
/etc/selinux/targeted/contexts/virtual_image_context
/usr/bin/grub2-mkimage
/usr/lib/firewalld/services/ovirt-imageio.xml
/usr/lib/firmware/nvidia/gm20b/pmu/image.bin
/usr/lib/firmware/nvidia/gp102/sec2/image.bin
The -i option consults for case-insensitive searches.
[root@servera ~]# locate -i messages
/usr/share/locale/aa/LC_MESSAGES
/usr/share/locale/ab/LC_MESSAGES
/usr/share/locale/ace/LC_MESSAGES
/usr/share/locale/ach/LC_MESSAGES
/usr/share/locale/ada/LC_MESSAGES
-n limit the number returned
[root@servera ~]# locate -n 5 message
/usr/lib/modules/3.10.0-957.el7.x86_64/kernel/drivers/message
/usr/lib/modules/3.10.0-957.el7.x86_64/kernel/drivers/message/fusion
/usr/lib/modules/3.10.0-957.el7.x86_64/kernel/drivers/message/fusion/mptbase.ko.xz
/usr/lib/modules/3.10.0-957.el7.x86_64/kernel/drivers/message/fusion/mptctl.ko.xz
/usr/lib/modules/3.10.0-957.el7.x86_64/kernel/drivers/message/fusion/mptsas.ko.xz
[root@servera ~]#
Search live files
The find command performs a real-time search in the file system hierarchy and is slower than locate, but more accurate. You can also search for files based on criteria other than file name, such as file permissions/type/size/modification time
find looks at the files on the file system using the user account that performed the search.
The first argument to find is the directory to search. If the directory argument is omitted, find will search starting in the current directory and look for a match in any subdirectories.
If you search for a file by file name, use -name filename and select find to return the path to the file that exactly matches filename.
[root@servera ~]# find / -name sshd_config
/etc/ssh/sshd_config
You can use wildcards to search for file names and return all results that match a partial match. If you use wildcards, be sure to enclose the file name you are looking for in parentheses to prevent the terminal from interpreting the wildcards.
[root@servera ~]# find / -name '*.txt' | head -n 3
/etc/pki/nssdb/pkcs11.txt
/var/cache/yum/x86_64/7/base/mirrorlist.txt
/var/cache/yum/x86_64/7/timedhosts.txt
[root@servera ~]#
[root@servera ~]# find /etc -name '*pass*'
/etc/pam.d/passwd
/etc/pam.d/password-auth-ac
/etc/pam.d/password-auth
/etc/passwd-
/etc/passwd
/etc/security/opasswd
/etc/openldap/certs/password
/etc/selinux/targeted/active/modules/100/passenger
To perform a case-insensitive search on a given filename, use the -iname option
[root@servera ~]# find / -iname '*messages*' | head -n 3
/var/log/messages
/usr/share/locale/ast/LC_MESSAGES
/usr/share/locale/ca/LC_MESSAGES
Search based on ownership or permissions
parameter:
- -user
- -group
- uid
- gid
- perm
[root@servera ~]# find -user root | head -n 3
.
./.bash_logout
./.bash_profile
[root@servera ~]# find -group root | head -n 3
.
./.bash_logout
./.bash_profile
[root@servera ~]# find -uid 0
.
./.bash_logout
./.bash_profile
./.bashrc
./.cshrc
./.tcshrc
./anaconda-ks.cfg
Search files based on size
- -k kilobytes
- -M megabytes
- -G gigabytes
find -size 10M 大小为10M的文件
fing -size +10G 大小超过10G的文件
find -size -10k 大小不到10k的文件
Search based on modification time
-mmin plus minutes or more will search for all files that were changed n minutes ago
find / -mmin 120 #120分钟以前修改的所有文件
find / -mmin +200 #200分钟以前修改的所有文件
find / -mmin -150 #150分钟内修改的所有文件
Search by file type
-type:
- fNormal file
- d directory
- lSoft chain
- b block device
#/etc目录的所有文件
[root@servera ~]# find /etc -type d | head -n 3
/etc
/etc/grub.d
/etc/terminfo
#/etc目录下所有软链
[root@servera ~]# find / -type l | head -n 3
/dev/cdrom
/dev/snd/by-path/pci-0000:02:02.0
/dev/initctl
#/dev下的块设备
[root@servera ~]# find /dev -type b
/dev/sdb1
/dev/dm-1
/dev/dm-0
/dev/sr0
/dev/sdb
/dev/sda2
/dev/sda1
/dev/sda
#软链大于3的普通文件
find / -type f -links +1