Find the answer to your Linux question:
Results 1 to 9 of 9
Hi All I'm having a problem grep'ing files for a certain string on my server. The command I'm using is: find . -name "*.php" -o -name "*.pl" -o -name "*.cgi" ...
  1. #1
    Just Joined!
    Join Date
    Apr 2011
    Location
    Oxfordshire, UK
    Posts
    4

    Grep (underscore?) Problem

    Hi All

    I'm having a problem grep'ing files for a certain string on my server. The command I'm using is:

    find . -name "*.php" -o -name "*.pl" -o -name "*.cgi" -o -name "*.pm" -o -name "*.xml" -print0 | xargs -0 grep -il "db_name"

    So, as you can see, I want to search all php|pl|cgi|pm|xml files under the current directory tree and list the path/filenames that contain the string "db_name".

    I've been using this command format for other strings successfully, but I suspect the underscore is throwing this one out as no matches are returned even though I know there is at least one file containing that string.

    I've tried enclosing the string in single quotes instead of double, escaping the underscore with a backslash and replacing the underscore with a full stop (regular expression style) but to no avail.

    I haven't found any answers yet by googling, so I wonder if someone here can quell my frustration? Please go easy on me though, as I have limited grasp of Linux in general.

    Thanks
    Bigus

  2. #2
    Linux Newbie Ziplock's Avatar
    Join Date
    Jan 2009
    Location
    Adelaide
    Posts
    169
    I can't see any reason why the grep wouldn't work. I gather you have tried just the grep command on a test file that definitely contains the string you are looking for? If the grep works on a test file, then it isn't finding the string

  3. #3
    Just Joined!
    Join Date
    Apr 2011
    Location
    Oxfordshire, UK
    Posts
    4
    Yes, regular grep'ing works.. I've kind of found what the problem was. That is, if I remove the '-print0' and '-0' it does find 'db_name' in all the files, so this works:

    find . -name "*.php" -o -name "*.pl" -o -name "*.cgi" -o -name "*.pm" -o -name "*.xml" | xargs grep -il "db_name"

    Trouble is, in amongst the list of files found, I get lines like this:

    grep: example/gallery1/gallery.xml: No such file or directory

    I gather '-print0' and '-0' are something to do with dir/file names with spaces in but I can't imagine why they would be affecting finding the string inside the files.

    Is there another way of finding files with reliable results?

  4. #4
    Linux Newbie Ziplock's Avatar
    Join Date
    Jan 2009
    Location
    Adelaide
    Posts
    169
    OK, this is a problem with filenames/directories with spaces in them.

    The workaround in this instance is, instead of piping you find output to xargs, use the exec flag on the find command:

    Code:
     find ... -exec grep -il 'db_name' '{}' ';'
    Using the quotes around the {} identifier should nullify those spaces for you.

  5. #5
    Just Joined!
    Join Date
    Apr 2011
    Location
    Oxfordshire, UK
    Posts
    4
    Thanks for your continued assistance, however that didn't work, i.e this came up with no matches:

    Code:
    find . -name "*.php" -o -name "*.pl" -o -name "*.cgi" -o -name "*.pm" -o -name "*.xml" -exec grep -il 'db_name' '{}' ';'
    However, these *did*:

    Code:
    find . -name "*.php" -exec grep -il 'db_name' '{}' ';'
    find . -name "*.php" -print0 | xargs -0 grep -il "db_name"
    Therefore it looks like it's the multiple files types / -o that caused both different search methods to fail.

    Is there another way of specifying multiple files types, or maybe there's some kind of grouping syntax for the "-name xx -o -name xxx" block?
    Last edited by Bigus; 04-13-2011 at 03:59 PM.

  6. #6
    Just Joined!
    Join Date
    Apr 2011
    Posts
    3

    Thumbs down darrr

    Not to be a complete idiot but how on earth did I not know
    about the -a switch?
    LoL I have been using find -type d -exec grep "find-this" {} ;\ forever

  7. #7
    Linux Newbie Ziplock's Avatar
    Join Date
    Jan 2009
    Location
    Adelaide
    Posts
    169
    OK, so the following is an alternative to the long string of -o -name options:

    Code:
    find . -iregex '.*php$\|.*cgi$' -exec grep -il 'db_name' '{}' ';'
    Add more extensions with the \| between them as required.

  8. #8
    Just Joined!
    Join Date
    Apr 2011
    Location
    Oxfordshire, UK
    Posts
    4
    YES! That works!

    It kinda feels like I've just climbed a mountain to get to a solution for this simple task, albeit Ziplock has pulled me up there!

    Many thanks for your help Zip

    Regards
    Bigus

  9. #9
    Linux Newbie Ziplock's Avatar
    Join Date
    Jan 2009
    Location
    Adelaide
    Posts
    169
    Glad I could help Bigus! Find is useful and flexible, but complicated too

Posting Permissions

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