Sign up ×
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
Go to the directory and type grep -l <keyword> * . This will list only the file names that contain the keyword. But it doesnt search sub directories. If you need to recursively search in sub directories as well then use a combination of find and grep commands. Refer here – Drona Oct 8 at 13:55

1 Answer 1

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
@AndersonGreen zenity is a regex. By default, GNU grep uses BRE. – Chris Down Dec 11 '12 at 4:42
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 – Stéphane Chazelas Dec 11 '12 at 7:09

Your Answer


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.