Results 1 to 5 of 5

Thread: Problem with find

  1. #1

    Default Problem with find

    Suddenly for me the "find" command is returning mostly a list of "Is a directory" errors.

    For example:
    cd /tmp
    mkdir a
    cd a
    mkdir b
    find . -exec grep foo {} \;
    grep: .: Is a directory
    grep: ./b: Is a directory

    I would expect "find" to return nothing in this case. Needless to say this in unhelpful when searching a large directory tree. Any help would be appreciated.

    More Info:
    :/tmp/a> which find
    /usr/bin/find
    :/tmp/a> ls -l /usr/bin/find
    -rwxr-xr-x 1 root root 180904 Dec 7 08:01 /usr/bin/find
    :/tmp/a> ls -l /bin/find
    lrwxrwxrwx 1 root root 13 Mar 1 13:14 /bin/find -> /usr/bin/find

    uname -a Linux lap1.site 3.4.28-2.20-desktop #1 SMP PREEMPT Tue Jan 29 16:51:37 UTC 2013 (143156b) x86_64 x86_64 x86_64 GNU/Linux

  2. #2
    Join Date
    Jun 2009
    Location
    Pennsylvania, USA
    Posts
    233

    Default Re: Problem with find

    helge59 wrote:

    >
    > Suddenly for me the "find" command is returning mostly a list of "Is a
    > directory" errors.
    >
    > For example:
    > cd /tmp
    > mkdir a
    > cd a
    > mkdir b
    > find . -exec grep foo {} \;
    > grep: .: Is a directory
    > grep: ./b: Is a directory
    >
    > I would expect "find" to return nothing in this case. Needless to say
    > this in unhelpful when searching a large directory tree. Any help would
    > be appreciated.
    >
    > More Info:
    > :/tmp/a> which find
    > /usr/bin/find
    > :/tmp/a> ls -l /usr/bin/find
    > -rwxr-xr-x 1 root root 180904 Dec 7 08:01 /usr/bin/find
    > :/tmp/a> ls -l /bin/find
    > lrwxrwxrwx 1 root root 13 Mar 1 13:14 /bin/find -> /usr/bin/find
    >
    > uname -a Linux lap1.site 3.4.28-2.20-desktop #1 SMP PREEMPT Tue Jan 29
    > 16:51:37 UTC 2013 (143156b) x86_64 x86_64 x86_64 GNU/Linux
    >
    >


    'find' finds everything: files, directories, symlinks, anything that can
    exist in the filesystem. If you only want to look at files, you need to
    tell it so.

    Code:
    find . -type f -exec grep foo \;
    Last edited by hcvv; 25-Mar-2013 at 07:30.

  3. #3
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    25,270

    Default Re: Problem with find

    Quote Originally Posted by helge59 View Post

    I would expect "find" to return nothing in this case.
    Your expectation is wrong.

    Please read carefully. It is not find that returns the error/warning, it is grep.
    And when you do not want grep to do this the use
    Code:
    grep -s foo
    or
    Code:
    grep foo 2>/dev/null
    or, as GeoBaltz advises, do not offer directories to grep by first filtering on "regular file".
    Henk van Velden

  4. #4

    Default Re: Problem with find

    Thanks for the replies and the "type -f" advice does eliminate the problem.

    But I've been using find the way I described for ~20 years with no problems.
    Everything from Solaris, HP-Unix, various Linuxes, and even cygwin.

    But it appears to me now that it is grep that has changed.

    On my Suse 12.1 desktop the following returns nothing:
    grep foo /tmp

    But with my new install of Suse 12.2 I see the following
    grep foo /tmp
    grep: /tmp: Is a directory

    Maybe they've tightened the rules.

  5. #5
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    25,270

    Default Re: Problem with find

    Quote Originally Posted by helge59 View Post
    Thanks for the replies and the "type -f" advice does eliminate the problem.

    But I've been using find the way I described for ~20 years with no problems.
    Everything from Solaris, HP-Unix, various Linuxes, and even cygwin.

    But it appears to me now that it is grep that has changed.

    On my Suse 12.1 desktop the following returns nothing:
    grep foo /tmp

    But with my new install of Suse 12.2 I see the following
    grep foo /tmp
    grep: /tmp: Is a directory

    Maybe they've tightened the rules.
    This is only a warning from grep. And yes, that warning wasn't always there which maybe an anoying change, But as you found out, even in earlier times you executed the grep on directories, only grep did not inform you of the fact.

    And it is always (was also in the past) a good thing to think about what find does when you type a find command.

    BTW read the man page of grep. That shows that another posibility is to use -d skip with grep. There are always many solutions to the same question in Unix/Linux.
    But I think that letting find only execute grep on things that matter is the best solution because it may prevent many executions of grep. Think of all the needless executions of grep you did in those ~20 years! That is the real problem!
    Last edited by hcvv; 26-Mar-2013 at 01:57.
    Henk van Velden

Posting Permissions

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