Linux 文件系统基本管理

查看设备和文件系统

lsblk 命令

 #查看块设备使用情况。
 [root@server ~ 15:10:25]# lsblk
 NAME            MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
 sda               8:0    0   200G  0 disk 
 ├─sda1            8:1    0     1G  0 part /boot
 └─sda2            8:2    0   199G  0 part 
   ├─centos-root 253:0    0    50G  0 lvm  /
   ├─centos-swap 253:1    0   3.9G  0 lvm  [SWAP]
   └─centos-home 253:2    0 145.1G  0 lvm  /home
 sr0              11:0    1   4.4G  0 rom  
 ​
 ​

df 命令

 #查看文件系统使用情况
 [root@server ~ 15:28:48]# df
 文件系统                    1K-块    已用      可用 已用% 挂载点
 devtmpfs                  1995980       0   1995980    0% /dev
 tmpfs                     2013056       0   2013056    0% /dev/shm
 tmpfs                     2013056   12596   2000460    1% /run
 tmpfs                     2013056       0   2013056    0% /sys/fs/cgroup
 /dev/mapper/centos-root  52403200 7116380  45286820   14% /
 /dev/mapper/centos-home 152092100 1452688 150639412    1% /home
 /dev/sda1                 1038336  177428    860908   18% /boot
 tmpfs                      402612      12    402600    1% /run/user/42
 tmpfs                      402612       0    402612    0% /run/user/0
 ​
 # -h选项,友好显示单位,单位进制是1024。
 # -T选项,显示文件系统类型。
 [root@server ~ 15:29:52]#  df -hT
 文件系统                类型      容量  已用  可用 已用% 挂载点
 devtmpfs                devtmpfs  2.0G     0  2.0G    0% /dev
 tmpfs                   tmpfs     2.0G     0  2.0G    0% /dev/shm
 tmpfs                   tmpfs     2.0G   13M  2.0G    1% /run
 tmpfs                   tmpfs     2.0G     0  2.0G    0% /sys/fs/cgroup
 /dev/mapper/centos-root xfs        50G  6.8G   44G   14% /
 /dev/mapper/centos-home xfs       146G  1.4G  144G    1% /home
 /dev/sda1               xfs      1014M  174M  841M   18% /boot
 tmpfs                   tmpfs     394M   12K  394M    1% /run/user/42
 tmpfs                   tmpfs     394M     0  394M    0% /run/user/0
 ​
 # -H选项,单位进制是1000
 [root@server ~ 15:30:16]# df -H
 文件系统                 容量  已用  可用 已用% 挂载点
 devtmpfs                 2.1G     0  2.1G    0% /dev
 tmpfs                    2.1G     0  2.1G    0% /dev/shm
 tmpfs                    2.1G   13M  2.1G    1% /run
 tmpfs                    2.1G     0  2.1G    0% /sys/fs/cgroup
 /dev/mapper/centos-root   54G  7.3G   47G   14% /
 /dev/mapper/centos-home  156G  1.5G  155G    1% /home
 /dev/sda1                1.1G  182M  882M   18% /boot
 tmpfs                    413M   13k  413M    1% /run/user/42
 tmpfs                    413M     0  413M    0% /run/user/0
 ​
 # 查看单个文件系统
 [root@server ~ 15:30:33]# df -hT /boot
 文件系统       类型  容量  已用  可用 已用% 挂载点
 /dev/sda1      xfs  1014M  174M  841M   18% /boot
 # 查看文件或目录存储在哪个设备
 [root@server ~ 15:30:51]# df /tmp
 文件系统                   1K-块    已用     可用 已用% 挂载点
 /dev/mapper/centos-root 52403200 7116384 45286816   14% /

du命令

 # 查看/boot目录及其子目录占用空间
 [root@server ~ 15:31:04]# du /boot
 0   /boot/efi/EFI/centos/fw
 6060    /boot/efi/EFI/centos
 1928    /boot/efi/EFI/BOOT
 7988    /boot/efi/EFI
 7988    /boot/efi
 2400    /boot/grub2/i386-pc
 3176    /boot/grub2/locale
 2504    /boot/grub2/fonts
 8096    /boot/grub2
 4   /boot/grub
 144212  /boot
 ​
 # 只查看/boot目录占用空间
 [root@server ~ 15:31:29]#  du -s /boot
 144212  /boot
 ​
 # -h 选项human方式显示size
 [root@server ~ 15:31:39]# du -sh /boot
 141M    /boot

案例

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

 # 环境准备,创建一个大小为4GB的文件:使用 0 填充该文件
 [root@server ~ 15:34:24]# 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)已复制,13.8809 秒,309 MB/秒
 ​
 ​
 # 查看过程如下:
 [root@server ~ 15:34:51]# du -sk /* |sort -n
 ......
 4   /hosts
 4   /opt
 12  /lab
 348 /tmp
 12608   /run
 43216   /etc
 144212  /boot
 1038312 /var
 1419472 /home
 2097296 /root
 8031944 /usr
 ​
 [root@server ~ 15:35:16]# du -sk /usr/* |sort -n
 0   /usr/etc
 0   /usr/games
 0   /usr/src
 0   /usr/tmp
 4   /usr/local
 13796   /usr/include
 66684   /usr/sbin
 73856   /usr/libexec
 147948  /usr/bin
 877472  /usr/lib
 1214028 /usr/lib64
 5638156 /usr/share
 ​
 [root@server ~ 15:35:46]# du -sk /usr/share/* |sort -n
 0   /usr/share/egl
 0   /usr/share/empty
 0   /usr/share/file
 ......
 ​
 [root@server ~ 15:36:49]# du -sk /usr/share/doc/* |sort -n
 ......
 ​
 ​
 [root@server ~ 15:36:49]#  du -sk /usr/share/doc/dhclient-4.2.5/* |sort -n
 4   /usr/share/doc/dhclient-4.2.5/dhclient6.conf.example
 4   /usr/share/doc/dhclient-4.2.5/dhclient.conf.example
 4   /usr/share/doc/dhclient-4.2.5/README.dhclient.d
 4194304 /usr/share/doc/dhclient-4.2.5/bigfile-4G
 ​
 # 方法二:
 [root@server ~ 15:37:11]# find / -size +100M 2>/dev/null
 /home/lyk/LaoMaoTao-PE.iso
 /proc/kcore
 /sys/devices/pci0000:00/0000:00:0f.0/resource1_wc
 /sys/devices/pci0000:00/0000:00:0f.0/resource1
 /root/bigfile
 ​

挂载和卸载文件系统

环境准备
 # 编辑虚拟机添加一块硬盘 /dev/sdb
 [root@server ~ 15:37:36]# lsblk 
 NAME            MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
 sda               8:0    0   200G  0 disk 
 ├─sda1            8:1    0     1G  0 part /boot
 └─sda2            8:2    0   199G  0 part 
   ├─centos-root 253:0    0    50G  0 lvm  /
   ├─centos-swap 253:1    0   3.9G  0 lvm  [SWAP]
   └─centos-home 253:2    0 145.1G  0 lvm  /home
 sdb               8:16   0   100G  0 disk 
 sr0              11:0    1   4.4G  0 rom  
 # 格式化文件系统
 [root@server ~ 15:54:04]# mkfs.xfs /dev/sdb 
 meta-data=/dev/sdb               isize=512    agcount=4, agsize=6553600 blks
          =                       sectsz=512   attr=2, projid32bit=1
          =                       crc=1        finobt=0, sparse=0
 data     =                       bsize=4096   blocks=26214400, imaxpct=25
          =                       sunit=0      swidth=0 blks
 naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
 log      =internal log           bsize=4096   blocks=12800, version=2
          =                       sectsz=512   sunit=0 blks, lazy-count=1
 realtime =none                   extsz=4096   blocks=0, rtextents=0
 ​
 # 创建挂载点
 [root@server ~ 15:54:30]# mkdir /data
 # 挂载设备并验证
 [root@server ~ 15:55:59]# mount /dev/sdb /data
 # 验证
 [root@server ~ 15:56:20]# df -h /data
 文件系统        容量  已用  可用 已用% 挂载点
 /dev/sdb        100G   33M  100G    1% /data
 ​
 # 显示系统中特定文件系统
 [root@server ~ 15:56:45]# blkid /dev/sdb
 /dev/sdb: UUID="9bfe1c70-3704-4ac0-8626-d1f461636e79" TYPE="xfs" 
 ​
 [root@server ~ 16:01:39]# touch /data/file-{00..09}
 [root@server ~ 16:20:58]# ls /data
 file-00  file-02  file-04  file-06  file-08
 file-01  file-03  file-05  file-07  file-09
 ​

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

 [root@server ~ 16:21:04]# mount
 sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime,seclabel)
 proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
 devtmpfs on /dev type devtmpfs (rw,nosuid,seclabel,size=199
 ......
 # 括号中属性,是文件系统当前支持的功能
 [root@server ~ 16:22:19]# mount |grep ^/dev
 /dev/mapper/centos-root on / type xfs (rw,relatime,seclabel,attr2,inode64,noquota)
 /dev/mapper/centos-home on /home type xfs (rw,relatime,seclabel,attr2,inode64,noquota)
 /dev/sda1 on /boot type xfs (rw,relatime,seclabel,attr2,inode64,noquota)
 /dev/sdb on /data type xfs (rw,relatime,seclabel,attr2,inode64,noquota)
 ​

卸载文件系统

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

umount命令语法:

 umount device | dir

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

示例:

 [root@server ~ 16:22:22]# umount /dev/sdb
 或者
 [root@server ~ 16:23:50]# umount /data
 ​
 [root@server ~ 16:23:50]# df -h /data
 文件系统                 容量  已用  可用 已用% 挂载点
 /dev/mapper/centos-root   50G   11G   40G   22% /

卸载失败处理

#挂载
[root@server ~ 16:24:30]# mount /dev/sdb /data
#cd到/data
[root@server ~ 16:25:07]# cd /data
[root@server data 16:25:14]# ls
file-00  file-02  file-04  file-06  file-08
file-01  file-03  file-05  file-07  file-09

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

lsof 命令

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

lsof 命令语法:

lsof [options] [names]

常用选项:

  • -i,查看打开的 Internet 文件。例如 -i @10.1.8.20:22-i :80

  • -p pid,根据进程PID查找特定进程打开的文件。

  • -u uid,根据用户 uid查找特定用户打开的文件。

  • names,是文件或者文件系统设备。

[root@server ~ 16:29:57]# cd /data
[root@server data 16:30:10]# lsof /data
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
bash    1950 root  cwd    DIR   8,16      156   64 /data
lsof    6349 root  cwd    DIR   8,16      156   64 /data
lsof    6350 root  cwd    DIR   8,16      156   64 /data

fuser 命令

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

fuser 命令语法:

fuser [-fMuvw] [-a|-s] [-4|-6] [-c|-m|-n SPACE] [-k [-i] [-SIGNAL]] NAME...

常用选项:

  • -k,杀死这些正在访问这些文件的进程。除非使用-signal修改信号,否则将发送SIGKILL信号。

  • -signal,指定发送的信号(signal 使用特定信号替换),缺省信号SIGKILL。

  • -i,交互模式杀死进程。

  • -l,列出所有已知的信号名称。

  • -m,列出文件系统被哪些程序使用。

  • -n,空间,选择不同的名字空间,可是file,udp,tcp。默认是file,也就是文件。

  • -4,仅查询IPV4套接字

  • -6,仅查询IPV6套接字。

  • -u,指定用户名。

# 新开窗口查看哪个进程在使用挂载点
[root@server ~ 16:30:58]# cd /data
[root@server data 16:31:02]# fuser -m /data
/data:                1950c
终止进程,再次卸载
# 返回家目录,再次卸载
[root@server data 16:31:04]# cd
[root@server ~ 16:31:48]# umount /data

Linux 挂载 NTFS 格式U盘

思路:默认情况下 Linux不识别NTFS格式文件系统,需要安装相应的模块进行识别。

  1. 安装 epel 仓库

  2. 安装 ntfs-3g (该软件包由epel仓库提供)

  3. 挂载

案例:Linux 挂载 exfat 格式U盘

思路:默认情况下 Linux 不识别 exfat 格式文件系统,需要安装相应的模块进行识别。

# 安装软件
yum install -y git pkg-config fuse-devel gcc make autoconf automake
git clone https://github.com/relan/exfat.git
cd exfat
autoreconf --install
./configure
make && make install

mount.exfat-fuse /dev/spec /mnt/exfat

查找系统中文件

locate(可忽略大小写)

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

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

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

[root@centos7 ~]# updatedb

locate命令语法:

locate [OPTION]... [PATTERN]...

常用选项:

  • -b, --basename

  • -i, --ignore-case

  • -c, --count

  • -r, --regexp

[root@server ~ 16:31:55]# yum install -y mlocate
已加载插件:fastestmirror, langpacks
Determining fastest mirrors
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
base                                                     | 3.6 kB     00:00     
epel                                                     | 4.3 kB     00:00     
extras                                                   | 2.9 kB     00:00     
updates                                                  | 2.9 kB     00:00     
软件包 mlocate-0.26-8.el7.x86_64 已安装并且是最新版本
无须任何处理
#man-db.cron,执行了mandb,自动更新
[root@server ~ 16:42:01]# updatedb
[root@server ~ 16:42:23]# cd /etc/cron.daily/
[root@server cron.daily 16:42:37]# ls
logrotate  man-db.cron  mlocate
#updatedb开始更新已经有的,后面装的自己更新
[root@server cron.daily 16:42:41]# yum install -y httpd
[root@server cron.daily 16:44:43]# updatedb
[root@server cron.daily 16:44:26]# locate httpd.conf
/etc/httpd/conf/httpd.conf
/usr/lib/tmpfiles.d/httpd.conf

find命令

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

语法:

find [path] [expression] [action]
  • path,是查询路径,如果没有指定文件夹,将会查找当前目录及子目录。

  • expression,是查询条件表达式。

  • action,是找到文件后采取的动作。

根据文件 name 查找
[root@server ~ 17:29:16]# touch /etc/PASSWD

[root@server ~ 17:29:55]# find /etc/ -name passwd
/etc/pam.d/passwd
/etc/passwd

[root@server ~ 17:30:01]# find /etc/ -name '*passwd*'
/etc/pam.d/passwd
/etc/passwd
/etc/security/opasswd
/etc/passwd-

[root@server ~ 17:30:12]# find /etc/ -iname passwd
/etc/pam.d/passwd
/etc/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@server ~ 16:44:43]# id lyk
uid=1000(lyk) gid=1000(lyk) 组=1000(lyk),10(wheel)
[root@server ~ 17:10:15]#  find / -user lyk
[root@server ~ 17:10:50]# find / -uid 1000
  • -group、-gid,属于特定组

[root@server ~ 17:10:50]# grep wheel /etc/group
wheel:x:10:lyk
[root@server ~ 17:12:29]# find / -group wheel
[root@server ~ 17:12:43]#  find / -gid 10
  • -nouser,不属于任何用户;-nogroup,不属于任何组

    # 查找系统中不属于任何用户或者不属于任何组的文件
    [root@server ~ 17:12:43]# find / -nouser -o -nogroup
    #比如一个laowang创建的文件,userdel laowang 那么他的文件就不属于任何组/用户
根据文件 perm 查找
#准备文件
[root@server ~ 17:16:55]# mkdir lab
[root@server ~ 17:17:02]# cd lab
[root@server lab 17:17:05]# touch file-{1..3}
[root@server lab 17:17:10]# ls -l file*
-rw-rw-r--. 1 root root 0 7月  31 17:17 file-1
-rw-rw-r--. 1 root root 0 7月  31 17:17 file-2
-rw-rw-r--. 1 root root 0 7月  31 17:17 file-3
  • -perm mode,查找文件权限为mode的文件。

    [root@server lab 17:17:13]# chmod 764 file-1
    [root@server lab 17:18:53]# find -perm 764 | xargs ls -l
    -rwxrw-r--. 1 root root 0 7月  31 17:17 ./file-1
  • -perm -mode,例如:mode为764,则ugo必须同时满足的最小权限:user至少为7、group至少为6、other为4。

    [root@server lab 17:19:44]# chmod 777 file-1
    [root@server lab 17:20:16]# chmod 764 file-2
    [root@server lab 17:20:20]# chmod 760 file-3
    [root@server lab 17:20:26]# find . -perm -764 | xargs ls -l
    -rwxrwxrwx. 1 root root  0 7月  31 17:17 ./file-1
    -rwxrw-r--. 1 root root  0 7月  31 17:17 ./file-2
    
    
    # 0作为通配符,表示不匹配对应权限位。
    # 查找系统中具有suid权限的文件
    [root@server lab 17:20:31]# find / -perm -4000
    
    # 查找系统中同时具有特殊权限的文件:suid和sgid
    [root@server lab 17:21:43]# find / -perm -6000 2>/dev/null |xargs ls -l
    -rwsr-sr-x. 1 abrt abrt 15344 10月  2 2020 /usr/libexec/abrt-action-install-debuginfo-to-abrt-cache
  • -perm /mode,例如mode为764,则ugo只要有一个满足即可:user至少为7、group至少为6、other至少为4。

    [root@server lab 17:21:46]# chmod a=- file-1
    [root@server lab 17:22:04]# chmod uo=-,g=rwx file-2
    [root@server lab 17:22:08]# chmod ug=-,o=r file-3
    [root@server lab 17:22:12]# find . -name 'file-*' -perm /764| xargs ls -l
    ----rwx---. 1 root root 0 7月  31 17:17 ./file-2
    -------r--. 1 root root 0 7月  31 17:17 ./file-3
    
    # 查找系统中具有特殊权限的文件:suid、sgid、sticky
    [root@server lab 17:22:37]# find / -perm /7000
    
    
    # 查找系统中具有特殊权限的文件:suid或者sgid
    [root@server lab 17:22:37]# find / -perm /6000
    根据文件 size 查找
    • 单位 c(字节)、k(KiB)、M(MiB)、G(GiB)

    # 大小等于10M
    [root@server ~ 17:23:31]# find -size 10M
    # 大小大于10G
    [root@server ~ 17:23:34]# find -size +10G
    # 大小小于10k
    [root@server ~ 17:23:39]# find -size -10k
    # 大小等于1M
    [root@server ~ 17:23:53]# find -size 1M
    # 注意:size会取整为1个单位,所以find -size 1M结果包含小于1M的文件。
    # 可以使用1024k取代1M
    [root@server ~ 17:24:01]# find -size 1024k
    根据文件 time 查找
  • -amin, -cmin, -mmin 单位1分钟。

  • -atime, -ctime, -mtime 单位24小时。

  • -newer file,比file新的文件。

# 10分钟前(正好10分钟)
[root@server ~ 17:24:14]#  find -amin 10
# 10分钟以前(大于10分钟)
[root@server ~ 17:25:28]# find -amin +10
# 10分钟以内(小于10分钟)
[root@server ~ 17:25:42]# find -amin -10
.
./lab
./lab/file-1
./lab/file-2
./lab/file-3
根据文件硬链接数和 inum 查找
# 硬链接数等于、大于、小于3的文件
[root@server ~ 17:25:56]# find -links 3
./.dbus
./.config
[root@server ~ 17:26:46]# find -links +3
.
./.cache
[root@server ~ 17:27:04]# find -links -3

# inode为67160130的文件
[root@server ~ 17:27:04]# ls -i /etc/fstab
67160130 /etc/fstab
[root@server ~ 17:27:28]# find / -inum 67160130| xargs ls -i
67160130 /etc/fstab
多条件表达式
  • 逻辑与: expr1 -a expr2 或者 expr1 expr2

    [root@server lab 17:28:17]# find . -name 'file-*' -perm /764
  • 逻辑或:expr1 -o expr2

    # 查找系统中不属于任何用户或者不属于任何组的文件
    [root@server lab 17:28:20]# find / -nouser -o -nogroup
    
    # 例如
    [root@server lab 17:28:39]# find / -perm /7000
    # 等效与
    [root@server lab 17:28:50]# find / -perm -4000 -o -perm -2000 -o -perm -1000
    
  • 逻辑非:! expr

    [root@centos7 ~]# find / ! -size -200M 2>/dev/null
    /proc/kcore
    /proc/59432/task/59432/fd/5
    /proc/59432/task/59432/fdinfo/5
    /proc/59432/fd/6
    /proc/59432/fdinfo/6
    [root@centos7 ~]# ls -lh /proc/kcore
    -r--------. 1 root root 128T Dec 24 22:00 /proc/kcore

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值