Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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

5 Answers 5

up vote 6 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

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 test.py. 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 test.py . -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 ../test.py 
$ 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

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
The basic problem comes from redirecting stderr to /dev/null. If you're running this as a scripted job, you'd never be aware of any errors even if the script did find a file. You wouldn't be aware that it was perhaps missing files it couldn't access due to permissions issues such as directories that don't have read permissions set. Remove that and let the parent script discard stderr if it is appropriate. –  Jeff Ferland Feb 7 '13 at 22:15

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.