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.

  • 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 wilddiary.com/find-files-containing-my-text – Drona Oct 8 '15 at 13:55

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 {} +
  • 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 – Stéphane Chazelas Dec 11 '12 at 7:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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