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:


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
  • 1
    umm, I cant seem to use normal file patterns like *.lua here..? – phil294 May 9 '17 at 4:05

I ultimately did:

grep -rli 'match_me' -- file1 file2 file3

It appears to be widely standard on Debian/Bash systems that a double dash with no name means no more flags can be defined; Often implying that all remaining arguments are file paths. Though disclaimer, I do not know if this is the intended semantic or just a symptom that fits to the tools I use.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.