1831703 Members
2565 Online
110029 Solutions
New Discussion

find then pipe to grep

 
SOLVED
Go to solution
Cody Godines_1
Occasional Advisor

find then pipe to grep

Hello,
I'm trying to search for string in all subdirectories under a parent directory. At one time I remember that:
find /parent -type f | grep -in 'string'

worked on the command line, and I forgot what system I was on. Now that I'm on an HP9000 c240 Visualize, it doesn't produce anything. The best I can do at the command line is to grep the files in one directory at a time.
Now, if I script it, and set a shell variable to the 'find' output, there are "too many words" set to that variable because there are a lot of files to go through. I think that for me, scripting would be the way to go, so an immediate solution would be obtained if I knew how to 'find' only the files in the current directory without going into the subdirectories. Yes, 'll -1F' does it but I still then have to ignore the directories.
And yes, any solution is appreciated.
Thanks,
-Cody
10 REPLIES 10
Donald Kok
Respected Contributor

Re: find then pipe to grep

find /parent -name string
will do the trick
My systems are 100% Murphy Compliant. Guaranteed!!!
Hai Nguyen_1
Honored Contributor

Re: find then pipe to grep

Cody,

Try this:

# find -type f | xargs grep -li

which will list all the files containing the

Hai
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: find then pipe to grep

I would do something like this:

#!/usr/bin/sh

TARGET=${1}
shift
find . -type f | while read X
do
grep -q "${TARGET}" ${X}
STAT=${?}
if [[ ${STAT} -eq 0 ]]
then
echo "File: ${X}"
grep "${TARGET}" ${X}
fi
done

If it ain't broke, I can fix that.
James R. Ferguson
Acclaimed Contributor

Re: find then pipe to grep

Hi Cody:

If you are looking for all files in a directory and all of its subdirectories, with a particular string, then you want something like:

# find /parent -exec grep -l 'string' {} \;

'find' is *intended* to recursively descend all directories in its starting path.

Regards!

...JRF...
Rodney Hills
Honored Contributor

Re: find then pipe to grep

Cody,

To have "find" do only 1 level, Try-

cd yourdirectory
find . -type f -path "./*" -prune -print

You can then pipe the output to grep.

-- Rod Hills
There be dragons...
Leif Halvarsson_2
Honored Contributor

Re: find then pipe to grep

Hi

In this case it is sometimes better to collect all filenames in a temporary file first. Then you can grep for evrything you want and as many times you want witout traversing the filesystem again.

find /parent >tmpfile
grep string tmpfile
Wodisch_1
Honored Contributor

Re: find then pipe to grep

Hi Cody,

be careful there with "grep(1)" onto binary files (still type "f", though)...

My usual command for problems like that:

find /parent -type f -print |
while read name; do
case "$(file $name)" in
*text*) grep 'string' $name /dev/null ;;
esac
done | more

The additional file parameter "/dev/null" forces "grep" to print the file name, a colon, and then the matching line (and since /dev/null is always empty it won't disturb your output).

Just my $0.02,
Wodisch
Sean OB_1
Honored Contributor

Re: find then pipe to grep

You need to use the xargs command.

"find / -name -print | xargs grep

Shannon Petry
Honored Contributor

Re: find then pipe to grep

Just noticed too something that certain find commands are picky on.. you need to use the verb -print to see the names on stdout.
I have some older systems that do nothing if you dont give a -print
I.E. find /etc -name hosts
#
find /etc -name hosts -print
hosts
#

Other systems find commands will complain about a missing verb if -find or -exec are not given.

You can also use the -exec embedded in your command...
find / -exec /bin/grep -i -n string {} \;

Hope it helps
Shannon
Microsoft. When do you want a virus today?
Sean OB_1
Honored Contributor

Re: find then pipe to grep

Cody,

Just a reminder to give points out to those who answered your questions. It only takes a moment, and helps make the forums more useful to everyone.

Sean