2

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.

2
  • Alternatively, how can I find all matches of a regular expression in a directory? Commented Dec 11, 2012 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 Commented Oct 8, 2015 at 13:55

1 Answer 1

3

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 {} + 
5
  • 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.) Commented Dec 11, 2012 at 4:37
  • @AndersonGreen Yes, and yes. -r and -R are the same in GNU grep (-r is merely portable to other implementations). Commented Dec 11, 2012 at 4:38
  • How would the command be altered if I wanted to search for a regex instead if a string? Commented Dec 11, 2012 at 4:40
  • 2
    @AndersonGreen zenity is a regex. By default, GNU grep uses BRE. Commented Dec 11, 2012 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 Commented Dec 11, 2012 at 7:09

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.