Results 1 to 5 of 5
Hi,
Sorry for my numpty questions but here goes:
Code:
find ./ -name *.c | xargs grep -n "$1"
Here is what i thought would work to search all directories ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 10-11-2005 #1Just Joined!
- Join Date
- Oct 2005
- Location
- Cambridge, UK
- Posts
- 7
Trying to do a script that will grep on *.c files using find
Hi,
Sorry for my numpty questions but here goes:
Here is what i thought would work to search all directories from ./Code:find ./ -name *.c | xargs grep -n "$1"
but i get the error:
find: paths must precede expression
Usage: find [-H] [-L] [-P] [path...] [expression]
Surely the ./ is preceding the expressions ?
Also, if i do
it prints out all the files and then the *.c ones.Code:find . *.c
Any ideas ?
Thanks
Ringo
PS Mandrake 10.1
- 10-12-2005 #2Linux Engineer
- Join Date
- Apr 2005
- Location
- Buenos Aires, Argentina
- Posts
- 908
Why don't you just use "." instead of "./"? It works that way, doesn't it?
serzsite.com.ar
"All the drugs in this world won\'t save you from yourself"
- 10-12-2005 #3Linux Newbie
- Join Date
- Oct 2004
- Posts
- 158
Code:find . -name \*.c -exec grep -l "my search string" {} \;
- 10-12-2005 #4Linux User
- Join Date
- Jul 2004
- Location
- Poland
- Posts
- 368
A comment to the previous post: the error is probably because *.c gets expanded by shell and the resulting files are passed to find, which doesn't expect any file arguments at its end (roughly speaking). So you have to block shell from expanding (i.e. substituting all c files for *.c) so that this wildcard pattern gets passed to find. My way of doing this would be (btw current directory is default when you omit path, so . or ./ is not necesary):
Code:find -name '*.c' # plus the rest
"I don't know what I'm running from
And I don't know where I'm running to
There's something deep and strange inside of me I see"
- 10-12-2005 #5Just Joined!
- Join Date
- Oct 2005
- Location
- Cambridge, UK
- Posts
- 7
find -name '*.c' | xargs grep -n "$1"
Works a treat.
Thanks very much.


Reply With Quote
