查看当前磁盘使用情况、查找占用空间最大的文件文件夹是日常工作中常用的操作。
一般情况下通过df、du命令来完成,本文就来详细了解下这两个命令。
1.df命令一般情况下,我们不太了解该命令时直接先man一下,查看基本用法
root@7bc18553126f:/# man df
NAME
df - report file system disk space usage
SYNOPSIS
df [OPTION]... [FILE]...
DESCRIPTION
This manual page documents the GNU version of df. df displays the amount of disk space available on the file system containing each file name argument.
If no file name is given, the space available on all currently mounted file systems is shown. Disk space is shown in 1K blocks by default, unless the
environment variable POSIXLY_CORRECT is set, in which case 512-byte blocks are used.
If an argument is the absolute file name of a disk device node containing a mounted file system, df shows the space available on that file system rather
than on the file system containing the device node. This version of df cannot show the space available on unmounted file systems, because on most kinds
of systems doing so requires very nonportable intimate knowledge of file system structures.
OPTIONS
-a, --all
include pseudo, duplicate, inaccessible file systems
-B, --block-size=SIZE
scale sizes by SIZE before printing them; e.g., '-BM' prints sizes in units of 1,048,576 bytes; see SIZE format below
-h, --human-readable
print sizes in powers of 1024 (e.g., 1023M)
-H, --si
print sizes in powers of 1000 (e.g., 1.1G)
-i, --inodes
list inode information instead of block usage
-k like --block-size=1K
-l, --local
limit listing to local file systems
--no-sync
...
给的option比较多,但是真正好用的就两个
1.1 df -h高可读性展示结果值
root@7bc18553126f:/# df -h
Filesystem Size Used Avail Use% Mounted on
overlay 59G 14G 42G 26% /
tmpfs 64M 0 64M 0% /dev
shm 64M 0 64M 0% /dev/shm
/dev/vda1 59G 14G 42G 26% /etc/hosts
tmpfs 995M 0 995M 0% /sys/firmware
结果值也比较容易理解,FileSystem就是当前文件系统,我们重点关注下 User%这一项,如果使用率过高,那么就需要告警出来,避免磁盘被占满,影响程序运行。
1.2 df -i有些时候,使用df -h查看后,发现剩余空间还有不少,但是就是空间不足。
那这时候就要考虑下是不是inode占用了过多磁盘空间,导致不足的(inode也是会占用磁盘的)
root@7bc18553126f:/# df -ih
Filesystem Inodes IUsed IFree IUse% Mounted on
overlay 3.8M 397K 3.4M 11% /
tmpfs 249K 17 249K 1% /dev
shm 249K 1 249K 1% /dev/shm
/dev/vda1 3.8M 397K 3.4M 11% /etc/hosts
tmpfs 249K 1 249K 1% /sys/firmware
df -i 就是从inode的视角来展示磁盘占用
1.3 解决磁盘使用率100%的问题如果在我们的Linux机器上,发生了磁盘使用率100%的问题,那么需要从下面两个维度来解决:
* df -h 显示100%,那么这种一般就是有些大文件把磁盘给占满了,解决方案就是把无用的大文件删除即可;
* df -i 显示100%,这种一般是文件过多(一般是小文件过多),解决方案就是清理这些无用的小文件即可;
2. du命令同样通过man du命令来查看其基本用法,笔者不再贴出来
我们直接看用法
2.1 查看指定目录占用空间root@7bc18553126f:/tmp/tm2# du -sh /tmp/
1.7G /tmp/
/tmp文件夹总占用空间为1.7G
2.2 查看指定目录及其子目录占用空间root@7bc18553126f:/tmp/tm2# du -h /tmp/
201M /tmp/tm2/tm3
701M /tmp/tm2
1.7G /tmp/
这里会展示出/tmp文件夹下的所有子文件夹大小
2.3 查看指定目录、其子目录、文件占用空间root@7bc18553126f:/tmp/tm2# du -ha /tmp/
500M /tmp/tm2/tt.txt
200M /tmp/tm2/tm3/t.txt
201M /tmp/tm2/tm3
701M /tmp/tm2
0 /tmp/test.txt
1001M /tmp/file
1.7G /tmp/
相比较与2.2,这里还会展示目录中具体文件的大小
2.4 --max-depth用法这个--max-depth还是有点意思的,主要是指定具体目录深度,我们通过一个示例来展示
笔者在/tmp目录下建立了tm2目录,并在/tmp/tm2目录下建立了tm3目录,所以tm3目录的路径为/tmp/tm2/tm3
root@7bc18553126f:/tmp/tm2# du -h --max-depth=0 /tmp/
1.7G /tmp/
root@7bc18553126f:/tmp/tm2# du -h --max-depth=1 /tmp/
701M /tmp/tm2
1.7G /tmp/
root@7bc18553126f:/tmp/tm2# du -h --max-depth=2 /tmp/
201M /tmp/tm2/tm3
701M /tmp/tm2
1.7G /tmp/
结果很清晰,--max-depth=0就是指当前目录;--max-depth=1 就是在当前目录向下延伸1个子目录;--max-depth=2向下延伸2个子目录...
总结:df -h 快速展示当前磁盘使用情况;
df -ih 展示当前磁盘 inode使用情况;
du -h --max-depth=1 / 展示/根目录下 各子目录占用磁盘情况