2

我想出了一个命令来查找文件并使用 find、xargs 和 du 打印它们的大小。当我搜索不存在的东西时遇到问题。使用 xargs 方法,当某些内容不存在时,du 会报告所有文件夹,但我希望它不会报告任何内容,因为应该找不到任何内容。使用 -exec 方法时,它可以正常工作,但是根据我在更大搜索中阅读和观察的内容,它的效率较低,因为它对找到的每个文件重复 du 命令,而不是对找到的文件组进行操作。请参阅它提到 -delete 的部分:http ://content.hccfl.edu/polllock/unix/findcmd.htm

这是一个例子。首先,这是目录中的内容:

ls
bar_dir/ test1.foo test2.foo test3.foo

ls bar_dir
test1.bar test2.bar test3.bar

以下是我希望找到结果的两个搜索:

find . -name '*.foo' -type f -print0 | xargs -0 du -h
4.0K ./test2.foo
4.0K ./test1.foo
4.0K ./test3.foo

find . -name '*.bar' -type f -print0 | xargs -0 du -h
4.0K ./bar_dir/test1.bar
4.0K ./bar_dir/test2.bar
4.0K ./bar_dir/test3.bar

这是一个我不希望有结果的搜索,但我得到了一个目录列表:

find . -name '*.qux' -type f -print0 | xargs -0 du -h
16K ./bar_dir
32K .

如果我只使用 find,它不会返回任何内容(如预期的那样)

find . -name '*.qux' -print0

如果我对 du 使用 -exec 方法,它也不会返回任何内容(如预期的那样)

find . -name '*.qux' -type f -exec du -h '{}' \;

那么当 find 没有找到任何东西时,xargs du 方法有什么问题呢?谢谢你的时间。

4

1 回答 1

0

你看了du --files0-from -吗?

man du

   --files0-from=F
          summarize disk usage of the NUL-terminated file names specified in file F; If F is - then read names from standard input

试试这样:

find . -name '*.qux' -type f -print0 | du -h --files0-from -
于 2014-01-24T17:36:05.950 回答