居然这样获取 Linux 系统目录大小,我佛了~

系统 Linux
如何去获取到文件目录的实际大小呢?别急,Linux 下的 du 命令可以帮到你。

[[401577]]

大家好,我是良许。

使用过 Linux 系统的小伙伴都知道应该都知道, Linux 系统下的 ls 命令通常被我们用来查看文件目录的内容,但是不知道大家有没有细心留意,ls 命令所显示出来的每一个目录的大小竟然都只有 4 KB ?

  1. $ ls -lh | grep ^d 
  2.  
  3. drwxr-xr-x  3 alvin alvin 4.0K Aug  2 13:57 Bank_Details 
  4. drwxr-xr-x  2 alvin alvin 4.0K Mar 15  2019 alvin 
  5. drwxr-xr-x  6 alvin alvin 4.0K Feb 16  2019 drive-alvin 
  6. drwxr-xr-x 13 alvin alvin 4.0K Jan  6  2019 drive-mageshm 
  7. 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 命令的使用。

查看指定目录的总大小

  1. $ du -hs /home/alvin/Documents 
  2. or 
  3. $ du -h --max-depth=0 /home/alvin/Documents/ 
  4.  
  5. 20G    /home/alvin/Documents 

上面两种方式都可以算出目标目录的总大小,其中:

  • -h 表示显示成 K、M、G 等大小格式
  • -s 表示显示总和
  • --max-depth=N 表示目录级别为 N

查看指定目录下每个目录(包括子目录)的大小

将 -s 选项去掉,不进行统计总和,就能显示每一个目录的大小了,以下仅显示前 20 条:

  1. $ du -h /home/alvin/Documents/ | sort -rh | head -20 
  2.  
  3. 20G    /home/alvin/Documents/ 
  4. 9.6G    /home/alvin/Documents/drive-alvin 
  5. 6.3G    /home/alvin/Documents/Thanu_Photos 
  6. 5.3G    /home/alvin/Documents/Thanu_Photos/Camera 
  7. 5.3G    /home/alvin/Documents/drive-alvin/Thanu-videos 
  8. 3.2G    /home/alvin/Documents/drive-mageshm 
  9. 2.3G    /home/alvin/Documents/drive-alvin/Thanu-Photos 
  10. 2.2G    /home/alvin/Documents/drive-alvin/Thanu-photos-by-month 
  11. 916M    /home/alvin/Documents/drive-mageshm/Tanisha 
  12. 454M    /home/alvin/Documents/drive-mageshm/2g-backup 
  13. 415M    /home/alvin/Documents/Thanu_Photos/WhatsApp Video 
  14. 300M    /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Jan-2017 
  15. 288M    /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Oct-2017 
  16. 226M    /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Sep-2017 
  17. 219M    /home/alvin/Documents/Thanu_Photos/WhatsApp Documents 
  18. 213M    /home/alvin/Documents/drive-mageshm/photos 
  19. 163M    /home/alvin/Documents/Thanu_Photos/WhatsApp Video/Sent 
  20. 161M    /home/alvin/Documents/Thanu_Photos/WhatsApp Images 
  21. 154M    /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/June-2017 
  22. 150M    /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Nov-2016 

查看指定目录下每个文件和目录的大小

结合通配符 * 的使用,可以实现显示出指定目录下的文件和目录的大小信息的效果:

  1. $ du -hs /home/alvin/Documents/* | sort -rh | head -10 
  2.  
  3. 9.6G    /home/alvin/Documents/drive-alvin 
  4. 6.3G    /home/alvin/Documents/Thanu_Photos 
  5. 3.2G    /home/alvin/Documents/drive-mageshm 
  6. 756K    /home/alvin/Documents/Bank_Details 
  7. 272K    /home/alvin/Documents/user-friendly-zorin-os-15-has-been-released-TouchInterface1.png 
  8. 172K    /home/alvin/Documents/user-friendly-zorin-os-15-has-been-released-NightLight.png 
  9. 164K    /home/alvin/Documents/ConfigServer Security and Firewall (csf) Cheat Sheet.pdf 
  10. 132K    /home/alvin/Documents/user-friendly-zorin-os-15-has-been-released-Todo.png 
  11. 112K    /home/alvin/Documents/user-friendly-zorin-os-15-has-been-released-ZorinAutoTheme.png 
  12. 96K    /home/alvin/Documents/distro-info.xlsx 

查看指定目录下的每个目录的大小(排除子目录)

使用 -S 选项,以排除子目录的形式来统计大小:

  1. $ du -hS /home/alvin/Documents/ | sort -rh | head -20 
  2.  
  3. 5.3G    /home/alvin/Documents/Thanu_Photos/Camera 
  4. 5.3G    /home/alvin/Documents/drive-alvin/Thanu-videos 
  5. 2.3G    /home/alvin/Documents/drive-alvin/Thanu-Photos 
  6. 1.5G    /home/alvin/Documents/drive-mageshm 
  7. 831M    /home/alvin/Documents/drive-mageshm/Tanisha 
  8. 454M    /home/alvin/Documents/drive-mageshm/2g-backup  
  9. 300M    /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Jan-2017 
  10. 288M    /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Oct-2017 
  11. 253M    /home/alvin/Documents/Thanu_Photos/WhatsApp Video 
  12. 226M    /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Sep-2017 
  13. 219M    /home/alvin/Documents/Thanu_Photos/WhatsApp Documents 
  14. 213M    /home/alvin/Documents/drive-mageshm/photos 
  15. 163M    /home/alvin/Documents/Thanu_Photos/WhatsApp Video/Sent 
  16. 154M    /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/June-2017 
  17. 150M    /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Nov-2016 
  18. 127M    /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Dec-2016 
  19. 100M    /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Oct-2016 
  20. 94M    /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Nov-2017 
  21. 92M    /home/alvin/Documents/Thanu_Photos/WhatsApp Images 
  22. 90M    /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Dec-2017 

查看指定目录中的一级子目录的大小

这个就很简单了,将 max-depth 设定为 1 就可以了:

  1. $ du -h --max-depth=1 /home/alvin/Documents/ 
  2.  
  3. 3.2G    /home/alvin/Documents/drive-mageshm 
  4. 4.0K    /home/alvin/Documents/alvin 
  5. 756K    /home/alvin/Documents/Bank_Details 
  6. 9.6G    /home/alvin/Documents/drive-alvin 
  7. 6.3G    /home/alvin/Documents/Thanu_Photos 
  8. 20G    /home/alvin/Documents/ 

查看指定目录的大小(包含统计功能)

使用 -c 选项能够对结果进行统计,可以看到,指定目录下的文件和目录的大小的总和是 20 G,这和上面第一条命令中所得出的 /home/alvin/Documents/ 的大小 20 G 是一致的:

  1. $ du -hsc /home/alvin/Documents/* | sort -rh | head -10 
  2.  
  3. 20G    total 
  4. 9.6G    /home/alvin/Documents/drive-alvin 
  5. 6.3G    /home/alvin/Documents/Thanu_Photos 
  6. 3.2G    /home/alvin/Documents/drive-mageshm 
  7. 756K    /home/alvin/Documents/Bank_Details 
  8. 272K    /home/alvin/Documents/user-friendly-zorin-os-15-has-been-released-TouchInterface1.png 
  9. 172K    /home/alvin/Documents/user-friendly-zorin-os-15-has-been-released-NightLight.png 
  10. 164K    /home/alvin/Documents/ConfigServer Security and Firewall (csf) Cheat Sheet.pdf 
  11. 132K    /home/alvin/Documents/user-friendly-zorin-os-15-has-been-released-Todo.png 
  12. 112K    /home/alvin/Documents/user-friendly-zorin-os-15-has-been-released-ZorinAutoTheme.png 

本文授权转载自公众号「良许Linux」。良许,世界500强外企Linux开发工程师,公众号里分享大量Linux干货,欢迎关注!

 

责任编辑:赵宁宁 来源: 良许Linux
相关推荐

2022-11-16 08:33:29

监听Spring监听器

2020-08-06 09:03:44

Go语言获取文件代码

2022-04-11 08:20:36

编程辅助工具GitHubCopilot

2017-04-07 10:00:11

Linux目录结构分区大小

2021-11-30 09:14:02

Linux目录命令

2017-07-14 15:20:43

Linux文件系统目录结构

2023-12-12 07:18:47

Linux系统虚拟化

2015-10-23 15:49:55

程序员加薪升职

2015-10-26 09:10:38

程序员加薪新方法

2021-08-03 22:26:46

Go函数分页

2022-07-20 23:08:55

互联网业务EDAC设备故障

2009-12-11 10:34:15

Linux获取文件大小

2016-11-21 21:28:51

根目录日志代码

2009-05-31 16:19:51

LinuxWin结构

2009-03-18 08:50:59

LinuxWindows系统目录

2009-04-15 20:32:39

Linux工作目录用户主目录

2021-10-27 13:51:54

Windows XP微软系统

2022-07-18 05:59:18

Linux目录结构操作系统

2014-04-10 15:42:08

Linux系统内存

2014-07-04 09:41:11

微软Win8
点赞
收藏

51CTO技术栈公众号