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

其他回答

使用实例递归显示当前目录的文件和子目录大小。

du -h .

要显示相同大小的信息,但不递归打印它们的子目录(可能是一个巨大的列表),只需使用——max-depth选项:

du -h --max-depth=1 .

Sudo du -hsx 2>/dev/null * .使用实例

0       bin
4.0K    boot
0       dev
9.0M    etc
6.5M    home
824K    init
0       lib
0       lib32
0       lib64
0       libx32
16K     lost+found
4.0K    media
4.0K    mnt
4.0K    opt
0       proc
61M     root
36K     run
0       sbin
4.0K    srv
0       sys
4.0K    tmp
2.2G    usr
4.9G    var

试着这样做:

du -sh *

简写版:

du --summarize --human-readable *

解释:

du:磁盘使用率

-s:显示每个指定文件的摘要。(相当于-d 0)

-h: "人类可读"输出。使用单位后缀:Byte, Kibibyte (KiB), Mebibyte (MiB), Gibibyte (GiB), Tebibyte (TiB)和Pebibyte (PiB)。(BASE2)

要以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

把这个shell函数声明放在你的shell初始化脚本中:

function duls {
    paste <( du -hs -- "$@" | cut -f1 ) <( ls -ld -- "$@" )
}

我称它为duls,因为它显示了du和ls的输出(按此顺序):

$ duls
210M    drwxr-xr-x  21 kk  staff  714 Jun 15 09:32 .

$ duls *
 36K    -rw-r--r--   1 kk  staff    35147 Jun  9 16:03 COPYING
8.0K    -rw-r--r--   1 kk  staff     6962 Jun  9 16:03 INSTALL
 28K    -rw-r--r--   1 kk  staff    24816 Jun 10 13:26 Makefile
4.0K    -rw-r--r--   1 kk  staff       75 Jun  9 16:03 Makefile.am
 24K    -rw-r--r--   1 kk  staff    24473 Jun 10 13:26 Makefile.in
4.0K    -rw-r--r--   1 kk  staff     1689 Jun  9 16:03 README
120K    -rw-r--r--   1 kk  staff   121585 Jun 10 13:26 aclocal.m4
684K    drwxr-xr-x   7 kk  staff      238 Jun 10 13:26 autom4te.cache
128K    drwxr-xr-x   8 kk  staff      272 Jun  9 16:03 build
 60K    -rw-r--r--   1 kk  staff    60083 Jun 10 13:26 config.log
 36K    -rwxr-xr-x   1 kk  staff    34716 Jun 10 13:26 config.status
264K    -rwxr-xr-x   1 kk  staff   266637 Jun 10 13:26 configure
8.0K    -rw-r--r--   1 kk  staff     4280 Jun 10 13:25 configure.ac
7.0M    drwxr-xr-x   8 kk  staff      272 Jun 10 13:26 doc
2.3M    drwxr-xr-x  28 kk  staff      952 Jun 10 13:26 examples
6.2M    -rw-r--r--   1 kk  staff  6505797 Jun 15 09:32 mrbayes-3.2.7-dev.tar.gz
 11M    drwxr-xr-x  42 kk  staff     1428 Jun 10 13:26 src

$ duls doc
7.0M    drwxr-xr-x  8 kk  staff  272 Jun 10 13:26 doc

$ duls [bM]*
 28K    -rw-r--r--  1 kk  staff  24816 Jun 10 13:26 Makefile
4.0K    -rw-r--r--  1 kk  staff     75 Jun  9 16:03 Makefile.am
 24K    -rw-r--r--  1 kk  staff  24473 Jun 10 13:26 Makefile.in
128K    drwxr-xr-x  8 kk  staff    272 Jun  9 16:03 build

解释:

粘贴实用程序根据您给它的规范从它的输入创建列。给定两个输入文件,它将它们并排放置,并使用制表符作为分隔符。

我们将du -hs——"$@" | cut -f1的输出作为第一个文件(实际上是输入流),ls -ld——"$@"的输出作为第二个文件。

在函数中,"$@"将计算为所有命令行参数的列表,每个参数都用双引号括起来。因此,它将理解通配符和带有空格的路径名等。

两个减号(——)表示du和ls命令行选项的结束。如果没有这些,说duls -l会混淆du,而ls没有的du选项也会混淆ls(并且两个实用程序中存在的选项可能意味着不同的东西,这将是相当混乱的)。

du之后的切只是切掉了du -hs输出的第一列(大小)。

我决定将du输出放在左边,否则我将不得不管理一个不稳定的右列(由于文件名的长度不同)。

该命令将不接受命令行标志。

这已经在bash和ksh93中进行了测试。它不能在/bin/sh上工作。