是否有可能在Unix中使用ls列出子目录的总大小及其所有内容,而不是通常的4K(我假设)只是目录文件本身?

total 12K
drwxrwxr-x  6 *** *** 4.0K 2009-06-19 10:10 branches
drwxrwxr-x 13 *** *** 4.0K 2009-06-19 10:52 tags
drwxrwxr-x 16 *** *** 4.0K 2009-06-19 10:02 trunk

在翻遍了手册之后,我一无所获。


当前回答

输入“ls -ltrh /path_to_directory”

其他回答

看一下du命令

输入“ls -ltrh /path_to_directory”

有一段时间,我使用Nautilus(在RHEL 6.0的Gnome桌面上)来删除我的主文件夹上的文件,而不是使用bash中的rm命令。因此,总大小显示为

du -sh

每个子目录的磁盘使用量之和不匹配,当我使用

du -sh *

我花了一段时间才意识到Nautilus将删除的文件发送到它的垃圾文件夹,而这个文件夹并没有在du -sh *命令中列出。所以,我只是想分享这个,以防有人面临同样的问题。

要以ls -lh格式显示,请使用:

(du -sh ./*; ls -lh --color=no) | awk '{ if($1 == "total") {X = 1} else if (!X) {SIZES[$2] = $1} else { sub($5 "[ ]*", sprintf("%-7s ", SIZES["./" $9]), $0); print $0} }'

Awk代码解释:

if($1 == "total") { // Set X when start of ls is detected
  X = 1 
} else if (!X) { // Until X is set, collect the sizes from `du`
  SIZES[$2] = $1
} else {
  // Replace the size on current current line (with alignment)
  sub($5 "[ ]*", sprintf("%-7s ", SIZES["./" $9]), $0); 
  print $0
}

样例输出:

drwxr-xr-x 2 root     root 4.0K    Feb 12 16:43 cgi-bin
drwxrws--- 6 root     www  20M     Feb 18 11:07 document_root
drwxr-xr-x 3 root     root 1.3M    Feb 18 00:18 icons
drwxrwsr-x 2 localusr www  8.0K    Dec 27 01:23 passwd

从ls中只检索以字节为单位的大小。

ls -ltr | head -n1 | cut -d' ' -f2