bash 命令打印出大文件,排序,大小为人类可读格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8943154/
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
command to print out large files, sorted, with sizes in human readable format
提问by Christopher Neylan
I've written a simple shell script that finds large files, mostly to save myself some typing. The work is being done with:
我编写了一个简单的 shell 脚本来查找大文件,主要是为了节省一些打字的时间。这项工作正在完成:
find $dir -type f -size +"$size"M -printf '%s %p\n' | sort -rn
I'd like to turn the byte output into a human readable format. I found ways online on how to manually do this, e.g.,
我想将字节输出转换为人类可读的格式。我在网上找到了如何手动执行此操作的方法,例如,
find $dir -type f -size +"$size"M -printf '%s %p\n' | sort -rn |
awk '{ hum[1024**4]="TB"; hum[1024**3]="GB"; hum[1024**2]="MB"; hum[1024]="KB"; hum[0]="B";
for (x=1024**4; x>=1024; x/=1024){
if (>=x) { printf "%7.2f %s\t%s\n",/x,hum[x],;break }
}}'
But this seems messy. I was wondering: is there was a standard way to convert bytes into a human-readable form?
但这似乎很混乱。我想知道:是否有一种标准方法可以将字节转换为人类可读的形式?
Of course, any alternate methods of producing the below output, given a directory and min-size as input, are also welcome:
当然,也欢迎任何产生以下输出的替代方法,给定目录和最小大小作为输入:
1.25 GB /foo/barf
598.80 MB /foo/bar/bazf
500.58 MB /bar/bazf
421.70 MB /bar/baz/bamf
...
Note: This must work on both 2.4 and 2.6, and the output should be sorted.
注意:这必须在 2.4 和 2.6 上都有效,并且应该对输出进行排序。
回答by olibre
Use du -hand sort -h
使用du -h和sort -h
find /your/dir -type f -size +5M -exec du -h '{}' + | sort -hr
Explanations:
说明:
du -h file1 file2 ...prints the disk usage in human readable format of the given files.sort -hrsorts human readable numbers in reverse order (larger numbers first).- the option
+offind -execwill reduce the number of invocations of commandduand therefore will speed up the execution. Here+can be replaced by';'.
du -h file1 file2 ...打印dISK ü圣贤^ h乌曼读取指定的文件格式。sort -hr排序ħ在UMAN可读的数字- [REVERSE顺序(较大的数字第一)。- 选项
+的find -exec会降低指令的调用次数du,并且因此将加快执行速度。这里+可以用 代替';'。
You can remove option -rof sortcommand if you want the larger files being printed at the end. You can even use the simpler following command, but your terminal window buffer may be filled!
您可以删除选项-r的sort,如果你想被打印在最后的大文件的命令。您甚至可以使用以下更简单的命令,但您的终端窗口缓冲区可能已满!
find /your/dir -type f -exec du -h '{}' + | sort -h
Or if you want just the top ten larger files:
或者,如果您只想要前十个较大的文件:
find /your/dir -type f -exec du -h '{}' + | sort -hr | head
Note:option -hof sorthas been introduced in about 2009, therefore this option may not be available on old distro (as Red Hat 5). Moreover the option +of find -execis not available either on older distro (as Red Hat 4).
注意:选项-hofsort已在 2009 年左右引入,因此该选项可能不适用于旧发行版(如 Red Hat 5)。此外,该选项+的find -exec不可用或者在旧的发行版(如红帽4)。
On old distro, you can use xargsinstead of option +of find -exec. The command lsmay also be used to print sorted files. But to guarantee the sorting by size, xargsmust invoke lsonly once. xargscan invoke lsonly once if your amount of files is acceptable: it depends on the text length passed to lsargument (sum of all filenames length).
在老版本,你可以使用xargs,而不是选择+的find -exec。该命令ls还可用于打印排序文件。但是为了保证按 size 排序,xargs必须ls只调用一次。如果您的文件数量可以接受,则xargs只能调用ls一次:这取决于传递给ls参数的文本长度(所有文件名长度的总和)。
find /your/dir -type f -size +5M -print0 | xargs -0 ls -1Ssh
(with a little inspiration borrowed from MichaelKrelin-hacker).
(从MichaelKrelin-hacker借来的一点灵感)。
Explanations:
说明:
ls -1displays one file per linels -Ssorts by file sizels -sprints the file sizels -hprints sizes in human readable format
ls -1每行显示一个文件ls -S按文件大小排序ls -s打印文件大小ls -h以人类可读的格式打印尺寸
The fastest command may be using the above ls -1Sshwith the +option of find -execbut as above the amount of files must be acceptable to invoke lsonly once in order to guarantee the sorting by size(option +of find -execworks in much the same way as xargs).
最快的命令可能是使用上面ls -1Ssh的+选项,find -exec但如上所述ls,为了保证按大小排序,文件的数量必须可以接受,只能调用一次(选项+的find -exec工作方式与 大致相同xargs)。
find /your/dir -type f -size +5M -exec ls -1Ssh '{}' +
To reduce the amount of files found, you can increase the threshold size: replace +5Mby +100Mfor instance.
为了减少文件的发现的量,可以增加阈值大小:取代+5M通过+100M对实例。
回答by Michael Krelin - hacker
回答by Rahul Thakkar
To find files > 10Mb in current directory sorted by size with human readable form
在当前目录中查找 > 10Mb 的文件,按大小排序,具有人类可读的形式
find . -type f -size +10M | xargs du -sh | sort -rn
find . -type f -size +10M | xargs du -sh | sort -rn

