简体   繁体   English

xargs错误:文件名太长

[英]xargs error: File name too long

I have a file that has a list of malicious file names. 我有一个包含恶意文件名列表的文件。 There are many file names contains blank spaces. 有很多文件名包含空格。 I need to find them and change their permissions. 我需要找到它们并更改其权限。 I have tried the following: 我尝试了以下方法:

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

But I am getting an error: 但我收到一个错误:

:File name too long 

An ideas? 有想法吗?

OK, you have one filename per line in your file, and the problem is that xargs without -0 will treat spaces and tabs as well as the newlines as file separators, while xargs with -0 expects the filenames to be separated by NUL characters and won't care about the newlines at all. 好的,文件中每行有一个文件名,问题是不带-0 xargs会将空格和制表符以及换行符当作文件分隔符,而 -0 xargs期望文件名用NUL字符和根本不会在乎换行符。

So turn the newlines into NUL s before feeding the result into the xargs -0 command: 因此,在将结果输入xargs -0命令之前,将换行符转换为NUL

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

Update: 更新:

See Mark Reeds correct answer. 参见马克·里德斯的正确答案。 This was wrong because nulls were needed for the filenames from the file, not the filenames generated by grep . 这是错误的,因为文件中的文件名需要空值,而不是grep生成的文件名。

Original: 原版的:

You need something more like this: 您需要这样的东西:

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

From xargs man page: 从xargs手册页:

Because Unix filenames can contain blanks and newlines, this default behaviour is often problematic; 因为Unix文件名可以包含空格和换行符,所以这种默认行为经常会出现问题。 filenames containing blanks and/or newlines are incorrectly processed by xargs. xargs错误地处理了包含空格和/或换行符的文件名。 In these situations it is better to use the -0 option, which prevents such problems. 在这些情况下,最好使用-0选项,以防止出现此类问题。 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. 使用此选项时,需要确保为xargs生成输入的程序还将空字符用作分隔符。

From grep man page: 从grep手册页:

 -Z, --null 

Output a zero byte (the ASCII NUL character) instead of the character that normally follows a file name. 输出零字节(ASCII NUL字符),而不是通常在文件名后的字符。 For example, grep -lZ outputs a zero byte after each file name instead of the usual newline. 例如,grep -lZ在每个文件名之后输出一个零字节,而不是通常的换行符。 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. 此选项可与诸如find -print0,perl -0,sort -z和xargs -0之类的命令一起使用,以处理任意文件名,即使是包含换行符的文件名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM