developer
developer

Reputation: 2050

Use grep to search for a string in files, include subfolders

i have to search for a particular text in files and for that im using grep command but it searches only in current folder.What i want is that using a single grep command i can search a particular thing in the current folder as well as in all of its sub folders.How can i do that???

Upvotes: 1

Views: 2089

Answers (3)

EdvardM
EdvardM

Reputation: 3092

And even more common is to use find with xargs, say

 find <dir> -type f -name <shellglob> -print0 | xargs grep -0 

where -print0 and -0, respectively, would use null char to separate entries in order to avoid issues with filenames having space characters.

Upvotes: 0

jim mcnamara
jim mcnamara

Reputation: 16399

POSIX grep does not support recursive searching - the GNU version of grep does.

find . -type f -exec grep 'pattern' {} \;

would be runnable on any POSIX compliant UNIX.

Upvotes: 2

aioobe
aioobe

Reputation: 421220

man grep says

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

Upvotes: 0

Related Questions