How to find files with certain text in the Terminal

179

51

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

Svish

Posted 2010-07-13T09:42:08.147

Reputation: 27 731

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 – 2010-07-13T09:47:46.260

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 – 2010-07-13T10:09:47.223

Take a look at http://unix.stackexchange.com/a/37932/213832 this solution works well

– boyd4715 – 2017-02-02T12:11:32.387

Answers

299

grep -r 'text goes here' path_goes_here

Ignacio Vazquez-Abrams

Posted 2010-07-13T09:42:08.147

Reputation: 100 516

1I would suggest a man grep to discover all the wonderful options of grep – Ludovic Kuty – 2016-01-20T12:31:53.687

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 – 2017-06-01T12:38:25.353

9Got it:

grep -lr "text pattern" ./ -s ; grep -lr "text pattern" [PATH OF PARENT] -s – CP3O – 2017-06-01T12:41:12.773

But this returns the contents of matching files, not the file details (name, path). @CP3O's suggestion works. – geotheory – 2018-11-21T10:55:02.097

31

use spotlight

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

don't forget to look at:

man mdfind

user46046

Posted 2010-07-13T09:42:08.147

Reputation: 901

Grep doesn't seem to parse .xlsx files, but this worked out fine. Another easy example: mdfind -onlyin . "searchtext" – FvD – 2015-09-02T11:44:50.260

2Will this work for files that spotlight doesn't index (i.e. files inside hidden folders, system config files, etc)? – Peter Berg – 2013-09-10T04:10:24.443

12

  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"

Anant Gupta

Posted 2010-07-13T09:42:08.147

Reputation: 221

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:

Using grep enter image description here

Using ack enter image description here

karthiks

Posted 2010-07-13T09:42:08.147

Reputation: 141

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

Posted 2010-07-13T09:42:08.147

Reputation: 129

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 – 2017-06-01T18:02:14.427

(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 Says 'Reinstate Monica' – 2017-06-01T19:01:18.990

@G-Man I edited the answer to show [PATH OF DIRECTORY] just incase someone unfamiliar wanted to know why I had put ./ – CP3O – 2017-06-02T09:38:45.913

@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 – 2017-06-02T09:39:42.300