2

Possible Duplicate:
Searching for string in files

Suppose I have a directory called Home and it is my current directory.

And in this home directory I have many other directories

directory1, directory2, etc.

How do I do a grep to find the occurrence of a word (say "AXN") in any of the files in all of these subdirectories (and their subdirectories?)

marked as duplicate by Gilles, manatwork, Michael Mrozek Mar 29 '12 at 15:41

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

7

You can use something like:

grep -r "AXN" .

Use -ir if you want it to be case-insensitive.

  • I remember I had achieved this before using a "." or "./", but I forgot the syntax. Do you know how it could be done that way? – CodeBlue Mar 28 '12 at 13:54
  • 1
    add a dot at the very end instead of the asterisk. using the dot is better, so it does not search binary files. – Uku Loskit Mar 28 '12 at 13:56
  • Dot will search binary files. Use the -I flag to skip them. – Jodie C Mar 28 '12 at 23:56
0

Try

find / -type f -exec grep -il AXN {} +
  • 2
    To avoid issues with whitespace, you should use find ... -print0 | xargs -0 ... or find ... -exec ... {} + (or {} \; if you want only one at a time). – Kevin Mar 28 '12 at 17:19
  • 2
    This may be functional but it involves launching a grep process for every file in the subdirectory. So, this may turn out be slower than grep -r – Gowtham Mar 28 '12 at 17:27
  • @Gowtham: like you, I prefer the xargs form, but the + on the end of that find command makes it construct a long command line like xargs does. i.e. {} + is different to plain {}. xargs has other advantages, including -P or --max-procs to run multiple commands in parallel (very nice on a multi-core system when combined with -n) – cas Jul 29 '12 at 11:07

Not the answer you're looking for? Browse other questions tagged or ask your own question.