可读,递归,排序的最大文件列表 [英] Human readable, recursive, sorted list of largest files

查看:89
本文介绍了可读,递归,排序的最大文件列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在POSIX shell中打印最大的前10个最大文件列表的最佳实践是什么?必须有比我当前解决方案更优雅的东西:

What is the best practice for printing a top 10 list of largest files in a POSIX shell? There has to be something more elegant than my current solution:

DIR="."
N=10
LIMIT=512000

find $DIR -type f -size +"${LIMIT}k" -exec du {} \; | sort -nr | head -$N | perl -p -e 's/^\d+\s+//' | xargs -I {} du -h {}

其中LIMIT是用于限制查找结果的文件大小阈值.

where LIMIT is a file size threshold to limit the results of find.

推荐答案

这使用awk为排序键创建额外的列.它仅调用du一次.输出应与du完全一样.

This uses awk to create extra columns for sort keys. It only calls du once. The output should look exactly like du.

我已将其拆分为多行,但可以将其重新组合成一个单行.

I've split it into multiple lines, but it can be recombined into a one-liner.

du -h |
  awk '{printf "%s %08.2f\t%s\n", 
    index("KMG", substr($1, length($1))),
    substr($1, 0, length($1)-1), $0}' |
  sort -r | cut -f2,3

说明:

  • BEGIN-创建一个字符串索引以用K,M,G替换1,2,3来按单位分组,如果没有单位(大小小于1K),则没有匹配项,并且返回零(完美!)
  • 打印新字段-单位,值(为使Alpha排序正常工作,它应为零填充,固定长度)和原始行
  • 索引大小字段的最后一个字符
  • 拉出尺寸的数字部分
  • 对结果进行排序,丢弃多余的列

不用cut命令尝试一下,看看它在做什么.

Try it without the cut command to see what it's doing.

这是一个可以在AWK脚本中进行排序并且不需要剪切的版本(需要GNU AWK(gawk)来支持asorti):

Here's a version which does the sorting within the AWK script and doesn't need cut (requires GNU AWK (gawk) for asorti support):

du -h |
   awk '{idx = sprintf("%s %08.2f %s", 
         index("KMG", substr($1, length($1))),
         substr($1, 0, length($1)-1), $0);
         lines[idx] = $0}
    END {c = asorti(lines, sorted);
         for (i = c; i >= 1; i--)
           print lines[sorted[i]]}'

这篇关于可读,递归,排序的最大文件列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆