2014-01-24 32 views
2

我想出了一个命令来查找文件并使用find,xargs和du打印它们的大小。当我搜索不存在的东西时,我遇到问题。使用xargs方法,du在不存在任何东西时报告所有文件夹,但我希望它不报告任何内容,因为找不到任何东西。当使用-exec方法时,它可以正常工作,但是从我在大型搜索中读取和观察到的情况来看,效率较低,因为它会为找到的每个文件重复du命令,而不是在找到的文件组上运行。请参阅它提及的部分 - 删除:http://content.hccfl.edu/pollock/unix/findcmd.htm管道空找到结果du通过xargs导致意想不到的行为

下面是一个示例。首先,这是在目录:

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 . -name '*.qux' -print0

如果我使用-exec方法杜,还没有返回值(如预期)

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

所以什么事用xargs du方法找时找不到什么东西?谢谢你的时间。

回答

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 - 
+0

这工作与未成年人编辑...添加'-I'和'{之间的空间}' :'找。 -name'* .qux'-type f -print0 | xargs -0 -I {} du -h {}'谢谢!作为一个方面说明,我尝试了我的原始代码,但没有使用'-print0'和'-0',并且目录仍然返回。这段代码:'find。 -name'* .qux'-type f -print | xargs du -h'仍然不起作用。 –

+0

@mixed_signals:对不起,编辑后发布了更好的解决方案IMO – grebneke

+0

这也适用:'find。 -name'* .qux'-type f -print0 | du -h --files0-from -'我会阅读这两个解决方案。再次感谢! –