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.

Is there an easy way to search inside 1000s of files in a complex directory structure to find files which contain a specific string within the file?

share|improve this question

2 Answers 2

up vote 3 down vote accepted
grep -H -R searchstring /directory

may want to redirect the results to a file (or tee)

You may also want to look at ack

share|improve this answer
    
Note that not all versions of grep support the -R option. –  gabe. Jun 14 '11 at 17:49
    
if grep does not support -R you can allways do: cat /directory/* | grep -H searchstring –  Wolfy Jun 15 '11 at 6:37
    
@Wolfy, this is not the same, especially if you have a complex directory structure ... And -H does not help you here. –  maxschlepzig Jun 15 '11 at 19:31
    
sorry, my mistake... –  Wolfy Jun 17 '11 at 11:35
    
You can use find and xargs. find -print0 -type f | xargs -0 grep -H searchstring –  paraxor Apr 20 '13 at 11:58

Yes, there is grep. It has an option for recursive directory traversal, e.g.:

grep -r specificstring path

With GNU grep you can restrict the searched files with the --include and --exclude pattern options.

-H is the default with -R (or -r which is the same and easier to type). You can also pipe the results of grep to less for screen paging.

share|improve this answer

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.