bug-gnu-utils
[Top][All Lists]
Advanced

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

Re: grep.... I know I am new to ubuntu but....


From: DAVE HITCHMAN
Subject: Re: grep.... I know I am new to ubuntu but....
Date: Thu, 9 Feb 2012 05:54:55 +0000 (UTC)

So the 'search in sub directories' flag doesn't work? Why does the shell try 
and fail to do the right thing? If it is going to 'process' one argument why 
does it not process them all? 
I will have to grab myself a proper Unix build but I'm damned sure it worked 
there before, it certainly works in things like cygwin.

----- Original Message -----
From: John Cowan <address@hidden>
To: DAVE HITCHMAN <address@hidden>
Cc: address@hidden
Sent: Thu, 09 Feb 2012 05:42:36 -0000 (UTC)
Subject: Re: grep.... I know I am new to ubuntu but....

DAVE HITCHMAN scripsit:

> I have a directory structure, inside that are a number of .c source
> files. I want to find which of those files contain certain strings.
> In a DOS window using grep I would type
>       grep "mystring" -i *.c
> this would result in a list of .c files containing the string.

The reason this doesn't work in Linux (or other Unix-style systems)
is that "*.c" means "files ending in '.c' in the current directory";
it is interpreted by the shell, not by grep itself.  Since there are no
such files, the literal string '*.c' is passed to grep, which (since it
knows nothing of wildcards) says there is no file with that name.

One way to solve this problem is to use two commands connected by a pipe
symbol, one to search the whole directory tree for files of the correct
name, and one to apply grep to those files.  We can write that idea thus:

        find -name '*.c' | xargs grep 'mystring'

This will not work if any of your filenames have spaces in them, and
then you need to use this form instead:

        find -name '*.c' -print0 | xargs -0 grep 'mystring'

That causes 'find' to transmit the filenames that it finds over the
pipe with NUL characters separating them rather than spaces, and 'xargs'
to look for that NUL rather than whitespace.

> Even trying the various bizarre combinations of finds and execs that
> I have seen on the web I can't seem to do this very simple task. If
> Microsoft can have managed to provide find in files and various other
> similar things for over 20 years isn't it time I could do the same on
> a linux system without the need to spend 10 hours searching endless
> uninformative websites all lying about what works?

"If the blind lead the blind, both shall fall into a pit." (Matt. 15:13)

-- 
John Cowan                                address@hidden
I amar prestar aen, han mathon ne nen,    http://www.ccil.org/~cowan
han mathon ne chae, a han noston ne 'wilith.  --Galadriel, LOTR:FOTR




reply via email to

[Prev in Thread] Current Thread [Next in Thread]