bug-findutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Is there a way to let find return non-zero when nothing is found?


From: Stephane Chazelas
Subject: Re: Is there a way to let find return non-zero when nothing is found?
Date: Mon, 11 Nov 2019 17:37:20 +0000
User-agent: NeoMutt/20171215

2019-11-11 17:45:03 +0800, Peng Yu:
> Hi,
> 
> Sometimes, I'd like to know whether there is nothing found. Is there a
> way to let find return none-zero when nothing is found? Thanks.
[...]

If using -print, you can pipe to grep '^'

if find ... | grep '^'; then
  echo "find found something, see list above"
fi

Or if you only care whether it found something:

if find ... -print -quit | grep -q '^'; then
  echo "find found something"
fi

With find alone, you can use this trick:

if find . /error ... -quit; then
  echo "find foung something"
else
  echo "no file found or error encountered before the first file was found"
fi

Note that NetBSD's find has a "-exit <status>" predicate, which
you could use here to return one different from 0 and the code
returned for errors:

find . ... -exit 12
case $? in
  (12) echo found;;
  (0) echo not there;;
  (*) echo not sure as there was an error;;
esac

I don't think GNU find has an equivalent command.

-- 
Stephane




reply via email to

[Prev in Thread] Current Thread [Next in Thread]