Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?)

share|improve this question

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.

2 Answers 2

up vote 5 down vote accepted

You can use something like:

grep -r "AXN" .

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

share|improve this answer
Ok. Thank you very much for the help. –  CodeBlue Mar 28 '12 at 13:52
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
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
Ok Thanks @Uku Loskit. –  CodeBlue Mar 28 '12 at 14:15
Dot will search binary files. Use the -I flag to skip them. –  JodieC Mar 28 '12 at 23:56


find / -type f -exec grep -il AXN {} +
share|improve this answer
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
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
Thanks guys, I did't think of performance. my fault :) –  mibzer Mar 28 '12 at 21:23
@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.