Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
    
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
add comment

3 Answers

up vote 2 down vote accepted

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.

share|improve this answer
    
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
add comment

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.

share|improve this answer
add comment
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
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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