目录

Linux-文件系统基本管理

Linux 文件系统基本管理

Linux 文件系统基本管理

识别文件系统和设备

Linux 中设备

在Linux中,对存储设备的访问由特殊类型文件块设备(block)提供。挂载块设备前,必须先使用文件系统对其进行格式化

块设备文件与其他的设备文件一起存储在 /dev 目录中。设备文件是由操作系统自动创建的

常见的不同类型接口块设备:

  • 接口:SATA/SAS/USB/SCSI,名称:/dev/sda、/dev/sdb …
  • 接口:virtio-blk,虚拟机磁盘,名称:/dev/vda、/dev/vdb …
  • 接口:NVMe SSD,名称:/dev/nvme0n1、/dev/nvme1n1…
  • 接口:SD/MMC/eMMC,名称:/dev/mmcblk0,/dev/mmcblk1 …

通常,不会将整个存储设备格式化为一个文件系统中,而是将硬盘驱动器划分为多个逻辑存储单元, 这些单元称为分区。各种分区使用不同的文件系统进行格式化或用于不同的用途。 例如,一个分区可以包含用户目录,而另一个分区可以包含系统数据和日志。 如果用户用数据填充主目录分区,则系统分区可能仍然有可用空间

  • /dev/sda 第一个分区为 /dev/sda1,第二个分区为 /dev/sda2,以此类推
  • /dev/nvme0n1p1,/dev/nvme0n1p2
  • /dev/vda1,/dev/vda2
  • /dev/xvda1,/dev/xvda2

Linux 文件系统

文件系统是操作系统用于明确存储设备或分区上的文件的方法和数据结构,即在存储设备上组织文件的方法

Linux 服务器上的文件是按文件系统层次结构访问的
https://i-blog.csdnimg.cn/direct/d8e70aeb6f534775aaa9ddc1262863ad.png#pic_center

管理文件系统需要:

  • 确定存储设备的空间使用情况以及文件系统层次结构中受影响的目录
  • 存储设备发生故障,而您需要知道哪些文件系统存在风险

查看设备和文件系统

lsblk 命令

查看块设备使用情况

[root@centos7 ~ 06:45:18]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0  100G  0 disk 
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0   99G  0 part 
  ├─centos-root 253:0    0   50G  0 lvm  /
  ├─centos-swap 253:1    0    2G  0 lvm  [SWAP]
  └─centos-home 253:2    0   47G  0 lvm  /home
sdb               8:16   0   20G  0 disk 
sr0              11:0    1  4.4G  0 rom 

结果说明:

  • NAME:块设备名
  • MAJ:MIN:主要(MAJ)和次要(MIN)设备号
  • RM:指明设备是否是可移动设备。注意,在本例中设备 sdb 和 sr0 的 RM 值等于1,这说明他们是可移动设备
  • SIZE:本栏列出设备的容量大小信息。例如298.1G表明该设备大小为298.1GB,而1K表明该设备大小为1KB
  • RO:该项表明设备是否为只读。在本案例中,所有设备的 RO 值为0,表明他们不是只读的
  • TYPE:本栏显示块设备是否是磁盘或磁盘上的一个分区。在本例中,sda 和 sdb 是磁盘,而 sr0 是只读存储(rom)
  • MOUNTPOINT:本栏指出设备挂载的挂载点
df 命令

查看文件系统使用情况

[root@centos7 ~ 06:45:58]# df
文件系统                   1K-块    已用     可用 已用% 挂载点
devtmpfs                  485776       0   485776    0% /dev
tmpfs                     497816       0   497816    0% /dev/shm
tmpfs                     497816    7788   490028    2% /run
tmpfs                     497816       0   497816    0% /sys/fs/cgroup
/dev/mapper/centos-root 52403200 1708808 50694392    4% /
/dev/mapper/centos-home 49250820   33008 49217812    1% /home
/dev/sda1                1038336  142212   896124   14% /boot
tmpfs                      99564       0    99564    0% /run/user/0

# -h选项:友好显示单位,单位进制是1024
# -T选项:显示文件系统类型
[root@centos7 ~ 06:47:31]# df -hT
文件系统                类型      容量  已用  可用 已用% 挂载点
devtmpfs                devtmpfs  475M     0  475M    0% /dev
tmpfs                   tmpfs     487M     0  487M    0% /dev/shm
tmpfs                   tmpfs     487M  7.7M  479M    2% /run
tmpfs                   tmpfs     487M     0  487M    0% /sys/fs/cgroup
/dev/mapper/centos-root xfs        50G  1.7G   49G    4% /
/dev/mapper/centos-home xfs        47G   33M   47G    1% /home
/dev/sda1               xfs      1014M  139M  876M   14% /boot
tmpfs                   tmpfs      98M     0   98M    0% /run/user/0

# -H 选项:单位进制是1000
[root@centos7 ~ 06:48:10]# df -H
文件系统                 容量  已用  可用 已用% 挂载点
devtmpfs                 498M     0  498M    0% /dev
tmpfs                    510M     0  510M    0% /dev/shm
tmpfs                    510M  8.0M  502M    2% /run
tmpfs                    510M     0  510M    0% /sys/fs/cgroup
/dev/mapper/centos-root   54G  1.8G   52G    4% /
/dev/mapper/centos-home   51G   34M   51G    1% /home
/dev/sda1                1.1G  146M  918M   14% /boot
tmpfs                    102M     0  102M    0% /run/user/0

# 查看单个文件系统
[root@centos7 ~ 06:48:32]# df -hT /boot
文件系统       类型  容量  已用  可用 已用% 挂载点
/dev/sda1      xfs  1014M  139M  876M   14% /boot

# 查看文件或目录存储在哪个设备
[root@centos7 ~ 06:48:57]# df /tmp
文件系统                   1K-块    已用     可用 已用% 挂载点
/dev/mapper/centos-root 52403200 1708808 50694392    4% /
du 命令

查看目录和文件占用磁盘空间大小

# 查看 /boot 目录及其子目录占用空间
[root@centos7 ~ 06:49:15]# du /boot
0       /boot/efi/EFI/centos
0       /boot/efi/EFI
0       /boot/efi
2400    /boot/grub2/i386-pc
3176    /boot/grub2/locale
2504    /boot/grub2/fonts
8096    /boot/grub2
4       /boot/grub
108996  /boot

# 只查看 /boot 目录占用空间
[root@centos7 ~ 06:49:57]# du -s /boot
108996  /boot

# -h 选项 human 方式显示 size
[root@centos7 ~ 06:50:26]# du -sh /boot
107M    /boot
案例

查找根文件系统中哪个文件占用了大量空间

# 环境准备,创建一个大小为4GB的文件:使用 0 填充该文件
[root@centos7 ~ 06:51:06]# dd if=/dev/zero of=/usr/share/doc/dhclient-4.2.5/bigfile-4G  bs=1M count=4096
记录了4096+0 的读入
记录了4096+0 的写出
4294967296字节(4.3 GB)已复制,5.49751 秒,781 MB/秒
# 查看过程如下:
[root@centos7 ~ 06:51:39]# du -sk /* | sort -n
...
0       /sys
4       /tmp
16      /home
36      /root
7788    /run
32604   /etc
108996  /boot
380096  /var
5440380 /usr
[root@centos7 ~ 06:52:12]# du -sk /usr/* | sort -n
...
0       /usr/tmp
36      /usr/include
12328   /usr/libexec
43708   /usr/sbin
66148   /usr/bin
134660  /usr/lib64
711536  /usr/lib
4471964 /usr/share

[root@centos7 ~ 06:52:49]# du -sk /usr/share/* | sort -n
...
5124    /usr/share/microcode_ctl
5536    /usr/share/mime
7884    /usr/share/hwdata
9192    /usr/share/cracklib
9704    /usr/share/i18n
15704   /usr/share/man
18828   /usr/share/backgrounds
19816   /usr/share/perl5
25204   /usr/share/vim
94348   /usr/share/locale
4229052 /usr/share/doc

[root@centos7 ~ 06:53:31]# du -sk /usr/share/doc/* | sort -n
...
728     /usr/share/doc/slang-2.2.4
736     /usr/share/doc/tar-1.26
760     /usr/share/doc/man-db-2.6.3
776     /usr/share/doc/lsof-4.87
872     /usr/share/doc/rpm-4.11.3
940     /usr/share/doc/coreutils-8.22
1060    /usr/share/doc/pam-1.1.8
1216    /usr/share/doc/postfix-2.10.1
4194316 /usr/share/doc/dhclient-4.2.5

# 另一种方法
[root@centos7 ~ 06:54:07]# find / -size +100M 2>/dev/null
/proc/kcore
/sys/devices/pci0000:00/0000:00:0f.0/resource1_wc
/sys/devices/pci0000:00/0000:00:0f.0/resource1
/var/cache/yum/x86_64/7/updates/gen/primary_db.sqlite
/usr/lib/locale/locale-archive
/usr/share/doc/dhclient-4.2.5/bigfile-4G

挂载和卸载文件系统

环境准备

# 虚拟机添加一块硬盘 /dev/sdb
[root@centos7 ~ 06:55:13]# lsblk /dev/sdb
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sdb    8:16   0  20G  0 disk 

# 格式化文件系统
[root@centos7 ~ 06:56:36]# mkfs.xfs /dev/sdb -f
meta-data=/dev/sdb               isize=512    agcount=4, agsize=1310720 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=5242880, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

挂载文件系统

当需要适用文件系统的时候,通过mount命令挂载

选项说明:

  • -t vfstype:指明文件系统类型
  • -o options:指明挂载选项
  • device:指明要挂载的具有文件系统的设备,可以通过 UUID=、LABEL=
  • mountpoint:指明挂载点位置
# 创建挂载点
[root@centos7 ~ 06:56:49]# mkdir /data

# 显示系统中所有文件系统
[root@centos7 ~ 06:57:55]# blkid
/dev/sr0: UUID="2022-07-26-15-09-17-00" LABEL="CentOS 7 x86_64" TYPE="iso9660" PTTYPE="dos" 
/dev/sda1: UUID="1983d62a-ce62-4572-991b-e51d45473e1c" TYPE="xfs" 
/dev/sda2: UUID="87eUfg-rvN0-aPRd-nP14-zAUx-bawZ-1Jc75j" TYPE="LVM2_member" 
/dev/mapper/centos-root: UUID="326a13ec-85d2-4aae-8285-ff673a40b35a" TYPE="xfs" 
/dev/mapper/centos-swap: UUID="732d3fdb-2243-4101-be74-875d1db2f9f8" TYPE="swap" 
/dev/mapper/centos-home: UUID="3b2ad2c5-0f07-4c30-a6d9-10da2914cbfe" TYPE="xfs" 
/dev/sdb: UUID="b42fb64b-e359-429b-bc1c-fbda53e0bbf8" TYPE="xfs" 

# 显示系统中特定文件系统
[root@centos7 ~ 06:58:09]# blkid /dev/sdb
/dev/sdb: UUID="b42fb64b-e359-429b-bc1c-fbda53e0bbf8" TYPE="xfs" 

# 挂载设备并验证
[root@centos7 ~ 06:58:37]# mount /dev/sdb /data

# 验证
[root@centos7 ~ 06:59:03]# df -h /data
文件系统        容量  已用  可用 已用% 挂载点
/dev/sdb         20G   33M   20G    1% /data

# 创建文件测试
[root@centos7 ~ 06:59:31]# touch /data/file-{1..5}
[root@centos7 ~ 07:00:02]# ls /data
file-1  file-2  file-3  file-4  file-5

查看系统当前所有挂载,可以看到挂载的详细信息

[root@centos7 ~ 07:00:06]# mount
sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime)
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
devtmpfs on /dev type devtmpfs (rw,nosuid,size=485776k,nr_inodes=121444,mode=755)
securityfs on /sys/kernel/security type securityfs (rw,nosuid,nodev,noexec,relatime)
tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev)
devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000)
...
# 括号中属性,是文件系统当前支持的功能

[root@centos7 ~ 07:01:12]# mount | grep ^/dev
/dev/mapper/centos-root on / type xfs (rw,relatime,attr2,inode64,noquota)
/dev/mapper/centos-home on /home type xfs (rw,relatime,attr2,inode64,noquota)
/dev/sda1 on /boot type xfs (rw,relatime,attr2,inode64,noquota)
/dev/sdb on /data type xfs (rw,relatime,attr2,inode64,noquota)

卸载文件系统

当文件系统不使用的时候,通过 umount 命令卸载

可以通过指定设备或者挂载点,卸载文件系统

示例:

[root@centos7 ~ 07:01:21]# umount /dev/sdb

# 或者
[root@centos7 ~ 07:01:46]# umount /data

[root@centos7 ~ 07:02:11]# df -h /data
文件系统                 容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root   50G  5.7G   45G   12% /

卸载失败处理

[root@centos7 ~ 07:02:17]# mount /dev/sdb /data
[root@centos7 ~ 07:03:04]# cd /data
[root@centos7 data 07:03:11]# ll
总用量 0
-rw-r--r-- 1 root root 0 9月  20 07:00 file-1
-rw-r--r-- 1 root root 0 9月  20 07:00 file-2
-rw-r--r-- 1 root root 0 9月  20 07:00 file-3
-rw-r--r-- 1 root root 0 9月  20 07:00 file-4
-rw-r--r-- 1 root root 0 9月  20 07:00 file-5

# 提示挂载点正在忙碌
[root@centos7 ~ 07:03:24]# umount /dev/sdb
umount: /data:目标忙。
        (有些情况下通过 lsof(8) 或 fuser(1) 可以
         找到有关使用该设备的进程的有用信息)
lsof 命令

lsof,list open files:用于查看系统打开的文件

常用选项:

  • -i:查看打开的 Internet 文件。例如 -i @10.1.8.20:22、-i :80
  • -p pid:根据进程 PID 查找特定进程打开的文件
  • -u uid:根据用户 uid 查找特定用户打开的文件
  • names:是文件或者文件系统设备
# 新开窗口查看哪个进程在使用挂载点
[root@centos7 ~ 07:07:16]# lsof /data
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
bash    1318 root  cwd    DIR   8,16       76   64 /data
fuser 命令

fuser:用于识别进程打开的文件

常用选项:

  • -k:杀死这些正在访问这些文件的进程。除非使用 -signal 修改信号,否则将发送 SIGKILL 信号
  • -signal:指定发送的信号(signal 使用特定信号替换),缺省信号 SIGKILL
  • -i:交互模式杀死进程
  • -l:列出所有已知的信号名称
  • -m:列出文件系统被哪些程序使用
  • -n:选择不同的名字空间,可是 file,udp,tcp。默认是 file,也就是文件
  • -4:仅查询 IPV4 套接字
  • -6:仅查询 IPV6 套接字
  • -u:指定用户名
# 新开窗口查看哪个进程在使用挂载点
[root@centos7 ~ 07:10:16]# fuser -m /data
/data:                1318c
终止进程,再次卸载
# 返回家目录,再次卸载
[root@centos7 ~ 07:10:23]# umount /data
[root@centos7 ~ 07:11:20]# df /data
文件系统                   1K-块    已用     可用 已用% 挂载点
/dev/mapper/centos-root 52403200 6214084 46189116   12% /

查找系统中文件

locate

locate 命令根据文件名及其路径,在 mlocate 数据库中查找文件,并返回结果。数据库中存放文件和文件路径信息

常规用户查找时,返回的结果仅包含用户有读取权限的目录树中匹配项

查找文件前,需要 root 用户手动执行 updatedb 命令更新 mlocate 数据库

[root@centos7 ~ 07:13:30]# updatedb

常用选项:

  • -b: –basename
  • -i:–ignore-case
  • -c:–count
  • -r:–regexp

示例:

[root@centos7 ~ 07:14:59]# yum install -y httpd
# 安装后,未跟新数据库,所以检索不到

[root@centos7 ~ 07:14:59]# locate httpd.conf

# 更新后查找
[root@centos7 ~ 07:15:23]# updatedb

[root@centos7 ~ 07:15:43]# locate httpd.conf
/etc/httpd/conf/httpd.conf
/usr/lib/tmpfiles.d/httpd.conf

# -b选项 查找
[root@centos7 ~ 07:15:52]# locate -b httpd
/etc/httpd
/etc/httpd/conf/httpd.conf
/etc/logrotate.d/httpd
/etc/sysconfig/httpd
/usr/lib/systemd/system/httpd.service
/usr/lib/tmpfiles.d/httpd.conf
/usr/lib64/httpd
...

# -i选项 忽略大小写
[root@centos7 ~ 07:16:17]# locate PASSWD
/etc/PASSWD

[root@centos7 ~ 07:16:45]# locate -i PASSWD
/etc/passwd
/etc/passwd-
/etc/pam.d/passwd
/etc/security/opasswd
/usr/bin/gpasswd
...

# -c选项 返回找到的数量
[root@centos7 ~ 07:17:50]# locate -b -c -i PASSWD
118

find

find 命令在本地文件系统中执行实时查找文件。使用 find 命令的用户对文件夹必须有读取和执行权限

  • path:是查询路径,如果没有指定文件夹,将会查找当前目录及子目录
  • expression:是查询条件表达式
  • action:是找到文件后采取的动作
根据文件 name 查找
[root@centos7 ~ 07:18:51]# touch /etc/PASSWD

[root@centos7 ~ 07:19:13]# find /etc/ -name passwd
/etc/passwd
/etc/pam.d/passwd

[root@centos7 ~ 07:19:39]# find /etc/ -name '*passwd*'
/etc/passwd-
/etc/passwd
/etc/pam.d/passwd
/etc/security/opasswd

[root@centos7 ~ 07:20:02]# find /etc/ -iname passwd
/etc/passwd
/etc/pam.d/passwd
/etc/PASSWD
根据文件 type 查找

-type 根据文件类型查找,支持文件类型:

  • b:block (buffered) special
  • c:character (unbuffered) special
  • d:directory
  • p:named pipe (FIFO)
  • f:regular file
  • l:symbolic link
  • s:socket
根据文件 owner 查找

-user、-uid:属于特定用户

[root@centos7 ~ 07:20:17]# id wan
uid=1000(wan) gid=1000(wan) =1000(wan)

[root@centos7 ~ 07:20:57]# find / -user wan

[root@centos7 ~ 07:21:37]# find / uid 1000

-group、-gid:属于特定组

[root@centos7 ~ 07:21:37]# grep wheel /etc/group
wheel:x:10:

[root@centos7 ~ 07:22:01]# find / -group wheel

[root@centos7 ~ 07:22:11]# find / -gid 10

-nouser:不属于任何用户;-nogroup:不属于任何组

# 查找系统中不属于任何用户或者不属于任何组的文件
[root@centos7 ~ 07:22:40]# find / -nouser -o -nogroup
根据文件 perm 查找
[root@centos7 ~ 07:23:23]# mkdir lab

[root@centos7 ~ 07:23:47]# cd lab

[root@centos7 lab 07:23:49]# touch file-{1..5}

[root@centos7 lab 07:24:09]# ll file*
-rw-r--r-- 1 root root 0 9月  20 07:24 file-1
-rw-r--r-- 1 root root 0 9月  20 07:24 file-2
-rw-r--r-- 1 root root 0 9月  20 07:24 file-3
-rw-r--r-- 1 root root 0 9月  20 07:24 file-4
-rw-r--r-- 1 root root 0 9月  20 07:24 file-5

-perm mode:查找文件权限为 mode 的文件

[root@centos7 lab 07:24:19]# chmod 764 file-1

# 查找文件权限为764的文件
[root@centos7 lab 07:25:11]# find -perm 764 | xargs ls -l
-rwxrw-r-- 1 root root 0 9月  20 07:24 ./file-1
根据文件 size 查找

单位 c(字节)、k(KiB)、M(MiB)、G(GiB)

# 大小等于10M
[root@centos7 lab 07:25:18]# find -size 10M

# 大小大于10G
[root@centos7 lab 07:26:59]# find -size +10G

# 注意:size 会取整为1个单位,所以find -size 1M结果包含小于1M的文件。
# 可以使用1024k取代1M
[root@centos7 lab 07:27:42]# find -size 1024k
根据文件 time 查找
  • -amin, -cmin, -mmin:单位1分钟
  • -atime, -ctime, -mtime:单位24小时
  • -newer file:比file新的文件
# 10分钟前(正好10分钟)
[root@centos7 lab 07:27:48]# find -amin 10
多条件表达式

逻辑与: expr1 -a expr2 或者 expr1 expr2

[root@centos7 lab 07:28:18]# find . -name 'file-*' -perm /764
./file-1
./file-2
./file-3
./file-4
./file-5

逻辑或:expr1 -o expr2

# 查找系统中不属于任何用户或者不属于任何组的文件
[root@centos7 lab 07:28:44]# find / -nouser -o -nogroup

逻辑非:! expr

[root@centos7 lab 07:29:06]# find / ! -size -200M 2>/dev/null
/proc/kcore
/proc/85063/task/85063/fd/6
/proc/85063/task/85063/fdinfo/6
/proc/85063/fd/5
/proc/85063/fdinfo/5
/usr/share/doc/dhclient-4.2.5/bigfile-4G

[root@centos7 lab 07:29:24]# ll -lh /proc/kcore
-r-------- 1 root root 128T 9月  20 07:29 /proc/kcore
action

-delete:查出找到的文件

[root@centos7 ~ 07:31:32]# find / -name PASSWD
/etc/PASSWD

[root@centos7 ~ 07:31:49]# find / -name PASSWD -delete

[root@centos7 ~ 07:32:03]# find / -name PASSWD

-ls:相当于 ls -dils 查看找到的文件

[root@centos7 ~ 07:32:08]# find /etc/ -name passwd 
/etc/passwd
/etc/pam.d/passwd

[root@centos7 ~ 07:32:54]# find /etc/ -name passwd -ls
67548567    4 -rw-r--r--   1 root     root         1107 9月 20 07:14 /etc/passwd
34168484    4 -rw-r--r--   1 root     root          188 4月  1  2020 /etc/pam.d/passwd

-exec command ;:找到文件后,执行相应的 command

[root@centos7 ~ 07:33:12]# find /etc/ -name passwd -exec echo haha \;
haha
haha

-exec command {} ;:找到文件后,可以在命令中对文件操作

[root@centos7 ~ 07:33:52]# find / -inum 67160130
/etc/fstab

# 将 inode 为67160130的所有文件复制到当前目录
[root@centos7 ~ 07:34:20]# mkdir findfiles

[root@centos7 ~ 07:34:40]# find / -inum 67160130 -exec cp -a {} ./findfiles \;

[root@centos7 ~ 07:35:16]# ls findfiles/fstab 
findfiles/fstab