2017-02-18 150 views
1

我有一個包含惡意文件名列表的文件。有很多包含空格的文件名。我需要找到他們並更改他們的權限。我曾嘗試以下:xargs錯誤:文件名太長

grep -E ". " suspicious.txt | xargs -0 chmod 000 

但我得到一個錯誤:

:File name too long 

的想法?

回答

4

OK,你必須每行一個文件名的文件,問題在於xargs沒有-0會把空格和製表符以及換行符作爲文件分隔符,而xargs-0預計,文件名由NUL分開字符,並不會關心換行符。

結果送入xargs -0命令之前,所以,開啓新行到NUL S:

grep -E ". " suspicious.txt | tr '\n' '\0' | xargs -0 chmod 000 
+0

謝謝馬克,它的工作! – Zaheer

0

更新:

Mark Reeds正確答案。這是錯誤的,因爲文件名需要空值,而不是由grep生成的文件名。

原文:

你需要更多的東西是這樣的:

grep -Z -E ". " suspicious.txt | xargs -0 chmod 000 

從xargs的手冊頁:

Because Unix filenames can contain blanks and newlines, this default behaviour is often problematic; filenames containing blanks and/or newlines are incorrectly processed by xargs. In these situations it is better to use the -0 option, which prevents such problems. When using this option you will need to ensure that the program which produces the input for xargs also uses a null character as a separator.

從grep的手冊頁:

-Z, --null 

Output a zero byte (the ASCII NUL character) instead of the character that normally follows a file name. For example, grep -lZ outputs a zero byte after each file name instead of the usual newline. This option makes the output unambiguous, even in the presence of file names containing unusual characters like newlines. This option can be used with commands like find -print0, perl -0, sort -z, and xargs -0 to process arbitrary file names, even those that contain newline characters.

+0

不,那是不對的。文件名是文本文件中的行;他們已經被換行符分隔了。 '-Z'只能幫助'grep'本身輸出的文件名(例如'grep -l')。 –

+0

不幸的是,斯蒂芬的解決方案沒有工作 – Zaheer

相關問題