If you want to search in several files at once, you can type them all at the end of the command line, separated by spaces.
grep -i test /path/to/file /some/other/file
You can use wildcard patterns.
grep -i test README ChangeLog *.txt
If you have a list of files, with one file name per line, then you have several possibilities. If there aren't any exotic characters in your file names, then either of these will work:
grep -i test -- $(cat list_of_file_names.txt)
<list_of_file_names.txt xargs grep -i test -H --
The first command substitutes the output of the command cat list_of_file_names.txt
into the command line. It fails if any of the file names contains whitespace or shell wildcards (\[?*
). It also fails if the list is so large as to go over the command line length limit (more than about 128kB, on many systems). The second command fails if any of the file names contains whitespace of \"'
. It takes care of running egrep
multiple times if the command line length limit demands it. The -H
option ensures that grep
will always print the name of the matching file, even if it happens to have been called with a single file. The --
ensures that if the first file name begins with a -
, it will be treated as a file name and not as an option.
A safe way to handle file names that may contain any character other than newlines is to turn off splitting on whitespace other than newlines and turn off globbing (wildcard expansion).
set -f; IFS='
'
grep -i test -- $(cat list_of_file_names.txt)
set +f; unset IFS