Reputation: 44371
I have a list of files in a file. The list is huge, and the filenames are non-standard: that means, some have spaces, non-ascii characters, quotes, single quotes ...
So, passing that huge list of files to grep as arguments is not an option:
xargs
, though.There must be a simpler way: how can I tell grep to use my list of files as files to grep from? I assume that since the list of files would not be processed by the shell, escaping and argument length is not an issue anymore. The question is whether grep supports this operation mode, which I have been unable to find in the documentation.
Upvotes: 10
Views: 15243
Reputation: 12749
GNU grep does not support this as far as I know. You have a few options.
Use a bash loop to parse your file list
This is the solution provided by @fedorqui
while read file; do
grep "$PATTERN" "$file"
done < file_with_list_of_files
Use xargs to pass multiple files to grep at once
Here I've told xargs to pass 10 filenames to grep
PATTERN='^$' # searching for blank lines
xargs -n 10 -d '\n' grep "$PATTERN" < file_with_list_of_files
Use xargs to pass multiple files to grep at once, dealing with newlines in filenames
As above, but using null-terminated lines
PATTERN='^$' # searching for blank lines
tr '\n' '\0' file_with_list_of_files
xargs -n 10 -0 grep "$PATTERN" < file_with_list_of_files
Edit: deal with whitespace correctly Edit2: Remove example that produces garbled output, add example that deals with newlines
Upvotes: 11