Linux 使用 ls 列出目录及其总大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1019116/
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-03 17:26:55  来源:igfitidea点击:

Using ls to list directories and their total sizes

linuxunix

提问by kmorris511

Is it possible to use lsin Unix to list the total size of a sub-directory and all its contents as opposed to the usual 4Kthat (I assume) is just the directory file itself?

是否可以ls在 Unix 中使用来列出子目录的总大小及其所有内容,而不是通常的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

After scouring the man pages I'm coming up empty.

搜索手册页后,我发现空空如也。

采纳答案by molf

Try something like:

尝试类似:

du -sh *

short version of:

简短版本:

du --summarize --human-readable *

Explanation:

解释:

du: Disk Usage

dudISK ü鼠尾草

-s: Display a summary for each specified file. (Equivalent to -d 0)

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

-h: "Human-readable" output. Use unit suffixes: Byte, Kibibyte (KiB), Mebibyte (MiB), Gibibyte (GiB), Tebibyte (TiB) and Pebibyte (PiB). (BASE2)

-h:“人类可读”的输出。利用单元的后缀:YTE,ķibibyte(KIB),中号ebibyte(MIB),G ^ibibyte(GIB),Ťebibyte(TIB)和Pebibyte(PIB)。(BASE2)

回答by zappan

look at ducommand for that

看看du命令

回答by Crenshaw

I always use du -sk(-kflag showing file size in kilobytes) instead.

我总是使用du -sk-k标志以千字节为单位显示文件大小)。

回答by Keith Smith

The command you want is 'du -sk' du = "disk usage"

你想要的命令是 'du -sk' du = "磁盘使用情况"

The -k flag gives you output in kilobytes, rather than the du default of disk sectors (512-byte blocks).

-k 标志以千字节为单位提供输出,而不是磁盘扇区(512 字节块)的 du 默认值。

The -s flag will only list things in the top level directory (i.e., the current directory, by default, or the directory specified on the command line). It's odd that du has the opposite behavior of ls in this regard. By default du will recursively give you the disk usage of each sub-directory. In contrast, ls will only give list files in the specified directory. (ls -R gives you recursive behavior.)

-s 标志将只列出顶级目录(即当前目录,默认情况下,或在命令行上指定的目录)中的内容。在这方面,du 的行为与 ls 截然相反,这很奇怪。默认情况下 du 将递归地为您提供每个子目录的磁盘使用情况。相比之下, ls 只会给出指定目录中的列表文件。(ls -R 为您提供递归行为。)

回答by GraveDigger

du -sk * | sort -nwill sort the folders by size. Helpful when looking to clear space..

du -sk * | sort -n将按大小对文件夹进行排序。在寻找清理空间时很有帮助..

回答by user2969885

du -sh * | sort -h

This will be displayed in human readable format.

这将以人类可读的格式显示。

回答by Pascal

To display current directory's files and subdirectories sizes recursively:

递归显示当前目录的文件和子目录大小:

du -h .

To display the same size informationbut withoutprinting their sub directories recursively (which can be a huge list), just use the --max-depthoption:

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

du -h --max-depth=1 .

回答by kakubei

These are all great suggestions, but the one I use is:

这些都是很好的建议,但我使用的是:

du -ksh * | sort -n -r

-kshmakes sure the files and folders are listed in a human-readable format and in megabytes, kilobytes, etc. Then you sort them numerically and reverse the sort so it puts the bigger ones first.

-ksh确保文件和文件夹以人类可读的格式列出,以兆字节、千字节等为单位。然后按数字对它们进行排序并反转排序,以便将较大的放在最前面。

The only downside to this command is that the computer does not know that Gigabyte is bigger than Megabyte so it will only sort by numbers and you will often find listings like this:

此命令的唯一缺点是计算机不知道 Gigabyte 大于 Megabyte,因此它只会按数字排序,您经常会发现这样的列表:

120K
12M
4G

Just be careful to look at the unit.

只是小心地看单位。

This command also works on the Mac (whereas sort -hdoes not for example).

此命令也适用于 Mac(而sort -h例如不适用)。

回答by StarDust

du -h --max-depth=1 . | sort -n -r

回答by John

du -sch * in the same directory.

du -sch * 在同一目录中。