Techy: Recursive Find and Grep

November 18th, 2003 by Richy B. Leave a reply »

Here’s another little Linux “script snippet” I’ve needed over the past few days: basically, give it a directory/folder and it’ll search all the files in that directory for a set word, words, phrase (or even a string of characters). Very handy if you can tell that a server obviously knows about the domain, but try as you might – you can’t figure out which site the domain name belongs to (as it’s an add on domain and the dratted Ensim control panel won’t allow you to show it). Quick search on the Apache configuration folder and bingo!

I’m entering it here so a) if anybody else needs it, they can easily find it by searching for things such as “grep”, “recursive”, “search”, “linux”, “files” and “find”, and b) so I don’t have to bother remembering the syntax myself 😉

find [start folder name] -type f -exec grep ‘[text you are searching for]’ {} \; -print

Quick explanation:

  • “find XXX”: search the following folder(s)
  • “-type f”: for file(s)
  • “-exec”: then run the following command:
  • “grep ‘search text’ {} \;” use the grep function to search the file for the text
  • “-print” and then display the file name and details

This post is over 6 months old.

This means that, despite my best intentions, it may no longer be accurate.

This blog holds over 12 years of archived content - during that time, I may have changed my opinion of something, technology will have advanced (and old "best standards" may no longer be the case), my technology "know how" has improved etc etc - it would probably take me a considerable amount of time to update all the archival entries: and defeat the point of keeping them anyway.

Please take these posts for what they are: a brief look into my past, my history, my journey and "caveat emptor".

3 comments

  1. forkqueue says:

    From the grep manpage:

    -R, -r, –recursive
    Read all files under each directory, recursively;
    this is equivalent to the -d recurse option.

  2. Dan says:

    grep -r never works for me. Dunno why.

  3. Paulson Peter says:

    Yes, somehow -R doesn’t work for grep.
    But to do the same using the find shown above, you don’t require it. Find itself is having a “start folder name”. That is implicitly recursive.

Leave a Reply

gamy-dance
%d bloggers like this: