ParityCheck

June 14, 2007

Unix find command to find text in a file buried deep in subdirectories

Filed under: Linux, Unix — paritycheck @ 3:10 am

Something I learned long time ago and continually find it useful.  This is a way to use the Unix find command to search through files for a particular word, even files buried deep in subdirectories.  Great when trying to dig through Verilog RTL searching for the source of a signal.

Simply type the command below in the following format.

find [dir] -name [files] | xargs grep [text to search for]

For example,

find . -name “*.v” | xargs grep parity_check

or

find . -name “*.*” | xargs grep parity_check

If there are other UNIX how-tos you’d like to see, please feel free to let me know.  I’ll do my best to help out..

20 Comments »

  1. thank you so much for the information. it is great

    Comment by zara — December 24, 2007 @ 4:50 am

  2. Thank you for the information, very easy and very useful.

    Comment by Goyo — May 12, 2008 @ 8:01 am

  3. That’s great.

    Comment by Jiwen — June 25, 2008 @ 2:30 pm

  4. This was terribly useful for me today! Great Thanks !

    Comment by karthik — October 6, 2008 @ 2:04 pm

  5. Indeed useful. Thanks!

    Comment by silencer — November 13, 2008 @ 6:48 pm

  6. Real useful.. Appreciate the text.

    Comment by MP — December 9, 2008 @ 1:24 pm

  7. Thanks it is useful. But how do you avoid “permission denied” directory listing during the search?. I would like to see only the files which are matcing the given string.

    Comment by Ravi — January 2, 2009 @ 10:58 am

  8. It was a gr8 command i was searching for

    Comment by phani — January 21, 2009 @ 6:12 pm

  9. How do I find a list of file names that do not contain searched “text” ?

    Comment by Adey — January 30, 2009 @ 9:18 am

  10. You can do the same without using xargs:

    find [dir] -name [files] -exec grep -l “[text to search for]” {} \;

    The double quotes, brackets and the colon escaped with a slash are mandatory, without them it doesn’t work. You sould also add a filter “-type [d|f]” (directory or file) to speedup the search ;)

    Comment by Peregrino — March 3, 2009 @ 8:05 pm

  11. I want to search “temp” text in the whole web site (zone) of unix server.
    Which unix command should I use?
    The above command:
    find [dir] -name [files] -exec grep -l “[text to search for]” {} \;
    doesn’t work for me.
    I wrote:
    find / -name -exec grep -l “[temp]” {} \;
    Please advise.

    Comment by ppa108 — April 1, 2009 @ 2:10 am

  12. ppa108,

    go to starting directory where you want to begin search..

    then type the following

    find . -name “*” | xargs grep temp

    let me know how it goes..

    Comment by paritycheck — April 2, 2009 @ 9:51 pm

  13. I want to search “temp” text in the whole web site (zone) of unix server.
    Which unix command should I use?
    The above command:
    find [dir] -name [files] -exec grep -l “[text to search for]” {} \;
    doesn’t work for me.
    I wrote:
    find / -name -exec grep -l “[temp]” {} \;
    Please advise.

    Comment by ppa108 — April 1, 2009 @ 2:10 am

    =======================================================================

    You want to remove the brackets from around “temp”
    Earlier when it was stated that the brackets were mandatory, he meant the curly brackets: {}

    Comment by wizdom — April 4, 2009 @ 5:55 pm

  14. You also need a name to search for I believe

    Comment by wizdom — April 4, 2009 @ 5:56 pm

  15. Both command are great.

    A) find [dir] -name [files] | xargs grep [text to search for]
    B) find [dir] -name [files] -exec grep -l “[text to search for]” {} \;

    But there are some minor issue. Maybe I good enough to handle them.
    For (A), it got problem when there is space in the directory name.
    For (B), if it got circulated soft link, it will hang forever. (I can use ctrl-C to stop it, anyway.)

    Any expert got way to work around this. :)

    Comment by ray — July 14, 2009 @ 7:02 pm

  16. Thank you! This was such a time saver!!! :)

    Comment by Mike — August 5, 2009 @ 3:46 pm

  17. Some more info about unix find command

    Comment by scripter — August 21, 2009 @ 9:41 pm

  18. Hey it has saved my 3 hours dude :-)

    Comment by Arunkumar — September 24, 2009 @ 4:40 am

  19. Awesome!!!! You saved my time able to respond to a customer query in seconds. We were able to track the source of the issue and fix it.
    THANKS a MILLION

    Comment by Nethaji — November 5, 2009 @ 6:59 am


RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.