2014-01-24 36 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 -'我會閱讀這兩個解決方案。再次感謝! –