Why doesn't "find .|grep aword" work?

Mark J. Reed markjreed@gmail.com
Mon Jul 6 17:17:00 GMT 2009


On Mon, Jul 6, 2009 at 1:13 PM, km4hr wrote:
>
> Do pipes work in cygwin in the usual way?

Pipes on Cygwin work in the usual way.  You seem to misunderstand how
find and grep work.


> Why doesn't the following command works on HP Unix? Why not cygwin?
> I get no output from this command even though I'm sure the word "hello" is
> in some files.
>
> find .|grep "hello"

That will look for files whose *name* contains "hello".  The output of
'find" is a list of filenames.  You then run grep on that list looking
for the string "hello".

If you want to instead run grep "hello" on all the files found by
find, then the simplest change to your command is to introduce the
xargs ("transpose arguments") utility, like this:

find . | xargs grep hello

However, that will fail on files with strange names, and give you
errors about trying to grep directories, so what you really want is
this:

find . -type f -print0 | xargs -0 grep hello

None of this is Cygwin-specific.  It's just the way find and grep
work, on all UNIXlike systems.
-- 
Mark J. Reed <markjreed@gmail.com>

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple



More information about the Cygwin mailing list