[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: using find with grep




I have an expression that searches within files
of a particular type and displays the output.
But I would ALSO like it to display the directory
for each find occurence.
The command below works, but won't give me
the directory.

find /home -name *.txt -exec grep searchstring {} \;
Try
find /home -name "*.txt" -exec grep --with-filename searchstring {} \;

If that doesn't do it then this surely will
find /home -name "*.txt" -exec grep searchstring {} /dev/null \;

Another very useful idiom  is:

find /home -name "*.txt" | xargs grep searchstring

This will probably be a bit faster because grep will be invoked as few times as possible (probably once), saving the overhead of all those execs.

find /home -name "*.txt" | xargs grep -l searchstring

will only report the names of the matching files.

An obscure case: if files and directories may contain spaces, you would need to do:

find /home -name "*.txt" -print0 | xargs -0 grep searchstring

Kevin Murphy




Reply to: