du 仅用于目录

fog*_*rit 74 shell du

在 C-Shell 中,如何获得与du -sh ./*不列出根目录中的文件相同的输出,即仅列出 ./ 中的子目录及其所有内容的大小?

小智 130

添加尾部斜杠,例如:

du -sh ./*/
Run Code Online (Sandbox Code Playgroud)

  • 这是我遇到过的最快的 Stack Exchange 修复之一。如果可以的话+2。 (2认同)

Dee*_*ale 17

上面的重复答案只是添加了排序和标志以以人类可读的格式显示大小

du -sh */ | sort -hr
Run Code Online (Sandbox Code Playgroud)

输出:

44G     workspace/
24G     Downloads/
6.2G    Videos/
1.5G    Pictures/
189M    Music/
12M     Documents/
8.0K    Postman/
8.0K    Desktop/
Run Code Online (Sandbox Code Playgroud)

您可能还想添加一个阈值

du -sh */ -t 100M | sort -hr
Run Code Online (Sandbox Code Playgroud)

输出:

44G     workspace/
24G     Downloads/
6.2G    Videos/
1.5G    Pictures/
189M    Music/
Run Code Online (Sandbox Code Playgroud)

对于手册页dusort

DU(1)                                                                                        

NAME
       du - estimate file space usage

SYNOPSIS
       du [OPTION]... [FILE]...
       du [OPTION]... --files0-from=F

DESCRIPTION
       Summarize disk usage of the set of FILEs, recursively for directories.

       -h, --human-readable
              print sizes in human readable format (e.g., 1K 234M 2G)

       -s, --summarize
              display only a total for each argument

       -t, --threshold=SIZE
          exclude entries smaller than SIZE if positive, or entries greater than SIZE if negative


SORT(1)                                                                                          

NAME
       sort - sort lines of text files

SYNOPSIS
       sort [OPTION]... [FILE]...
       sort [OPTION]... --files0-from=F

DESCRIPTION
       Write sorted concatenation of all FILE(s) to standard output.

       -h, --human-numeric-sort
              compare human readable numbers (e.g., 2K 1G)

       -r, --reverse
              reverse the result of comparisons
Run Code Online (Sandbox Code Playgroud)

  • 请注意使用`| sort -hr` 输出将被缓冲,而 `df` 本身在工作时输出一个流。 (2认同)