Bash 获取使用 head 命令提取的文件列表的大小

Bash get size of a list of files extracted with head command

我有以下 bash 命令,它给我一个文件夹的 300 个随机文件。

ls /directory/ | sort -R | head -n 300

我想像这样获取300个随机文件的总大小

ls /directory/ | sort -R | head -n 300 | du -h

问题是'du'不是针对这300个文件执行的,而是针对我当前所在的整个目录执行的。

我该怎么做?

像这样使用 findxargs

find /directory -mindepth 1 -maxdepth 1 | sort -R | head -n300 | xargs du -sh

要同时打印 xargs 提供给 du 的所有文件的总大小,请将 -c 选项添加到 du,如下所示:du -sch