2015-06-20 28 views
0

我有一個完整的名稱中有空格的目錄/文件夾,我想所有的*.ttf文件複製到另一個目錄除了文件夾ConsolasSystem Volume Information 。繼this後,我想:嘗試到grep一個發現-print0引起奇怪的行爲

find ... -print0 | grep -v "System Volume Information" --null | grep -v "Consolas" --null | xargs -0 cp -t destination 

出於某種原因,這導致從第一grep的一個Binary file (standard input) matches消息。使用的egrep的過濾器結合起來,我試圖:

find . -name '*.ttf' -print0 | egrep -v "(System Volume Information)|(Consolas)" --null | xargs... 

但在這種情況下,egrep的將打印出沒有到終端,即使有很多其他的文件夾,除了System Volume InformationConsolas。當我刪除除了find部分命令之外的所有內容時,由於-print0選項,我得到了一大段沒有換行符的文本。由於整個區塊包括Consolas,通過省略它我省略了的一切。我確認了這一點,當我嘗試對結果做一個grep Arial並且打印出了整個塊。

我應該如何防止這種行爲在grep?

+1

你正在告訴'find'輸出一個空終止列表,你不告訴'grep'讀一個空終止列表,你需要一個額外的'--null-data'來處理'greps' – BroSlow

+0

@ BroSlow經過數小時的搜索後,非常感謝'--null-data',並且不會誤解'-Z'和'-z'! –

回答

-1

這是使用grep的'-v'選項的預期行爲。這是通過不給你任何含有Consolas的行來告訴它的。爲避免這種情況,請不要使用-print0和xargs後處理(如你已經發現的那樣),或者如果你真的需要一起運行所有東西,可以在'grep -v'過濾器之後通過類似的方式執行:

echo -n $(find。-name'* .ttf'| grep -v「System Volume Information」--null | grep -v「Consolas」--null)

但您可能並不需要這個問題作爲xargs是足夠的。

+0

我試圖做一個'cp -t dest $(rest_of_command)',但是似乎空間文件夾仍然沒有正確處理。也許這不是一個'xargs'問題? –

+0

請嘗試以下操作。 cp -t dest''echo -n $(...)'' –

相關問題