Grep Files from List – Efficient Methods and Commands

bashcommand-substitutiongrepprocess-substitution

I am trying to run grep against a list of a few hundred files:

$ head -n 3 <(cat files.txt)
admin.php
ajax/accept.php
ajax/add_note.php

However, even though I am grepping for a string that I know is found in the files, the following does not search the files:

$ grep -i 'foo' <(cat files.txt)

$ grep -i 'foo' admin.php
The foo was found

I am familiar with the -f flag which will read the patterns from a file. But how to read the input files?

I had considered the horrible workaround of copying the files to a temporary directory as cp seems to support the <(cat files.txt) format, and from there grepping the files. Shirley there is a better way.

Best Answer

You seem to be grepping the list of filenames, not the files themselves. <(cat files.txt) just lists the files. Try <(cat $(cat files.txt)) to actually concatenate them and search them as a single stream, or

grep -i 'foo' $(cat files.txt)

to give grep all the files.

However, if there are too many files on the list, you may have problems with number of arguments. In that case I'd just write

while read filename; do grep -Hi 'foo' "$filename"; done < files.txt
Related Question