Find exit with success with non executable directory


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Find exit with success with non executable directory
# 1  
Old 05-09-2016
Find exit with success with non executable directory

Hi there,

I'm quite surprised by the following behavior of find.
Code:
$ 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
Code:
$ 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

Last edited by chebarbudo; 05-10-2016 at 04:58 AM.. Reason: more details
# 2  
Old 05-09-2016
Quote:
Originally Posted by chebarbudo
Hi there,

I'm quite surprised by the following behavior of find.
Code:
$ find                  # I have a directory and a file in it
.
./dir
./dir/file
$ chmod ugo=x dir/
$ find; echo $?         # If the directory is not readable, find fails because it cannot explore it
.
./dir
find: `./dir': Permission denied
1
$ chmod ugo=r dir/      # If the directory is not cd-able, find does NOT fails although it cannot explore it
$ find; echo $?
.
./dir
0

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.
# 3  
Old 05-10-2016
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:
Code:
$ 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:
Code:
$ 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

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

No such file or directory for existing executable

Hi, I have a very puzzling problem. I have a binary file, it is executable but it says "No such file or directory" when I try to run it : root@debian:~# ls -l bmcfwul -rwxr-xr-t 1 root root 762808 Mar 11 2010 bmcfwul root@debian:~# ./bmcfwul -su: ./bmcfwul: No such file or directory... (9 Replies)
Discussion started by: chebarbudo
9 Replies

2. UNIX for Dummies Questions & Answers

Run executable in one directory and then move to another successively

Hello, I have several hundred subdirectories which contain input files for a binary executable. I need to get into each of the subdirectories, run the executable and then move to the next one and repeat the process. What is the best way to do this? Arbitrarily my file structures look like... (3 Replies)
Discussion started by: Gussifinknottle
3 Replies

3. Shell Programming and Scripting

Finding the executable files of a directory using Grep

Hi guys, Can you please help me print all the executable files of a directory(in this case /home) using grep? All i know is that this command should do it but it doesnt... ls -l ~ | grep -..x it shows me the following mesage grep: invalid option -- '.' Χρήση: grep ... ΥΠΟΔΕΙΓΜΑ ... (3 Replies)
Discussion started by: jimas13
3 Replies

4. Shell Programming and Scripting

Exit shell after setting executable to run?

Hi, I have an executable file that has a rather long and tedious process to complete. How would I launch the executable using the shell, and then exit the shell while leaving the executable to run in the background? (1 Reply)
Discussion started by: pcwiz
1 Replies

5. Shell Programming and Scripting

How to tar all executable file in a directory

Dear all I want to create a tar file which contains all executable files in a specific directory cd /appl/home/ file some_exe some_exe: 64-bit XCOFF executable or object module not stripped My current approach is to tar it one by one tar -cvf test.tar exefile1 tar -uvf test.tar... (2 Replies)
Discussion started by: on9west
2 Replies

6. Shell Programming and Scripting

script to find latest executable in particular directory and start that particular ex

i have a directory in which there are executable files and these files are added at runtime. now i need a shell script which will be called at a certain interval. this shell script should find the latest executable file in that directory and start that executable. care should be taken that once the... (6 Replies)
Discussion started by: kvineeth
6 Replies

7. Shell Programming and Scripting

how to exit with success if the file is not found

I want to put a exit if there is no file found. In the directory I can have multiple txt files with EPISGCHGS as prefix or maynot have one. When I am trying the following it is erroring if there is no text file with the EPISGCHGS as prefix. for file in EPISGCHGS*.txt do cat $file >... (2 Replies)
Discussion started by: apps_user
2 Replies

8. UNIX for Dummies Questions & Answers

Executable directory general question

This may be a dumb question (but this is the UNIX for Dummies Q&A forum :) But I'm wondering, what is the purpose of a directory being given execute permissions? I can't execute a directory, only files, right? If I make a directory executable, the files inside still won't execute unless I give... (1 Reply)
Discussion started by: FredSmith
1 Replies

9. UNIX for Dummies Questions & Answers

Where can I find a list of exit codes? (Exit code 64)

I'm receiving an exit code 64 in our batch scheduler (BMC product control-m) executing a PERL script on UX-HP. Can you tell me where I can find a list of exit codes and their meaning. I'm assuming the exit code is from the Unix operating system not PERL. (3 Replies)
Discussion started by: jkuchar747
3 Replies

10. Programming

How to get exit value of an executable that gets called from function?

How to get exit value of an executable that gets called from function? I have an executable called “myexec” which returns 0 on success and different values for different fail scenarios. I need to call this (myexec) executable from “myprog()” function of other executable and get the exit value... (1 Reply)
Discussion started by: sureshreddi_ps
1 Replies
Login or Register to Ask a Question