Find exit with success with non executable directory

Hi there,

I'm quite surprised by the following behavior of find .

$ find -ls                  # I have a directory and a file in it
  8145    4 drwxr-xr-x   3 me me     4096 May 10 09:36 .
    87    4 drwxr-xr-x   2 me me     4096 May 10 09:36 ./dir
    88    0 -rw-r--r--   1 me me        0 May 10 09:36 ./dir/file
$ chmod 111 dir/
$ find -ls; echo $?         # If the directory is not readable, find fails because it cannot explore it
  8145    4 drwxr-xr-x   3 me me     4096 May 10 09:36 .
    87    4 d--x--x--x   2 me me     4096 May 10 09:36 ./dir
find: `./dir': Permission denied
1
$ chmod 444 dir/
$ find -ls; echo $?         # If the directory is not cd-able, find does NOT fail with an error although it fails to find the file in it
  8145    4 drwxr-xr-x   3 me me     4096 May 10 09:36 .
    87    4 dr--r--r--   2 me me     4096 May 10 09:36 ./dir
0

I'm using a fresh install of Debian 8 jessie

$ uname -a; cat /etc/debian_version
Linux debian 3.16.0-4-amd64 #1 SMP Debian 3.16.7-ckt20-1+deb8u4 (2016-02-29) x86_64 GNU/Linux
8.3

How can I make sure find fails if it cannot list all files and folders?

Thanks in advance

Santiago

Those are strange results. What operating system are you using?

Note that the results I would expect from a readable, unsearchable directory vary depending on whether or not there are any files (other than . and .. ) in that directory. The find utility can find all of the files in a directory even if it is unsearchable, so if there aren't any files in that unsearchable directory, there is no reason for find to fail. But, if files are present in an unsearchable directory, find won't be able to stat() those files to determine if they are directories that also need to be searched (so it should fail).

But, if a directory is unreadable, the find utility can't determine whether or not there are any files in that directory (so it should fail).

The BSD-based find utility on OS X behaves as I expect. I don't have other versions of find readily available to test against my expectations.

Hi Don Cragun,

Note that there is indeed a file in my directory and that find fails to find it if it lacks the x permission on the directory. I've updated my initial post to make it clearer. On the other hand, ls will find the file even if it fails to stat it:

$ find -ls
  8145    4 drwxr-xr-x   3 me me     4096 May 10 09:36 .
    87    4 dr--r--r--   2 me me     4096 May 10 10:13 ./dir
$ ls -l dir/
ls: cannot access dir/file: Permission denied
total 0
-????????? ? ? ? ?            ? file

I've also noted that find starts failing as soon as there is a directory inside the unsearchable directory:

$ sudo rm -rf dir/
$ mkdir dir; touch dir/file; chmod 444 dir; find; echo $?
.
./dir
0
$ sudo mkdir dir/dir2; find; echo $?
.
./dir
find: `./dir': Permission denied
1