Linux "du"(磁盘使用)命令是一个标准的Unix/Linux命令,用于检测机器上文件和目录的磁盘使用信息。du命令有很多参数选项,它们能够用于用很多格式获取结果。du命令也用递归方式显示文件和目录尺寸。
1、要查找/home/blctrl目录树和其每个子目录中磁盘使用概要。输入命令:
- [root@rockygu blctrl]# du /home/blctrl/
- 4 /home/blctrl/EPICS/TEST/testIoc/configure/O.linux-x86_64
- ...
- 5052 /home/blctrl/EPICS
- 12 /home/blctrl/.dbus/session-bus
- 12 /home/blctrl/.dbus
- 4 /home/blctrl/.ssh
- 0 /home/blctrl/Rocky
- 0 /home/blctrl/rocky
- 28 /home/blctrl/.config/pulse
- 28 /home/blctrl/.config
- 25844 /home/blctrl/
以上命令的输出显示了/home/blctrl和其子目录中磁盘块数目。
2、使用"-h"选项的"du"命令提供了"人可阅读格式"的结果。这表示你可以看到以字节,千字节,兆字节,吉字节为尺寸的结果。
- [root@rockygu blctrl]# du -h /home/blctrl
- ...
- 12K /home/blctrl/.dbus/session-bus
- 12K /home/blctrl/.dbus
- 4.0K /home/blctrl/.ssh
- 0 /home/blctrl/Rocky
- 0 /home/blctrl/rocky
- 28K /home/blctrl/.config/pulse
- 28K /home/blctrl/.config
- 26M /home/blctrl
3、要获取一个目录的整个磁盘使用尺寸的概要,使用"-s"选项。
- [root@rockygu blctrl]# du -sh /home/blctrl
- 26M /home/blctrl
4、"du"命令使用"-a"标记显示所有文件和目录的磁盘使用。
- [root@rockygu blctrl]# du -ah /home/blctrl
- 4.0K /home/blctrl/.bash_logout
- 4.0K /home/blctrl/.bash_profile
- 4.0K /home/blctrl/.bashrc
- 4.0K /home/blctrl/EPICS/test.db
- ...
- 4.0K /home/blctrl/.Xauthority
- 26M /home/blctrl
5、用千字节块查找一棵目录树以及子树的磁盘使用。使用"-k":
- [root@rockygu blctrl]# du -k /home/blctrl
- 4 /home/blctrl/EPICS/TEST/testIoc/configure/O.linux-x86_64
- 0 /home/blctrl/EPICS/TEST/testIoc/configure/O.Common
- 36 /home/blctrl/EPICS/TEST/testIoc/configure
- ...
- 25844 /home/blctrl
6、用兆字节块查找一棵目录树以及子树的磁盘使用。使用"-m":
- [root@rockygu blctrl]# du -mh /home/blctrl
- 4.0K /home/blctrl/EPICS/TEST/testIoc/configure/O.linux-x86_64
- 0 /home/blctrl/EPICS/TEST/testIoc/configure/O.Common
- 36K /home/blctrl/EPICS/TEST/testIoc/configure
- ...
- 28K /home/blctrl/.config/pulse
- 28K /home/blctrl/.config
- 26M /home/blctrl
7、"-c"标记在末行提供一个总使用磁盘空间。
- [root@rockygu blctrl]# du -ch /home/blctrl
- 4.0K /home/blctrl/EPICS/TEST/testIoc/configure/O.linux-x86_64
- ...
- 28K /home/blctrl/.config
- 26M /home/blctrl
- 26M total
8、以下命令计算和现实所有文件和目录的磁盘使用,但排除匹配指定模式的文件。以下命令在计算一个目录的整个尺寸时排除了".txt"文件。通过使用"--exclude"标记,你能够排除任何文件格式。
- [root@rockygu blctrl]# du -ah --exclude="*.txt" /home/blctrl
- 4.0K /home/blctrl/.bash_logout
- 4.0K /home/blctrl/.bash_profile
- 4.0K /home/blctrl/.bashrc
- ...
- 26M /home/blctrl
9、根据修改时间显示磁盘使用,使用标记"--time"。
- [root@rockygu blctrl]# du -ah --time /home/blctrl
- 4.0K 2021-07-27 22:21 /home/blctrl/.bash_logout
- 4.0K 2021-07-27 22:21 /home/blctrl/.bash_profile
- 4.0K 2021-07-27 22:21 /home/blctrl/.bashrc
- ...
- 4.0K 2022-06-12 17:29 /home/blctrl/EPICS/TEST/testIoc/testIocApp/Makefile
- 4.0K 2022-06-12 17:29 /home/blctrl/EPICS/TEST/testIoc/testIocApp/src/Makefile
- 4.0K 2022-06-12 17:29 /home/blctrl/EPICS/TEST/testIoc/testIocApp/src/devtestIocVers
10、运行以下命令查找在/home目录下最大的目录。
- [root@rockygu blctrl]# du -a /home/ | sort -n -r | head -n 5
- 25844 /home/blctrl
- 25844 /home/
- 20676 /home/blctrl/wordpress.tar.gz
- 5052 /home/blctrl/EPICS
- 2920 /home/blctrl/EPICS/IOCs
以上命令显示/home目录中最大的5个目录。
如果你想要显示在当前工作目录中最大目录,运行:
- [root@rockygu blctrl]# du -a | sort -n -r | head -n 5
- 25844 .
- 20676 ./wordpress.tar.gz
- 5052 ./EPICS
- 2920 ./EPICS/IOCs
- 2760 ./EPICS/IOCs/sampleIOC
分解以上命令并且看每个参数说了什么。
想要以人阅读的格式显示以上结果,即:你想要以KB,MB或GB显示最大文件。
- [root@rockygu blctrl]# du -hs * | sort -rh | head -5
- 21M wordpress.tar.gz
- 5.0M EPICS
- 4.0K test
- 4.0K services.txt
- 4.0K rocky.txt
以上命令将显示最上层目录,它们消耗过更多磁盘空间。如果你觉得某些目录不重要,你可以删除一些子目录或者删除整个文件夹来释放一些空间。
要显示包括子目录的最大文件夹/文件,运行:
- [root@rockygu blctrl]# du -Sh | sort -rh | head -5
- 21M .
- 860K ./EPICS/IOCs/sampleIOC/sampleIOCApp/src/O.linux-x86_64
- 648K ./EPICS/TEST/testIoc/testIocApp/src/O.linux-x86_64
- 636K ./EPICS/IOCs/sampleIOC/sampleIOCApp/src/O.Common
- 620K ./EPICS/IOCs/sampleIOC/dbd
在以上命令中每个选项的含义:
如果你只想要显示最大文件,则运行以下命令:
- [root@rockygu blctrl]# find -type f -exec du -Sh {} + | sort -rh | head -5
- 21M ./wordpress.tar.gz
- 612K ./EPICS/IOCs/sampleIOC/sampleIOCApp/src/O.Common/sampleIOC.dbd
- 612K ./EPICS/IOCs/sampleIOC/dbd/sampleIOC.dbd
- 456K ./EPICS/TEST/testIoc/testIocApp/src/O.Common/testIoc.dbd
- 456K ./EPICS/TEST/testIoc/dbd/testIoc.dbd
要查找一个特定位置的最大文件,只要在find命令中包含这个路径:
- [root@rockygu blctrl]# find /home/blctrl/ -type f -exec du -Sh {} + | sort -rh | head -5
- 21M /home/blctrl/wordpress.tar.gz
- 612K /home/blctrl/EPICS/IOCs/sampleIOC/sampleIOCApp/src/O.Common/sampleIOC.dbd
- 612K /home/blctrl/EPICS/IOCs/sampleIOC/dbd/sampleIOC.dbd
- 456K /home/blctrl/EPICS/TEST/testIoc/testIocApp/src/O.Common/testIoc.dbd
- 456K /home/blctrl/EPICS/TEST/testIoc/dbd/testIoc.dbd