≡ Menu

Linux / UNIX Recursively Search All Files For A String

How do I recursively search all text files for a string such as foo under UNIX / Linux / *BSD / Mac OS X shell prompt?

You can use grep command or find command as follows.

grep command: Recursively Search All Files For A String

cd /path/to/dir
grep -r "word" .

grep -r "string" .
Ignore case distinctions:
grep -ri "word" .

To display print only the filenames with GNU grep, enter:
grep -r -l "foo" .
You can also specify directory name:
grep -r -l "foo" /path/to/dir/*.c

find command: Recursively Search All Files For A String

find command is recommend because of speed and ability to deal with filenames that contain spaces.
cd /path/to/dir
find . -type f -exec grep -l "word" {} +
find . -type f -exec grep -l "seting" {} +
find . -type f -exec grep -l "foo" {} +
Older UNIX version should use xargs to speed up things:
find /path/to/dir -type f | xargs grep -l "foo"
It is good idea to pass -print0 option to find command that it can deal with filenames that contain spaces or other metacharacters:
find /path/to/dir -type f -print0 | xargs -0 grep -l "foo"

{ 17 comments… add one }

  • rjbcollege May 27, 2010, 10:08 am

    That is fantastic, a command that says it does exactly what I’m looking for.

    Unfortunately, when I copy and paste the example to recursivle search for files containing a string:
    find . -type f -exec grep -l “word” {} +

    in to my linux session I get a set of error message lines which all say this:

    find: grep: Argument list too long

    To fix this, simply add the following to hide any such error messages by oplacing them in the trash:

    find . -type f -exec grep -l “word” {} + 2>>/dev/null

  • Pete Shore July 25, 2010, 10:51 am

    Perfect, had to search a joomla install for all occurrences of a string, used grep as i remember if from my uni days, thanks for the post.


  • lanh May 14, 2011, 5:37 pm

    Thank you this was very useful for debugging!

  • Radhakrishnan July 12, 2011, 7:06 am

    This tutorial is very useful .

  • Fernando Lopez Jr. October 2, 2011, 1:24 am

    it won’t work if you are trying to search for:

    grep -r “test!!!” .

    you need to use the single quotations. like:

    grep -r ‘test!!!’ .

  • jinil February 15, 2012, 4:51 am

    How can I recursively search all files for 2 strings?
    I mean, files containing both strings..

    Thanks :)

    • Ben May 17, 2015, 12:28 am

      You can pipe one grep into another.

  • analytical February 21, 2012, 3:28 pm

    Thanks, changed my default web directory somewhere and now I don’t know where. This will help.

  • mica March 30, 2012, 3:40 pm

    Would you let me know how to add a grep -v to the search , so i can supress some unwanted files?


  • Ken April 6, 2012, 10:17 pm

    This is indeed great. Now I wanted to shortcut this expression by putting it into my .bash_profile file as an alias but I can’t get that too work. I’ve tried a lot of variations on this theme:

    alias f=”find . -type f -exec grep -l $1 {} +”

    but then when I type something like “f foobar” it responds with “find: foobar: unknown option”. I’m sure this is stupidly easy but I’ve tried enough variations I thought I’d ask for some help.

    • nixCraft April 7, 2012, 10:24 am

      You can not pass args to alias. Use bash shell function:

      f(){ find . -type f -exec grep -l $1 {} + ; }

      Run it as:

      f foobar

      Hope this helps!

  • Ken April 10, 2012, 4:55 pm

    Great thanks Vivek!

  • Arsie May 28, 2012, 3:23 am

    Thanks for this information, I was able to get the file I am looking for on my box.

  • CaptSaltyJack May 14, 2013, 3:29 am

    Actually, using find to grep files is way slower than using grep -r. Try it, go into a folder with a whole bunch of files (hundreds, if not more), and run:

    date ; find . -type f -exec grep somestring {} \; ; date

    and then:

    date ; grep -r somestring . ; date

    The first operation took me about 10 seconds. The second one took about 3-4 seconds.

  • perfect tutorial February 26, 2014, 5:01 am

    Great article! Just another reason why *nix should be embraced for ever.

  • Joshua Pinter March 21, 2014, 7:44 pm

    Also, it’s helpful to use the “-n” flag to show the line number of the file the text is on.

  • Igor September 24, 2015, 7:38 pm

    grep -ri “word” .
    grep -ri -l “word” .
    thanks a lot.

Leave a Comment

   Tagged with: , , , , , , , , ,