I'd like to find all files that contain a certain string of text. How would you do that in the Terminal?
-
If you want a fast result, use Spotlight, though. Also if you want to find files that don't necessarily store text directly within the file such as PDF or ODF.– JoeyJul 13, 2010 at 9:47
-
Good advice, but in my case I'm not sure the files are indexed since they are on network drivers. And also they are xml files belonging to a particular program.– SvishJul 13, 2010 at 10:09
-
Take a look at unix.stackexchange.com/a/37932/213832 this solution works well– boyd4715Feb 2, 2017 at 12:11
5 Answers
grep -r 'text goes here' path_goes_here
-
1I would suggest a
man grep
to discover all the wonderful options of grep Jan 20, 2016 at 12:31 -
1This lists every file that exists in the path and adds :
No such file or directory
I am trying to get just the list of text occurrences. How can we get that list?– CP3OJun 1, 2017 at 12:38 -
11Got it:
grep -lr "text pattern" ./ -s
;grep -lr "text pattern" [PATH OF PARENT] -s
– CP3OJun 1, 2017 at 12:41 -
But this returns the contents of matching files, not the file details (name, path). @CP3O's suggestion works. Nov 21, 2018 at 10:55
-
2How to hide "permission denied" results?– user517095Jul 12, 2020 at 20:51
use spotlight
mdfind "text goes here"
mdfind -onlyin /home/user/Desktop -live "live update"
don't forget to look at:
man mdfind
-
2Will this work for files that spotlight doesn't index (i.e. files inside hidden folders, system config files, etc)? Sep 10, 2013 at 4:10
-
Grep doesn't seem to parse .xlsx files, but this worked out fine. Another easy example: mdfind -onlyin . "searchtext"– FvDSep 2, 2015 at 11:44
- Through Ack
brew install ack
ack "text goes here"
- Through find
find . |grep "text goes here"
- Through grep
grep -RnslI "text goes here"
You can choose one of the below depending on your taste and needs. Supposing you need to search for files containing text - "async", recursively in current directory, you can do so in one of the ways like below:
Ignacio's Answer is great and helped me find the files containing certain text. The only issue I was facing was that when running this command all the files would be listed, including one where the pattern did not show up.
No such file or directory
This is what I see alongside files that do not contain the pattern.
If instead you add -s
to the command, as in:
grep -lr "text pattern" ./ -s
; grep -lr "text pattern" [PATH DIRECTORY] -s
is used, it will only show you which files contain the pattern.
Similarly if grep -nr "text pattern" ./ -s
; grep -nr "text pattern" [PATH OF DIRECTORY] -s
command is used it prints the file plus the line number, and occurrence of the pattern.
Please correct me if my understanding is wrong.
Reference: How can I have grep not print out 'No such file or directory' errors?
-
Another way to avoid those pesky No such file errors, is to pipe stderr to null.
grep ... 2>/dev/null
. This solution works for most programs, since they report error messages on the stderr stream, not stdout. I have used this solution many times withfind
, as it will say Permission denied for many files.– jpaughJun 1, 2017 at 18:02 -
(1) If you’re getting
No such file or directory
messages for files that exist, either you’re doing something wrong, or your system is broken. (2) What does[PATH OF PARENT]
have to do with anything? Jun 1, 2017 at 19:01 -
@G-Man I edited the answer to show
[PATH OF DIRECTORY]
just incase someone unfamiliar wanted to know why I had put./
– CP3OJun 2, 2017 at 9:38 -
@G-Man I'm using a mac not Ubuntu, not sure how my system would be broken, I've barely done anything on it yet.– CP3OJun 2, 2017 at 9:39