Unix search command

AgentEL

Golden Member
Jun 25, 2001
1,327
0
0
When I used Linux at work I used the command:

find . -name "*.c" | xargs grep <expression-to-find> | more

Now, that I changed jobs (and OSes), the same command doesn't work anymore. FYI, I am using cygwin.

What is another command that searches similar to the above? What it the above command does is find all files in the current directory and all subdirectories for all files with a .c extension. It then greps each file for the <expression-to-find> and returns the results.

This type of searching came in really handy and I could really use a similar command.

Any takers?
 

AgentEL

Golden Member
Jun 25, 2001
1,327
0
0
Originally posted by: n0cmonkey
I usuallly just use things like find . -name "*.c"-exec grep EXPRESSION {} \; | more

Thanks for the help. However, is there something missing? I got:

C:\<directory>>find . -name "*.c" -exec grep "msg" {} \;
find: missing argument to `-exec'

I know I didn't put the | more, but I don't think that's it.
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
Using find -exec will run grep for each individual file afaik. Just pipe it.

find . -name '*.c' | grep msg

Or

find . -name '*msg*.c'

and a recursive grep would be more appropriate if you were trying to find files whose contents contain certain patterns.
 

n0cmonkey

Elite Member
Jun 10, 2001
42,936
1
0
Here's what I got when I tried it my way (editted, obviously ;)):
n0c@dent:~$ find . -name "*.c" -exec grep "uck" {} \;
find: ./.libssl-test/lib/pkgconfig: Permission denied
/** gEEk-f*ck-khaled.c -- remote mirc < 6.11 exploit by blasty
* DKBTrace was originally written by David K. Buck.
* DKBTrace Ver 2.0-2.12 were written by David K. Buck & Aaron A. Collins.
* Please don't f*ck with 'timeval', or the digest we generate later for
/* Are we about to copy a chuck to the end of the buffer?
dprintf(idx, "%stuck exempt: %s\n", yn ? "S" : "Uns", s);
dprintf(idx, "%stuck %s exempt: %s\n", yn ? "S" : "Uns", chname, s);
dprintf(idx, "%stuck invite: %s\n", yn ? "S" : "Uns", s);
dprintf(idx, "%stuck %s invite: %s\n", yn ? "S" : "Uns", chname, s);
dprintf(idx, "%stuck ban: %s\n", yn ? "S" : "Uns", s);
dprintf(idx, "%stuck %s ban: %s\n", yn ? "S" : "Uns", chname, s);
#define sucknetword(x) ((x)+=2,((u_16bit_t) (((x)[-2] << 8) | ((x)[-1] << 0))))
#define sucknetshort(x) ((x)+=2,((short) (((x)[-2] << 8) | ((x)[-1] << 0))))
#define sucknetdword(x) ((x)+=4,((dword) (((x)[-4] << 24) | ((x)[-3] << 16) | \
#define sucknetlong(x) ((x)+=4,((long) (((x)[-4] << 24) | ((x)[-3] << 16) | \
/* Return the hash bucket number for id.
/* Return the hash bucket number for ip.
/* Return the hash bucket number for host.
qdatatype = sucknetword(c);
qclass = sucknetword(c);
datatype = sucknetword(c);
class = sucknetword(c);
ttl = sucknetlong(c);
rdatalength = sucknetword(c);
nuke_server("i-lines suck");
/* Pass the buck to Make_HandleUse to apply the rule. */

Piping the output of just a basic find to grep should also work, I just think -exec is more fun. Maybe it's a cygwin thing. I don't have a windows machine handy to play around with though.
 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
Some shells need the characters {} ; escaped, bash is one of them. You'd need to use it like this find . -name '*.c' -exec grep \{\} \;
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
Oh, I didn't read the initial post. :p I didn't see that he was looking for the contents of files.

find . -name \*.c -print0 | xargs -0 grep msg

or

find . -name \*.c | while read i; do grep msg "$i"; done

or find -exec. I don't like find -exec though :p
 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
IIRC -print0 is a GNU extention to find, while it'll work in cygwin because that runs GNU tools, it's not portable.
 

manly

Lifer
Jan 25, 2000
13,589
4,239
136
Originally posted by: BingBongWongFooey
and a recursive grep would be more appropriate if you were trying to find files whose contents contain certain patterns.
It's a heck of a lot easier than remembering the -exec syntax. :p
 

n0cmonkey

Elite Member
Jun 10, 2001
42,936
1
0
Originally posted by: Nothinman
Some shells need the characters {} ; escaped, bash is one of them. You'd need to use it like this find . -name '*.c' -exec grep \{\} \;

I believe you are wrong. Atleast, I didn't escape them, and it worked just the same.
 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
It seems that only the ; needs escaping, maybe that's been changed recently. I remember having to do that at some point...

Ah well either way the extra escapes don't hurt =)