Find the answer to your Linux question:
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.
  1. #1
    Just 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:

    Code:
    find ./ -name *.c | xargs grep -n "$1"
    Here is what i thought would work to search all directories from ./

    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

    Code:
    find . *.c
    it prints out all the files and then the *.c ones.

    Any ideas ?

    Thanks

    Ringo

    PS Mandrake 10.1

  2. #2
    Linux 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"

  3. #3
    Linux Newbie
    Join Date
    Oct 2004
    Posts
    158
    Code:
    find . -name \*.c -exec grep -l "my search string" {} \;

  4. $spacer_open
    $spacer_close
  5. #4
    Linux 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"

  6. #5
    Just Joined!
    Join Date
    Oct 2005
    Location
    Cambridge, UK
    Posts
    7
    find -name '*.c' | xargs grep -n "$1"

    Works a treat.

    Thanks very much.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •