How to find files with certain text in the Terminal

305,920

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

  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"

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:

Using grep enter image description here

Using ack enter image description here

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?

Share:
305,920

Related videos on Youtube

Svish
Author by

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, 2022

Comments

  • Svish
    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
      Admin almost 14 years
      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.
    • Admin
      Admin over 7 years
      Take a look at unix.stackexchange.com/a/37932/213832 this solution works well
  • MaxiFlaxiBoii
    MaxiFlaxiBoii almost 11 years
    Will this work for files that spotlight doesn't index (i.e. files inside hidden folders, system config files, etc)?
  • FvD
    FvD almost 9 years
    Grep doesn't seem to parse .xlsx files, but this worked out fine. Another easy example: mdfind -onlyin . "searchtext"
  • Ludovic Kuty
    Ludovic Kuty over 8 years
    I would suggest a man grep to discover all the wonderful options of grep
  • CP3O
    CP3O about 7 years
    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
    CP3O about 7 years
    Got it: grep -lr "text pattern" ./ -s ; grep -lr "text pattern" [PATH OF PARENT] -s
  • jpaugh
    jpaugh about 7 years
    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.
  • G-Man Says 'Reinstate Monica'
    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
    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
    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
    geotheory over 5 years
    But this returns the contents of matching files, not the file details (name, path). @CP3O's suggestion works.
  • Daniel Springer
    Daniel Springer almost 4 years
    How to hide "permission denied" results?