是否有可能在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 -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 -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

看一下du命令

嗯,最好的方法是使用这个命令:

du -h -x / | sort -hr >> /home/log_size.txt

然后你就可以在你的服务器上获得所有大小的文件夹。轻松帮助您找到最大的尺寸。

我遇到了一个类似于Martin Wilde描述的问题,在我的例子中,在使用rsync镜像后比较两个不同服务器上的相同目录。

我没有使用脚本,而是向du添加了-b标志,它以字节为单位计算大小,据我所能确定,消除了两台服务器上的差异。您仍然可以使用-s -h来得到一个可理解的输出。

du -S

du还有另一个有用的选项:-s,——separate-dirs告诉du不包含子目录的大小——在某些情况下很方便。

例1 -仅显示目录中的文件大小:

du -Sh  * 
3,1G    10/CR2
280M    10

例2 -显示了目录下的文件大小和子目录:

du -h  * 
3,1G    10/CR2 
3,4G    10