Terminal - Search entire directory for keyword

20,927

Solution 1

You can use the recursive mode of grep:

grep -R pattern directory

ack (ack-grep on Ubuntu) and ag are also good for this.

Solution 2

cd ~/path/to/your/directory
grep -rwI KeywordCaseSensitive *

Explanateion:

-r search recursively
w search for a word only (i.e. surrounded by white space and/or punctuation)
I ignore binary files

You may consider option -i (ignore case).

Share:
20,927

Related videos on Youtube

Admin
Author by

Admin

Updated on September 18, 2022

Comments

  • Admin
    Admin almost 2 years

    I am working on a Ruby on Rails project so I have to manage multiple files. I think I made a syntax mistake somewhere but I'm not sure well. It would be awesome if I could search for a keyword through the entire project directory.

    I'm sure there's some command on Terminal, but I'm not sure what it is.

    • Avinash Raj
      Avinash Raj about 10 years
      did you want to find the files which contain a specific keyword?
  • terdon
    terdon about 10 years
    +1 but ack? You mean ack-grep? And what is ag? Did you mean agrep?
  • Volker Siegel
    Volker Siegel about 8 years
    I think it should be option -r (--recursive) instead of -R (--dereference-recursive), as -R would follow symlinks out of the project directory.