1819791 Members
3218 Online
109607 Solutions
New Discussion юеВ

grep in subdirectories

 
SOLVED
Go to solution
Ninad_1
Honored Contributor

grep in subdirectories

hello friends,
i have been working on this question for quite some time - but havent found an answer. pls can anyone suggest a solution -
when we use the grep command - it searches for text in the present working directory - but if i want to search the pattern ( text ) also in the files in subdirectories and the sub -sub directories then how do i do that ?
this could be very useful i think for many.
hope to hear from someone very soon.

Ninad
16 REPLIES 16
Helen French
Honored Contributor
Solution

Re: grep in subdirectories

what about combining grep with find command:

# cd dir_path
# find . -type f -print | xargs grep xyz

This will check all sub-sub directories under dir_path for the string xyz
Life is a promise, fulfill it!
Pete Randall
Outstanding Contributor

Re: grep in subdirectories

Ninad,

Use find:

find /directory_name -exec grep -l "search for this text" {} \;

That should do it,
Pete

Pete
eran maor
Honored Contributor

Re: grep in subdirectories

Hi

you can find this option in the man page
of the command grep
just check the man page - after all the option

Search all files in the current directory for the string xyz:

grep xyz *

Search all files in the current directory subtree for the string xyz,
and ensure that no error occurs due to file name expansion exceeding
system argument list limits:

find . -type f -print |xargs grep xyz
love computers
H.Merijn Brand (procura
Honored Contributor

Re: grep in subdirectories

Use GNU grep (http://hpux.cs.utah.edu/hppd/hpux/Gnu/grep-2.5/) with -r option
Enjoy, Have FUN! H.Merijn
Wodisch
Honored Contributor

Re: grep in subdirectories

Hi Ninad,

actually just "find" and "grep" is too dangerous!
So usually I do it that way:

cd /your/start/directory
find . -type f -print |
while read name; do
case "$(file $name)" in
*text*) grep "YOUR-PATTERN-HERE" $name /dev/null ;;
esac
done | more

Just my $0.02,
Wodisch
Vincent Fleming
Honored Contributor

Re: grep in subdirectories

Wodisch,

It's a resource hog, and should be used lightly (it spawns a "grep" for every file), but I've never heard it called dangerous.

Please, do tell why!
No matter where you go, there you are.
Rodney Hills
Honored Contributor

Re: grep in subdirectories

I believe that the gnu version of "grep" has a -R option to do a recursive search within subdirectories.

You can get it from the software porting and archive centre.

-- Rod Hills
There be dragons...
Alan Riggs
Honored Contributor

Re: grep in subdirectories

Vincent,

I hesitate to answer for Wodisch, but the obvious problem with piping the output of find directly to grep is that it you encounter compiled binaries, special files, etc. you will hose your terminal session and fill your screen/output file with garbage. Wodisch's case statement tests for the string "text" in the output of the file [FILENAME] command, which prevents such ill-behaved activity.

I don't know that I would call it dangerous, but it is certainly annoying.
steven Burgess_2
Honored Contributor

Re: grep in subdirectories

Hi

Here is your answer

find /var -name '*' -exec grep -i -l '15AUG00 16:38' {} \; > /tmp/output_file

This will find all files contained within the /var directory (and any subdirectories). Notice that the wildcard * is enclosed in single quotes. The output of the find is sent to a grep with a -i (case insensitive) and -l (ell) switch. The -l swich is necessary as this causes the grep to return the name of the file in which the search text was found (as opposed to simply returning a copy of the line itself).

In the above example, I have searched for the string '15AUG00 16:38' (note the single quotes again around the string) and sent the output to a file called /tmp/outpu_file.


Regards

Steve
take your time and think things through
Gary Yu
Super Advisor

Re: grep in subdirectories

How about grep "some_pattern" `find . -name "*" -print`

notice the back quote
H.Merijn Brand (procura
Honored Contributor

Re: grep in subdirectories

Alan, binary output will - by default - not be issued when using GNU grep. You need to pass -a to get that.
Enjoy, Have FUN! H.Merijn
Arockia Jegan
Trusted Contributor

Re: grep in subdirectories

Hi Ninad,

Use "find /directory -type f -exec grep -l {} \;


Also you can use "grep *" from the parent directory.

Replace the pattern with the string you are looking for.
Vincent Fleming
Honored Contributor

Re: grep in subdirectories

Alan,

I don't know if I would call it dangerous, either... which is why I asked.

Wodisch, now I'm really curious!
No matter where you go, there you are.
Jordan Bean
Honored Contributor

Re: grep in subdirectories


find . -type f | perl -ne 'chomp; print "$_\n" if -T;' | xargs grep -l pattern

Ninad_1
Honored Contributor

Re: grep in subdirectories

hi again,
thanks everyone for their very prompt and useful reply.
the grep -l option shows only the filenames and not the line containing the pattern - but still it serves the purpose.
and procura - i will definitely try the gnu option , thanks for the link.

Ninad
Mark Landin
Valued Contributor

Re: grep in subdirectories

The "-exec" version of the suggestions creates a grep process for each file passed to it. The "xargs" version spawns one grep process, and sends it all the filenames passed to it. From a process standpoint, the xargs version is probably better.