# Use “grep” to match text in multiple files

I have data in multiple files. I want to find some text that matches in all files. Can I use the grep command for that? If yes then how?

-
This is bizarrely obvious, in that "grep PATTERN file1 file2 ..." will work. Do you mean to do something else? –  cjc Aug 29 '11 at 14:42

If you do not know where exactly the files are located, but know their names, you can use find:

find .  $$-name "filename1" -o -name "filename2"$$ -exec grep "<grepstatement>" '{}' \; -print


Assuming that the files are in this directory somewhere.

-
1. You use "and" instead of "or" logic between the files, 2. There is no need for mixing find into this, 3. Just what would be the purpose of the -print at the end?! –  rozcietrzewiacz Aug 29 '11 at 15:29
Good point with the and/or. I just fixed it. You also have a point about not needing to use find, I use the command when I don't know what which file things are in but know what information I'm looking for. –  Mark D Aug 29 '11 at 15:48
Ok, so I edited so that it looks more justified. But note that using -print at the end is not too good (my 3rd point in the previous comment) - it messes up the output; if you want to learn the location of the files, just use grep -H instead. –  rozcietrzewiacz Aug 29 '11 at 15:54

Just add all files on the command line. You can use * or ? or whatever your shell allows as placeholder.

From manpage:

grep [OPTIONS] PATTERN [FILE...]


means: as many files as you wish.. or none if you want to grep stdin/pipe.

-
grep 'mydata' *


The star * symbol signifies you want to search in multiple files. If you want to search through multiple files in multiple directories, you can add -R for a recursive search.

grep 'mydata' * -R

-