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.

What command would I use to find all of the files under the girl directory that have the ascii text “shirt” inside the files, starting from the root directory?

share|improve this question

3 Answers 3

find . -print0 | xargs -0 grep -FH text

Runs faster than either -exec or grep -r on my machine.

share|improve this answer
    
This is the best answer of the (current) three, because it's a more general technique, it's more portable, and doesn't require memorising finds funky syntax. It's also more flexible, because you can include any number of other filters between the find and the xargs (by the way, the -print0 and -0 are formally correct, but rarely necessary, and usually a pest to type) –  Norman Gray Oct 23 '11 at 19:45
1  
Nice. It won't be faster when there are many binary files, which grep knows how to skip. @norman-gray -print0 and -0 are always necessary for scripts. They live on past the author and can give "wtf" moments when they don't work correctly. –  Shawn J. Goff Oct 23 '11 at 20:11
    
@ShawnJ.Goff Yes, I agree with you about -print0 in scripts -- it's best to be on one's best behaviour when it's written down for others to see... –  Norman Gray Oct 30 '11 at 17:46

grep is the standard tool for searching for a pattern inside of files. It has an option for searching recursively, which means if it finds a directory, it goes inside and searches in everything in there.

The command would simply be grep -r shirt /path/to/girl/

I also like to add two options that skip files that I know I'm not interested in: --binary-files=without-match and --devices=skip.

share|improve this answer
4  
If you just want a list of files, use grep -r -l shirt /path/to/girl. –  Keith Thompson Oct 23 '11 at 19:31
    
Yes, @keith-thompson 's suggestion is faster, also because it will stop scanning once it finds just one match. –  Shawn J. Goff Oct 23 '11 at 20:03
    
Thank you Shawn J. Goff. grep -r shirt /path/to/girl/ worked perfectly! –  Malia Oct 23 '11 at 22:33

As stated by Shawn, using grep -r might be enough for your needs.

Otherwise a combination of find and grep can do the trick:

find . -exec grep -H text '{}' ';'

This allows to refine the search using specific find predicates. (And -H option is there so that filenames are printed along with the matched content.)

share|improve this answer
    
The above helped all well. Thank you! –  Malia Oct 23 '11 at 22:34

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.