Linux基础:系统目录大小如何通过du命令获得
使用过 Linux 系统的小火伴都知道应当都知道, Linux 系统下的 ls 命令通常被我们用来查看文件目录的内容,但是不知道大家有无仔细留意,ls 命令所显示出来的每个目录的大小居然都只有 4 KB ?
$ ls -lh | grep ^d drwxr-xr-x 3 alvin alvin 4.0K Aug 2 13:57 Bank_Details drwxr-xr-x 2 alvin alvin 4.0K Mar 15 2019 alvin drwxr-xr-x 6 alvin alvin 4.0K Feb 16 2019 drive-alvin drwxr-xr-x 13 alvin alvin 4.0K Jan 6 2019 drive-mageshm drwxr-xr-x 15 alvin alvin 4.0K Sep 29 21:32 Thanu_Photos
其实啊,这是由于 Linux 下一切皆文件,相信大家都听过这句话,所以你看到的 4 KB 只是用于存储目录元信息的文件大小而已,其实不是我们通常意义上的目录大小。那末问题来了,如何去获得到文件目录的实际大小呢?别急,Linux 下的 du 命令可以帮到你。
du 是英文 disk usage 的缩写,代表磁盘使用率,它是一个标准的 Unix 程序,用于估算当前工作目录中文件空间的使用情况。
下面将以 /home/alvin/Documents 为例,来介绍 du 命令的使用。
查看指定目录的总大小
$ du -hs /home/alvin/Documents or $ du -h --max-depth=0 /home/alvin/Documents/ 20G /home/alvin/Documents
上面两种方式都可以算出目标目录的总大小,其中:
- -h 表示显示成 K、M、G 等大小格式
- -s 表示显示总和
- –max-depth=N 表示目录级别为 N
查看指定目录下每一个目录(包括子目录)的大小
将 -s 选项去掉,不进行统计总和,就可以显示每个目录的大小了,以下仅显示前 20 条:
$ du -h /home/alvin/Documents/ | sort -rh | head ⑵0 20G /home/alvin/Documents/ 9.6G /home/alvin/Documents/drive-alvin 6.3G /home/alvin/Documents/Thanu_Photos 5.3G /home/alvin/Documents/Thanu_Photos/Camera 5.3G /home/alvin/Documents/drive-alvin/Thanu-videos 3.2G /home/alvin/Documents/drive-mageshm 2.3G /home/alvin/Documents/drive-alvin/Thanu-Photos 2.2G /home/alvin/Documents/drive-alvin/Thanu-photos-by-month 916M /home/alvin/Documents/drive-mageshm/Tanisha 454M /home/alvin/Documents/drive-mageshm/2g-backup 415M /home/alvin/Documents/Thanu_Photos/WhatsApp Video 300M /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Jan⑵017 288M /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Oct⑵017 226M /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Sep⑵017 219M /home/alvin/Documents/Thanu_Photos/WhatsApp Documents 213M /home/alvin/Documents/drive-mageshm/photos 163M /home/alvin/Documents/Thanu_Photos/WhatsApp Video/Sent 161M /home/alvin/Documents/Thanu_Photos/WhatsApp Images 154M /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/June⑵017 150M /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Nov⑵016
查看指定目录下每一个文件和目录的大小
结合通配符 * 的使用,可以实现显示出指定目录下的文件和目录的大小信息的效果:
$ du -hs /home/alvin/Documents/* | sort -rh | head ⑴0 9.6G /home/alvin/Documents/drive-alvin 6.3G /home/alvin/Documents/Thanu_Photos 3.2G /home/alvin/Documents/drive-mageshm 756K /home/alvin/Documents/Bank_Details 272K /home/alvin/Documents/user-friendly-zorin-os⑴5-has-been-released-TouchInterface1.png 172K /home/alvin/Documents/user-friendly-zorin-os⑴5-has-been-released-NightLight.png 164K /home/alvin/Documents/ConfigServer Security and Firewall (csf) Cheat Sheet.pdf 132K /home/alvin/Documents/user-friendly-zorin-os⑴5-has-been-released-Todo.png 112K /home/alvin/Documents/user-friendly-zorin-os⑴5-has-been-released-ZorinAutoTheme.png 96K /home/alvin/Documents/distro-info.xlsx
查看指定目录下的每一个目录的大小(排除子目录)
使用 -S 选项,以排除子目录的情势来统计大小:
$ du -hS /home/alvin/Documents/ | sort -rh | head ⑵0 5.3G /home/alvin/Documents/Thanu_Photos/Camera 5.3G /home/alvin/Documents/drive-alvin/Thanu-videos 2.3G /home/alvin/Documents/drive-alvin/Thanu-Photos 1.5G /home/alvin/Documents/drive-mageshm 831M /home/alvin/Documents/drive-mageshm/Tanisha 454M /home/alvin/Documents/drive-mageshm/2g-backup 300M /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Jan⑵017 288M /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Oct⑵017 253M /home/alvin/Documents/Thanu_Photos/WhatsApp Video 226M /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Sep⑵017 219M /home/alvin/Documents/Thanu_Photos/WhatsApp Documents 213M /home/alvin/Documents/drive-mageshm/photos 163M /home/alvin/Documents/Thanu_Photos/WhatsApp Video/Sent 154M /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/June⑵017 150M /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Nov⑵016 127M /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Dec⑵016 100M /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Oct⑵016 94M /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Nov⑵017 92M /home/alvin/Documents/Thanu_Photos/WhatsApp Images 90M /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Dec⑵017
查看指定目录中的一级子目录的大小
这个就很简单了,将 max-depth 设定为 1 就能够了:
$ du -h --max-depth=1 /home/alvin/Documents/ 3.2G /home/alvin/Documents/drive-mageshm 4.0K /home/alvin/Documents/alvin 756K /home/alvin/Documents/Bank_Details 9.6G /home/alvin/Documents/drive-alvin 6.3G /home/alvin/Documents/Thanu_Photos 20G /home/alvin/Documents/
查看指定目录的大小(包括统计功能)
使用 -c 选项能够对结果进行统计,可以看到,指定目录下的文件和目录的大小的总和是 20 G,这和上面第一条命令中所得出的 /home/alvin/Documents/ 的大小 20 G 是一致的:
$ du -hsc /home/alvin/Documents/* | sort -rh | head ⑴0 20G total 9.6G /home/alvin/Documents/drive-alvin 6.3G /home/alvin/Documents/Thanu_Photos 3.2G /home/alvin/Documents/drive-mageshm 756K /home/alvin/Documents/Bank_Details 272K /home/alvin/Documents/user-friendly-zorin-os⑴5-has-been-released-TouchInterface1.png 172K /home/alvin/Documents/user-friendly-zorin-os⑴5-has-been-released-NightLight.png 164K /home/alvin/Documents/ConfigServer Security and Firewall (csf) Cheat Sheet.pdf 132K /home/alvin/Documents/user-friendly-zorin-os⑴5-has-been-released-Todo.png 112K /home/alvin/Documents/user-friendly-zorin-os⑴5-has-been-released-ZorinAutoTheme.png
到此这篇关于Linux基础:系统目录大小如何通过du命令获得的文章就介绍到这了,更多相关用du命令获得Linux系统目录大小内容请搜索之前的文章或继续浏览下面的相关文章希望大家以后多多支持!
文章来源:丸子建站
文章标题:Linux基础:系统目录大小如何通过du命令获得
https://www.wanzijz.com/view/52869.html