145

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. – Joey Jul 13 '10 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. – Svish Jul 13 '10 at 10:09
  • Take a look at unix.stackexchange.com/a/37932/213832 this solution works well – boyd4715 Feb 2 '17 at 12:11
252
grep -r 'text goes here' path_goes_here
  • 1
    I would suggest a man grep to discover all the wonderful options of grep – Ludovic Kuty Jan 20 '16 at 12:31
  • This 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 Jun 1 '17 at 12:38
  • 3
    Got it: grep -lr "text pattern" ./ -s ; grep -lr "text pattern" [PATH OF PARENT] -s – CP3O Jun 1 '17 at 12:41
  • But this returns the contents of matching files, not the file details (name, path). @CP3O's suggestion works. – geotheory Nov 21 '18 at 10:55
26

use spotlight

mdfind "text goes here"
mdfind -onlyin /home/user/Desktop -live "live update"

don't forget to look at:

man mdfind
  • 2
    Will this work for files that spotlight doesn't index (i.e. files inside hidden folders, system config files, etc)? – Peter Berg Sep 10 '13 at 4:10
  • Grep doesn't seem to parse .xlsx files, but this worked out fine. Another easy example: mdfind -onlyin . "searchtext" – FvD Sep 2 '15 at 11:44
9
  1. Through Ack

brew install ack ack "text goes here"

  1. Through find

find . |grep "text goes here"

  1. Through grep

grep -RnslI "text goes here"

3

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:

Using grep enter image description here

Using ack enter image description here

1

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 with find, as it will say Permission denied for many files. – jpaugh Jun 1 '17 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? – G-Man Jun 1 '17 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 ./ – CP3O Jun 2 '17 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. – CP3O Jun 2 '17 at 9:39

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.