How to find files with certain text in the Terminal
Solution 1
grep -r 'text goes here' path_goes_here
Solution 2
use spotlight
mdfind "text goes here"
mdfind -onlyin /home/user/Desktop -live "live update"
don't forget to look at:
man mdfind
Solution 3
- Through Ack
brew install ack
ack "text goes here"
- Through find
find . |grep "text goes here"
- Through grep
grep -RnslI "text goes here"
Solution 4
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:
Solution 5
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?
Related videos on Youtube
Svish
Software Developer, Geek, HSP, SDA, ..., open, honest, careful, perfectionist, ... Currently into indoor rowing and rock climbing, just to mention something non-computer-related... Not the best at bragging about myself... so... not sure what more to write... 🤔
Updated on September 17, 2022Comments
-
Svish almost 2 years
I'd like to find all files that contain a certain string of text. How would you do that in the Terminal?
-
Admin almost 14 yearsGood 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.
-
Admin over 7 yearsTake a look at unix.stackexchange.com/a/37932/213832 this solution works well
-
-
MaxiFlaxiBoii almost 11 yearsWill this work for files that spotlight doesn't index (i.e. files inside hidden folders, system config files, etc)?
-
FvD almost 9 yearsGrep doesn't seem to parse .xlsx files, but this worked out fine. Another easy example: mdfind -onlyin . "searchtext"
-
Ludovic Kuty over 8 yearsI would suggest a
man grep
to discover all the wonderful options of grep -
CP3O about 7 yearsThis 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? -
CP3O about 7 yearsGot it:
grep -lr "text pattern" ./ -s
;grep -lr "text pattern" [PATH OF PARENT] -s
-
jpaugh about 7 yearsAnother 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. -
G-Man Says 'Reinstate Monica' about 7 years(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? -
CP3O about 7 years@G-Man I edited the answer to show
[PATH OF DIRECTORY]
just incase someone unfamiliar wanted to know why I had put./
-
CP3O about 7 years@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.
-
geotheory over 5 yearsBut this returns the contents of matching files, not the file details (name, path). @CP3O's suggestion works.
-
Daniel Springer almost 4 yearsHow to hide "permission denied" results?