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.

Using grep (or some other utility), is it possible to find all matches of a regular expression in a folder (searching the text of each file?) I want to find every match of the regular expression zenity within a specific folder, so that I can find the file that contains the string zenity.

share|improve this question
    
Alternatively, how can I find all matches of a regular expression in a directory? –  Anderson Green Dec 11 '12 at 4:33
add comment

1 Answer

up vote 2 down vote accepted

If you're using GNU grep, you can use -r.

grep -r zenity directory

Otherwise, if your grep implementation does not have any options for recursion, you can use find and grep:

find directory -exec grep -H zenity {} +
share|improve this answer
    
Will this search the folder recursively, and is it the same as grep -R zenity directory? Does capitalization matter, in this case? (It was written this way before the answer was edited.) –  Anderson Green Dec 11 '12 at 4:37
    
@AndersonGreen Yes, and yes. -r and -R are the same in GNU grep (-r is merely portable to other implementations). –  Chris Down Dec 11 '12 at 4:38
    
How would the command be altered if I wanted to search for a regex instead if a string? –  Anderson Green Dec 11 '12 at 4:40
2  
@AndersonGreen zenity is a regex. By default, GNU grep uses BRE. –  Chris Down Dec 11 '12 at 4:42
1  
Beware that GNU grep -r follows symlinks when descending directories, which is generally not what you want. Note that grep implementations are more likely to support -r than -H –  Stephane Chazelas Dec 11 '12 at 7:09
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.