Page 1 of 2 12 LastLast
Results 1 to 10 of 20

Thread: search recursively in a particular filename

  1. #1
    Join Date
    Aug 2012
    Beans
    597

    search recursively in a particular filename

    Hello,
    Situation : I want to search for the word, 'confedit' in all makefiles in my system using grep.

    I have tried :
    I know of grep -r
    I tried doing grep -r 'confedit' ./*Makefile 2>/dev/null
    but it doesn't return anything.

    The makefiles could be named as Makefile or makefile.
    So please give me a file pattern which checks both.

    Please advise,
    Thanks.

  2. #2
    Join Date
    Feb 2009
    Location
    Dallas, TX
    Beans
    6,840
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: search recursively in a particular filename

    Hi IAMTubby.

    As far as I know, if you omit the filename, it would search recursively. Unfortunately, it would look for all files.

    This may work:
    Code:
    find -type f -iname "makefile" -exec grep -H "confedit" '{}' \;
    where:
    • -type will filter only files
    • -iname will match the filename "makefile" (case insensitive).
    • -exec will execute a grep for each file found,
    • -H in grep will print the filename with the match line.

    Let us know how it goes.
    Regards.
    Last edited by papibe; May 16th, 2013 at 04:48 AM. Reason: added -H option

  3. #3
    prodigy_ is offline May the Ubuntu Be With You!
    Join Date
    Mar 2008
    Beans
    1,219

    Re: search recursively in a particular filename

    ^ This won't output filenames where matching lines are found. Need to add -print.

  4. #4
    Join Date
    Feb 2009
    Location
    Dallas, TX
    Beans
    6,840
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: search recursively in a particular filename

    Quote Originally Posted by prodigy_ View Post
    ^ This won't output filenames where matching lines are found. Need to add -print.
    Good catch!

    The grep's option -H would do that even better (filename in same line as matched line).

    Regards.

  5. #5
    Join Date
    Aug 2012
    Beans
    597

    Re: search recursively in a particular filename

    Quote Originally Posted by papibe View Post
    find -type f -iname "makefile" -exec grep -H "confedit" '{}' \;
    Thanks a lot papibe, it worked

    I have 2 questions though:
    1. I don't have to add the -print option,correct ? (I think it worked for me by adding -H alone, but since you said "good catch", I'm a bit confused)
    2. Can you tell me what '{}' \; means towards the end of the command ?

    Thanks.

  6. #6
    Join Date
    Feb 2009
    Location
    Dallas, TX
    Beans
    6,840
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: search recursively in a particular filename

    Quote Originally Posted by IAMTubby View Post
    1. I don't have to add the -print option,correct ?
    You don't have to.
    Quote Originally Posted by IAMTubby View Post
    2. Can you tell me what '{}' \; means towards the end of the command ?
    In the context of the -exec option the string '{}' represents the file that has been match. '\;' is the syntax to mark the end of the exec command.

    Does that help?
    Regards.

  7. #7
    Join Date
    Aug 2012
    Beans
    597

    Re: search recursively in a particular filename

    Quote Originally Posted by papibe View Post
    In the context of the -exec option the string '{}' represents the file that has been match. '\;' is the syntax to mark the end of the exec command.
    Thanks a lot papibe, it's clear now.

  8. #8
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    1,969
    Distro
    Kubuntu 12.10 Quantal Quetzal

    Re: search recursively in a particular filename

    Also:
    Code:
    shopt -s globstar
    grep -l "confedit" **/makefile
    (with the "globstar" option "**" expands recursively to all the directories in the tree).

  9. #9
    Join Date
    Aug 2012
    Beans
    597

    Re: search recursively in a particular filename

    Quote Originally Posted by papibe View Post
    find -type f -iname "makefile" -exec grep -H "confedit" '{}' \;
    papibe, I'm sorry to reopen this thread again.
    Ever since, you told me of the above command, I have been using it extensively with great success.

    But I would like to know how you can give a pattern to search within the makefile.
    To find all makefiles containing a reference to anything ending with .lo, I tried doing find -type f -iname "makefile" -exec grep -H "*.lo" '{}' \; but couldn't get the desired result.

    Surprisingly, the pattern works when I'm only using find, as in, to find all files ending with .lo, this works
    find ./ -name '*.lo' 2>/dev/null works and lists everything ending with .lo

  10. #10
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    1,969
    Distro
    Kubuntu 12.10 Quantal Quetzal

    Re: search recursively in a particular filename

    Replace the '*' by backslash
    Code:
    find -type f -iname "makefile" -exec grep -H "\.lo" '{}' \;
    


    "*.lo" looks for the string "*.lo", not for any string that ends with ".lo".

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

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