Linux Find Command – How to Make `find` Return Non-0 When No Matching Files Are Found

find

Even when /tmp has no file called something, searching for it with find will return 0:

  $ find /tmp -name something 
  $ echo $?
  0

How can I get a non-zero exit status when find does not find anything?

Best Answer

find /tmp -name something | grep .

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

EDIT: Changed from egrep '.*' to the much simpler grep ., since the result is the same.

Related Topic