How to check if find command didn't find anything?
Count the number of lines output and store it in a variable, then test it:
lines=$(find ... | wc -l)
if [ $lines -eq 0 ]; then
...
fi
You want to use find command inside an if condition , you can try this one liner :
[[ ! -z `find 'YOUR_DIR/' -name 'something'` ]] && echo "found" || echo "not found"
example of use :
[prompt] $ mkdir -p Dir/dir1 Dir/dir2/ Dir/dir3
[prompt] $ ls Dir/
dir1 dir2 dir3
[prompt] $ [[ ! -z `find 'Dir/' -name 'something'` ]] && echo "found" || echo "not found"
not found
[prompt] $ touch Dir/dir3/something
[prompt] $ [[ ! -z `find 'Dir/' -name 'something'` ]] && echo "found" || echo "not found"
found
Exit 0 is easy with find, exit >0 is harder because that usually only happens with an error. However we can make it happen:
if find -type f -exec false {} +
then
echo 'nothing found'
else
echo 'something found'
fi