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

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


当前回答

如果你想要更多地控制目录列表的大小,你可以使用阈值(-t)开关,如下所示:

$ du -ht 1000000000 | sort --reverse

磁盘使用率 人可读的格式 T -阈值大小

在这里,我们想要列出大小大于1GB的所有目录。

$ du -ht 1G | sort --reverse

解释:

wiki中描述的单位如下:

K, M, G, T, P, E, Z, Y(1024的幂)或 KB, MB, GB, TB, PB, EB, ZB, YB(1000的幂)。

其他回答

这是我喜欢的

更新:我不喜欢前一个,因为它不显示当前目录下的文件,它只列出目录。

ubuntu上/var的输出示例:

sudo du -hDaxd1 /var | sort -h | tail -n10 .使用实例

4.0K    /var/lock
4.0K    /var/run
4.0K    /var/www
12K     /var/spool
3.7M    /var/backups
33M     /var/log
45M     /var/webmin
231M    /var/cache
1.4G    /var/lib
1.7G    /var

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

ndu (ncurses du)

这个很棒的CLI实用程序允许您轻松地交互式地查找大文件和目录(递归总大小)。

例如,我们在一个著名的开源项目的根目录中:

sudo apt install ncdu
ncdu

结果是:

然后,我在键盘上向下输入,进入/drivers文件夹,我看到:

Ncdu只在整个树启动时递归地计算文件大小,所以它是高效的。这样,当您在子目录内移动时,就不必重新计算大小,因为您试图确定磁盘占用是什么。

“总磁盘使用量”vs“表观大小”类似于du,我已经解释了它:为什么' du '的输出经常与' du -b '如此不同

项目主页:https://dev.yorhel.nl/ncdu

相关问题:

https://unix.stackexchange.com/questions/67806/how-to-recursively-find-the-amount-stored-in-directory/67808 https://unix.stackexchange.com/questions/125429/tracking-down-where-disk-space-has-gone-on-linux https://askubuntu.com/questions/57603/how-to-list-recursive-file-sizes-of-files-and-directories-in-a-directory https://serverfault.com/questions/43296/how-does-one-find-which-files-are-taking-up-80-of-the-space-on-a-linux-webserve

在Ubuntu 16.04中测试。

Ubuntu列表根目录

你可能想要:

ncdu --exclude-kernfs -x /

地点:

-x停止跨文件系统屏障 ——exclude-kernfs跳过特殊的文件系统,比如/sys

MacOS 10.15.5 list root

为了正确地在该系统上列出root /,我还需要——exclude-firmlinks,例如:

brew install ncdu
cd /
ncdu --exclude-firmlinks

否则,它似乎进入了一些链接无限循环,可能是因为:https://www.swiftforensics.com/2019/10/macos-1015-volumes-firmlink-magic.html

我们为了爱而学习的东西。

Ncdu非交互用法

ncdu的另一个很酷的特性是,您可以先以JSON格式转储大小,然后再重用它们。

例如,要生成文件,运行:

ncdu -o ncdu.json

然后用互动的方式检查它:

ncdu -f ncdu.json

如果您正在处理一个非常大而缓慢的文件系统,如NFS,这是非常有用的。

通过这种方式,您可以首先只导出一次,这可能需要几个小时,然后浏览文件、退出、再次浏览等等。

输出格式只是JSON,所以很容易在其他程序中重用它,例如:

ncdu -o -  | python -m json.tool | less

揭示了一个简单的目录树数据结构:

[
    1,
    0,
    {
        "progname": "ncdu",
        "progver": "1.12",
        "timestamp": 1562151680
    },
    [
        {
            "asize": 4096,
            "dev": 2065,
            "dsize": 4096,
            "ino": 9838037,
            "name": "/work/linux-kernel-module-cheat/submodules/linux"
        },
        {
            "asize": 1513,
            "dsize": 4096,
            "ino": 9856660,
            "name": "Kbuild"
        },
        [
            {
                "asize": 4096,
                "dsize": 4096,
                "ino": 10101519,
                "name": "net"
            },
            [
                {
                    "asize": 4096,
                    "dsize": 4096,
                    "ino": 11417591,
                    "name": "l2tp"
                },
                {
                    "asize": 48173,
                    "dsize": 49152,
                    "ino": 11418744,
                    "name": "l2tp_core.c"
                },

在Ubuntu 18.04中测试。

输入“ls -ltrh /path_to_directory”

只是一个警告,如果你想比较文件的大小。Du根据文件系统、块大小、... .产生不同的结果

可能会发生文件大小不同的情况,例如比较您的本地硬盘和USB大容量存储设备上的相同目录。我使用以下脚本,包括ls来计算目录大小。结果以字节为单位,将所有子目录都考虑在内。

echo "[GetFileSize.sh] target directory: \"$1\""

iRetValue=0

uiLength=$(expr length "$1")
if [ $uiLength -lt 2 ]; then
  echo "[GetFileSize.sh] invalid target directory: \"$1\" - exiting!"
  iRetValue=-1
else
  echo "[GetFileSize.sh] computing size of files..."

  # use ls to compute total size of all files - skip directories as they may
  # show different sizes, depending on block size of target disk / file system
  uiTotalSize=$(ls -l -R $1 | grep -v ^d | awk '{total+=$5;} END {print total;}')
  uiLength=$(expr length "$uiTotalSize")
  if [ $uiLength -lt 1 ]; then
    uiTotalSize=0
  fi
  echo -e "[GetFileSize.sh] total target file size: \"$uiTotalSize\""

fi

exit "$iRetValue"