我怎样才能得到这样的Linux系统的目录大小?用过linux系统的朋友应该都知道,linux系统下的ls命令通常是我们用来查看文件目录内容的,但是不知道大家有没有注意过显示的每个目录的大小thelscommand竟然都只有4KB?$ls-lh|grep^ddrwxr-xr-x3alvinalvin4.0KAug213:57Bank_Detailsdrwxr-xr-x2alvinalvin4.0KMar152019alvindrwxr-xr-x6alvinalvin4.0KFeb162019drive-alvindrwxr-xr-x13alvinalvin4.0KJan62019drive-mageshmdrwxr-xr-x15alvinalvin4.0KSep2921:32Thanu_Photos实际上,这是因为在Linux下一切都是文件。这句话相信大家都听过,所以大家看到的4KB只是用来存放目录元数据的文件大小,并不是我们通常意义上的目录大小。那么问题来了,如何获取文件目录的实际大小呢?别着急,Linux下的du命令可以帮到你。du是英文diskusage的缩写,代表diskusage。它是一个标准的Unix程序,用于估计当前工作目录中文件空间的使用情况。下面以/home/alvin/Documents为例介绍du命令的使用。查看指定目录的总大小$du-hs/home/alvin/Documents或者$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-2020G/home/alvin/Documents/9.6G/home/alvin/Documents/drive-alvin6.3G/home/alvin/Documents/Thanu_Photos5.3G/home/alvin/Documents/Thanu_Photos/Camera5.3G/home/alvin/Documents/drive-alvin/Thanu-videos3.2G/home/alvin/Documents/drive-mageshm2.3G/主页/alvin/Documents/drive-alvin/Thanu-Photos2.2G/home/alvin/Documents/drive-alvin/Thanu-photos-by-month916M/home/alvin/Documents/drive-mageshm/Tanisha454M/home/alvin/文档/drive-mageshm/2g-backup415M/home/alvin/Documents/Thanu_Photos/WhatsAppVideo300M/home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Jan-2017288M/home/alvin/Documents/drive-alvin/Thanu-照片按月/Oct-2017226M/home/alvin/Documents/drive-alv在/Thanu-照片按月/Sep-2017219M/home/alvin/Documents/Thanu_Photos/WhatsAppDocuments213M/home/alvin/Documents/drive-mageshm/photos163M/home/alvin/Documents/Thanu_Photos/WhatsAppVideo/Sent161M/home/alvin/Documents/Thanu_Photos/WhatsAppImages154M/home/alvin/Documents/drive-alvin/Thanu-photos-by-month/June-2017150M/home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Nov-2016查看指定目录下每个文件和目录的大小,使用通配符*实现显示指定目录下文件和目录大小信息的效果:$du-hs/home/alvin/Documents/*|sort-rh|head-109.6G/home/alvin/Documents/drive-alvin6.3G/home/alvin/Documents/Thanu_Photos3.2G/home/alvin/Documents/drive-mageshm756K/home/alvin/Documents/Bank_Details272K/home/alvin/Documents/user-friendly-zorin-os-15-has-been-released-TouchInterface1.png172K/home/alvin/Documents/user-friendly-zorin-os-15-has-been-released-NightLight.png164K/home/alvin/文档/配置ServerSecurityandFirewall(csf)CheatSheet.pdf132K/home/alvin/Documents/user-friendly-zorin-os-15-has-been-released-Todo.png112K/home/alvin/Documents/user-friendly-zorin-os-15-has-been-released-ZorinAutoTheme.png96K/home/alvin/Documents/distro-info.xlsx查看指定目录下各目录大小(排除子目录)使用-S选项以排除子目录的形式统计大小:$du-hS/home/alvin/Documents/|sort-rh|head-205.3G/home/alvin/Documents/Thanu_Photos/Camera5。3G/home/alvin/Documents/drive-alvin/Thanu-videos2.3G/home/alvin/Documents/drive-alvin/Thanu-Photos1.5G/home/alvin/Documents/drive-mageshm831M/home/alvin/Documents/drive-mageshm/Tanisha454M/home/alvin/Documents/drive-mageshm/2g-backup300M/home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Jan-2017288M/home/alvin/Documents/drive-alvin/Thanu-照片按月/Oct-2017253M/home/alvin/Documents/Thanu_Photos/WhatsAppVideo226M/home/alvin/Documents/drive-alvin/Thanu-照片按月/Sep-2017219M/home/alvin/文档/Thanu_Photos/WhatsAppDocuments213M/home/alvin/Documents/drive-mageshm/photos163M/home/alvin/Documents/Thanu_Photos/WhatsAppVideo/Sent154M/home/alvin/Documents/drive-mageshm/Thanu-照片按月/June-2017150M/home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Nov-2016127M/home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Dec-2016100M/home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Oct-201694M/home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Nov-201792M/home/alvin/Documents/Thanu_Photos/WhatsAppImages90M/home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Dec-2017查看指定目录下一级子目录的大小。这很简单。设置max-depth设置为1:$du-h--max-depth=1/home/alvin/Documents/3.2G/home/alvin/Documents/drive-mageshm4.0K/home/alvin/Documents/alvin756K/home/alvin/Documents/Bank_Details9.6G/home/alvin/Documents/drive-alvin6.3G/home/alvin/Documents/Thanu_Photos20G/home/alvin/Documents/查看指定目录的大小(包括统计功能)使用-c选项统计结果显示指定目录下的文件和目录总大小为20G,与上面第一条命令得到的/home/alvin/Documents/的20G大小一致:$du-hsc/home/alvin/Documents/*|sort-rh|head-1020Gtotal9.6G/home/alvin/Documents/drive-alvin6.3G/home/alvin/Documents/Thanu_Photos3.2G/home/alvin/Documents/drive-mageshm756K/home/alvin/Documents/Bank_Details272K/home/alvin/Documents/user-friendly-zorin-os-15-has-been-released-TouchInterface1.png172K/home/alvin/Documents/user-friendly-zorin-os-15-has-been-released-NightLight.png164K/home/alvin/Documents/ConfigServerSecurityandFirewall(csf)CheatSheet.pdf132K/home/alvin/Documents/user-friendly-zorin-os-15-has-been-released-Todo.png112K/home/alvin/Documents/user-friendly-zorin-os-15-has-been-released-ZorinAutoTheme.png本文授权转载自公众号《良序Linux》世界500强外企Linux开发工程师良旭,在公众号分享大量Linux干货,欢迎关注!
