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.
So turn the newlines into NUL
s before feeding the result into the xargs -0
command:
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
.
Original:
You need something more like this:
grep -Z -E ". " suspicious.txt | xargs -0 chmod 000
From xargs man page:
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.
From grep man page:
-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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.