Linux 如何在Bash中列出每个文件和目录的大小并按大小降序排序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7463554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 06:14:45  来源:igfitidea点击:

How to list the size of each file and directory and sort by descending size in Bash?

linuxfilebash

提问by Kit Ho

I found that there is no easy to get way the size of a directory in Bash?

我发现在 Bash 中没有容易获得目录大小的方法?

I want that when I type ls -<some options>, it can list of all the sum of the file size of directory recursively and files at the same time and sort by size order.

我希望当我输入时ls -<some options>,它可以同时列出目录和文件的所有文件大小的总和,并按大小顺序排序。

Is that possible?

那可能吗?

采纳答案by Developer

Simply navigate to directory and run following command:

只需导航到目录并运行以下命令:

du -a --max-depth=1 | sort -n

OR add -h for human readable sizes and -r to print bigger directories/files first.

或添加 -h 为人类可读的大小和 -r 首先打印更大的目录/文件。

du -a -h --max-depth=1 | sort -hr

回答by Thanatos

ls -Ssorts by size. Then, to show the size too, ls -lSgives a long (-l), sorted by size (-S) display. I usually add -htoo, to make things easier to read, so, ls -lhS.

ls -S按大小排序。然后,为了显示大小,ls -lS给出一个长 ( -l),按大小排序 ( -S) 显示。我通常-h也会添加,以使内容更易于阅读,因此,ls -lhS.

回答by sehe

du -s -- * | sort -n

(this willnot show hidden (.dotfiles) files)

(这不会显示隐藏的(.dotfiles)文件)

Use du -smfor Mb units etc. I always use

使用du -sm甲基溴单位等我总是使用

du -smc -- * | sort -n

because the total line (-c) will end up at the bottom for obvious reasons :)

因为-c很明显,总行 ( ) 将在底部结束:)

PS:

PS:

  • See comments for handling dotfiles
  • I frequently use e.g. 'du -smc /home//| sort -n |tail' to get a feel of where exactly the large bits are sitting
  • 请参阅处理点文件的注释
  • 我经常使用例如'du -smc /home/ /| sort -n |tail' 以了解大块的确切位置

回答by dvorak

I think I might have figured out what you want to do. This will give a sorted list of all the files and all the directories, sorted by file size and size of the content in the directories.

我想我可能已经弄清楚你想要做什么了。这将给出所有文件和所有目录的排序列表,按文件大小和目录中内容的大小排序。

(find . -depth 1 -type f -exec ls -s {} \;; find . -depth 1 -type d -exec du -s {} \;) | sort -n

回答by ztank1013

[enhanced version]
This is going to be much faster and precise than the initial versionbelow and will output the sum of all the file size of current directory:

[增强版]
这将比下面的初始版本更快更精确,并将输出当前目录所有文件大小的总和:

echo `find . -type f -exec stat -c %s {} \; | tr '\n' '+' | sed 's/+$//g'` | bc

the stat -c %scommand on a file will return its size in bytes. The trcommand here is used to overcome xargscommand limitations (apparently piping to xargsis splitting results on more lines, breaking the logic of my command). Hence tris taking care of replacing line feed with +(plus) sign. sedhas the only goal to remove the last +sign from the resulting string to avoid complains from the final bc(basic calculator) command that, as usual, does the math.

stat -c %s文件上的命令将以字节为单位返回其大小。tr这里的命令用于克服xargs命令限制(显然管道到xargs是在更多行上拆分结果,破坏了我的命令的逻辑)。因此tr要注意用+(加号)符号替换换行符。sed唯一的目标是+从结果字符串中删除最后一个符号,以避免来自最终bc(基本计算器)命令的抱怨,该命令像往常一样进行数学运算。

Performances:I tested it on several directories and over ~150.000 files top (the current number of files of my fedora 15 box) having what I believe it is an amazing result:

性能:我在几个目录和超过 150.000 个文件(我的 Fedora 15 盒子的当前文件数)上对其进行了测试,我认为这是一个惊人的结果:

# time echo `find / -type f -exec stat -c %s {} \; | tr '\n' '+' | sed 's/+$//g'` | bc
12671767700

real    2m19.164s
user    0m2.039s
sys 0m14.850s

Just in case you want to make a comparison with the du -sb /command, it will output an estimated disk usage in bytes (-boption)

以防万一您想与du -sb /命令进行比较,它会以字节为单位输出估计的磁盘使用情况(-b选项)

# du -sb /
12684646920 /

As I was expecting it is a little larger than my command calculation because the duutility returns allocated space of each file and not the actual consumed space.

正如我所期望的那样,它比我的命令计算稍大,因为该du实用程序返回每个文件的分配空间,而不是实际消耗的空间。

[initial version]
You cannot use ducommand if you need to know the exact sum size of your folder because (as per man page citation) duestimates file space usage. Hence it will lead you to a wrong result, an approximation (maybe close to the sum size but most likely greater than the actual size you are looking for).

[初始版本]如果您需要知道文件夹的确切总大小,
则不能使用du命令,因为(根据手册页引用)du估计文件空间使用情况。因此,它会导致您得到错误的结果,即近似值(可能接近总和大小,但很可能大于您要查找的实际大小)。

I think there might be different ways to answer your question but this is mine:

我认为可能有不同的方法来回答你的问题,但这是我的:

ls -l $(find . -type f | xargs) | cut -d" " -f5 | xargs | sed 's/\ /+/g'| bc

It finds all files under . directory (change . with whatever directory you like), also hidden files are included and (using xargs) outputs their names in a single line, then produces a detailed list using ls -l. This (sometimes) huge output is piped towards cut command and only the fifth field (-f5), which is the file size in bytes is taken and again piped against xargswhich produces again a single line of sizes separated by blanks. Now take place a sed magic which replaces each blank space with a plus (+) sign and finally bc(basic calculator) does the math.

它在 . 目录(将 . 更改为您喜欢的任何目录),还包含隐藏文件并(使用xargs)在一行中输出它们的名称,然后使用ls -l. 这个(有时)巨大的输出被传-f5送到cut 命令,只有第五个字段 ( ),它是文件大小(以字节为单位)被接受并再次传送到xargs它再次产生一行由空格分隔的大小。现在进行 sed 魔术,用加号 ( +)替换每个空格,最后bc(基本计算器)进行数学运算。

It might need additional tuning and you may have lscommand complaining about arguments list too long.

它可能需要额外的调整,你可能有ls命令抱怨参数列表太长。

回答by Ambal

Another simple solution.

另一个简单的解决方案。

$ for entry in $(ls); do du -s "$entry"; done | sort -n

the result will look like

结果看起来像

2900    tmp
6781    boot
8428    bin
24932   lib64
34436   sbin
90084   var
106676  etc
125216  lib
3313136 usr
4828700 opt

changing "du -s" to "du -sh" will show human readable size, but we won't be able to sort in this method.

将“du -s”更改为“du -sh”将显示人类可读的大小,但我们将无法在此方法中进行排序。

回答by Martin Thoma

Command

命令

du -h --max-depth=0 * | sort -hr

Output

输出

3,5M    asdf.6000.gz
3,4M    asdf.4000.gz
3,2M    asdf.2000.gz
2,5M    xyz.PT.gz
136K    xyz.6000.gz
116K    xyz.6000p.gz
88K test.4000.gz
76K test.4000p.gz
44K test.2000.gz
8,0K    desc.common.tcl
8,0K    wer.2000p.gz
8,0K    wer.2000.gz
4,0K    ttree.3

Explanation

解释

  • dudisplays "disk usage"
  • his for "human readable" (both, in sort and in du)
  • max-depth=0means duwill not show sizes of subfolders (remove that if you want to show all sizes of every file in every sub-, subsub-, ..., folder)
  • ris for "reverse" (biggest file first)
  • du显示“磁盘使用情况”
  • h用于“人类可读”(无论是在排序还是在 du 中)
  • max-depth=0意味着du不会显示子文件夹的大小(如果要显示每个子文件夹中每个文件的所有大小,请删除它)
  • r用于“反向”(最大文件在前)

ncdu

全国总工会

When I came to this question, I wanted to clean up my file system. The command line tool ncduis way better suited to this task.

当我遇到这个问题时,我想清理我的文件系统。命令行工具ncdu更适合此任务。

Installation on Ubuntu:

在 Ubuntu 上安装:

$ sudo apt-get install ncdu

Usage:

用法:

Just type ncdu [path]in the command line. After a few seconds for analyzing the path, you will see something like this:

只需ncdu [path]在命令行中输入即可。几秒钟后分析路径,您将看到如下内容:

$ ncdu 1.11 ~ Use the arrow keys to navigate, press ? for help
--- / ---------------------------------------------------------
.  96,1 GiB [##########] /home
.  17,7 GiB [#         ] /usr
.   4,5 GiB [          ] /var
    1,1 GiB [          ] /lib
  732,1 MiB [          ] /opt
. 275,6 MiB [          ] /boot
  198,0 MiB [          ] /storage
. 153,5 MiB [          ] /run
.  16,6 MiB [          ] /etc
   13,5 MiB [          ] /bin
   11,3 MiB [          ] /sbin
.   8,8 MiB [          ] /tmp
.   2,2 MiB [          ] /dev
!  16,0 KiB [          ] /lost+found
    8,0 KiB [          ] /media
    8,0 KiB [          ] /snap
    4,0 KiB [          ] /lib64
e   4,0 KiB [          ] /srv
!   4,0 KiB [          ] /root
e   4,0 KiB [          ] /mnt
e   4,0 KiB [          ] /cdrom
.   0,0   B [          ] /proc
.   0,0   B [          ] /sys
@   0,0   B [          ]  initrd.img.old
@   0,0   B [          ]  initrd.img
@   0,0   B [          ]  vmlinuz.old
@   0,0   B [          ]  vmlinuz

Delete the currently highlighted element with d, exit with CTRL+ c

用 删除当前突出显示的元素d,用CTRL+退出c

回答by Ghassan Shawahneh

you can use the below to list files by size du -h | sort -hr | more or du -h --max-depth=0 * | sort -hr | more

您可以使用以下命令按大小列出文件 du -h | 排序 -hr | 更多或 du -h --max-depth=0 * | 排序 -hr | 更多的

回答by Pace

I tend to use du in a simple way.

我倾向于以简单的方式使用 du。

du -sh */ | sort -n

This provides me with an idea of what directories are consuming the most space. I can then run more precise searches later.

这让我了解哪些目录占用的空间最多。然后我可以稍后运行更精确的搜索。

回答by cevaris

Apparently --max-depthoption is not in Mac OS X's version of the ducommand. You can use the following instead.

显然--max-depth选项不在 Mac OS X 的du命令版本中。您可以改用以下内容。

du -h -d 1 | sort -n

du -h -d 1 | sort -n