Operating System - HP-UX
1829693 Members
8529 Online
109992 Solutions
New Discussion

Re: grep file in the subdirectory

 
O'lnes
Regular Advisor

grep file in the subdirectory

The 'grep' function can only use in the current directory, how to grep file into the subdirectory ? Thx.





Andy
4 REPLIES 4
Stefan Farrelly
Honored Contributor

Re: grep file in the subdirectory


To grep in all files in all subdirs;

grep */
or
grep /

Im from Palmerston North, New Zealand, but somehow ended up in London...
Christian Gebhardt
Honored Contributor

Re: grep file in the subdirectory

You can use "find":

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


This instruction searches in all subdirectories, in all files the pattern "pattern" (lower and upper cases)

Christian
steven Burgess_2
Honored Contributor

Re: grep file in the subdirectory

Hi

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/output_file.


Regards

Steve
take your time and think things through
O'lnes
Regular Advisor

Re: grep file in the subdirectory

It is OK, thx help.
Andy