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– Anderson Green2012-12-11 04:33:46 +00:00Commented 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-textDrona– Drona2015-10-08 13:55:34 +00:00Commented Oct 8, 2015 at 13:55
Add a comment |
1 Answer
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– Anderson Green2012-12-11 04:37:22 +00:00Commented Dec 11, 2012 at 4:37 - @AndersonGreen Yes, and yes.
-rand-Rare the same in GNU grep (-ris merely portable to other implementations).Chris Down– Chris Down2012-12-11 04:38:23 +00:00Commented Dec 11, 2012 at 4:38 - How would the command be altered if I wanted to search for a regex instead if a string?Anderson Green– Anderson Green2012-12-11 04:40:22 +00:00Commented Dec 11, 2012 at 4:40
- 2@AndersonGreen
zenityis a regex. By default, GNUgrepuses BRE.Chris Down– Chris Down2012-12-11 04:42:02 +00:00Commented Dec 11, 2012 at 4:42 - 1Beware that GNU
grep -rfollows symlinks when descending directories, which is generally not what you want. Note thatgrepimplementations are more likely to support-rthan-HStéphane Chazelas– Stéphane Chazelas2012-12-11 07:09:33 +00:00Commented Dec 11, 2012 at 7:09