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

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.


find . -type f -exec grep foo \;

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

grep -s foo

or

grep foo 2>/dev/null

or, as GeoBaltz advises, do not offer directories to grep by first filtering on “regular file”.

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. :wink:

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!