我有以下bash命令,该命令为我提供了一个文件夹的300个随机文件。
ls / directory / | 排序-R | 头-n 300
我想像这样获得300个随机文件的总大小
ls / directory / | 排序-R | 头-n 300 | 杜-h
问题是,“ du”不是针对这300个文件执行的,而是针对我当前所在的整个目录执行的。
我该怎么做?
使用find
和xargs
像这样:
find /directory -mindepth 1 -maxdepth 1 | sort -R | head -n300 | xargs du -sh
要同时打印所有要xargs
馈入的文件的总大小,请向du
中添加-c
选项du
,如下所示:du -sch
如果使用-printf“%k”来使用find,则可能不需要一起使用du
谢谢你。现在工作正常。但是,有没有办法在最后打印总尺寸呢?
@PetarYakov更新了答案。