Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

when I perform

  find /tmp  -name something 

find command not find the something word under /tmp

  echo $?

I get $?=0

it's OK

but how to enable Exit status diff then 0 when find command not find the something word?

share|improve this question
up vote 14 down vote accepted

Here is a one-liner that I believe does what you want:

find /tmp -name something | egrep '.*'

The return status will be 0 when something is found, and non-zero otherwise.

If you also need to capture the output of find for further processing, then SvenW's answer has covered that.

share|improve this answer
The trailing * on the egrep regex is completely redundant. Since you are not using egrep regular expression syntax, plain old grep might be a few microseconds faster. – tripleee Oct 30 '15 at 13:03

It's not possible. Find returns 0 if it exits successfully, even if it didn't find a file (which is a correct result not indicating an error when the file indeed doesn't exist).

To quote the find manpage


find exits with status 0 if all files are processed successfully, greater than 0 if errors occur. This is deliberately a very broad description, but if the return value is non-zero, you should not rely on the correctness of the results of find.

Depending on what you want to achieve you could try to let find -print the filename and test against it's output:

MYVAR=`find . -name "something" -print`
if [ -z "$MYVAR" ]; then
    echo "Notfound"
   echo $MYVAR
share|improve this answer

It is not only find that returns the exit status codes as zero when it successful. In unix what ever the command you execute, if its succeeds then it returns the exit status as zero.

share|improve this answer

Here's a little script I called It improves upon other methods posted in that it will return an error code if one is set, and it additionally set one if find didn't list any files:

from subprocess import Popen
import sys

p = Popen(['find'] + sys.argv)
out, err = p.communicate()
if p.returncode:
if not out:

Here's the command-line output:

$ python . -maxdepth 1 -name notthere
$ echo $?
$ find . -maxdepth 1 -name notthere
$ echo $?
$ find . -failedarg
find: unknown predicate `-failedarg'
$ echo $?

Then, for a result where find had errors but found files:

$ ls -lh
$ d---------  2 jeff users   6 Feb  6 11:49 noentry
$ find .
find: `./noentry': Permission denied
$ echo $?
$ find . | egrep '.*'
find: `./noentry': Permission denied
$ echo $?
python ../ 
$ echo $?

Then, if you want the list of files you can make use of -print 0 passed to find and split the out variable on nulls, or you can just add a print statement for it.

share|improve this answer

Having just found this question whilst trying to find my way to solve a problem with Puppet (change permissions on folders under a directory but not on the directory itself), this seems to work:

! test -z $(find /tmp -name something)

My specific use case is this:

! test -z $(find /home -mindepth 1 -maxdepth 1 -perm -711)

Which will exit code 1 if the find command finds no files with the required permissions.

share|improve this answer

I feel that this is the most concise and direct method:

test `find /tmp/ -name something -print -quit 2>/dev/null`
share|improve this answer
In this case, if find had an error it would print the result to /dev/null, return a non-zero exit, and then have that fixed to 0 by the test command. – Jeff Ferland Feb 6 '13 at 19:24
I don't believe that's true. Please describe your example. If there is an error and find exits, then the string will be empty and test will return non-zero. – danorton Feb 7 '13 at 18:10
I'm not sure what your example might be, but I accidentally omitted -print -quit, which might address your concern. – danorton Feb 7 '13 at 18:19
Perhaps the other person who downvoted (presumably for the same reason) can provide a contrary example? – danorton Feb 7 '13 at 22:04
Okay, you just don't like my style. For my use cases, any errors that find might report are either noise or so serious that they'll show up elsewhere. In any case, this method correctly answers the ultimate question: "Is there an accessible file in the path by the given name." An error will return a correct answer of "No". If I want to find out why not, I can add more complex code that answers questions not originally posed here. – danorton Feb 7 '13 at 22:31

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.