是否有可能在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
在翻遍了手册之后,我一无所获。
是否有可能在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
在翻遍了手册之后,我一无所获。
当前回答
试着这样做:
du -sh *
简写版:
du --summarize --human-readable *
解释:
du:磁盘使用率
-s:显示每个指定文件的摘要。(相当于-d 0)
-h: "人类可读"输出。使用单位后缀:Byte, Kibibyte (KiB), Mebibyte (MiB), Gibibyte (GiB), Tebibyte (TiB)和Pebibyte (PiB)。(BASE2)
其他回答
使用实例递归显示当前目录的文件和子目录大小。
du -h .
要显示相同大小的信息,但不递归打印它们的子目录(可能是一个巨大的列表),只需使用——max-depth选项:
du -h --max-depth=1 .
du -sh * | sort -h
这将以人类可读的格式显示。
du -h --max-depth=1 . | sort -n -r
我遇到了一个类似于Martin Wilde描述的问题,在我的例子中,在使用rsync镜像后比较两个不同服务器上的相同目录。
我没有使用脚本,而是向du添加了-b标志,它以字节为单位计算大小,据我所能确定,消除了两台服务器上的差异。您仍然可以使用-s -h来得到一个可理解的输出。
要以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