265

I'd like to find all files that contain a certain string of text. How would you do that in the Terminal?

Chealion
  • 26,327
Svish
  • 41,258

5 Answers5

429
grep -r 'text goes here' path_goes_here
44

use spotlight

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

don't forget to look at:

man mdfind
user46046
  • 1,331
25
  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"
5

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

karthiks
  • 151
2

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?

CP3O
  • 129
  • 2