[Top][All Lists]

[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: Bernhard Voelker
Subject: Re: Is there a way to let find return non-zero when nothing is found?
Date: Tue, 12 Nov 2019 23:54:45 +0100
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.2.1

On 2019-11-11 18:37, Stephane Chazelas wrote:
> 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.

Thanks for digging this up.
I see the following in 'man find' on NetBSD 8.0:

     -exit [status]
             This primary causes find to stop traversing the file system and
             exit immediately, with the specified numeric exit status.  If the
             status value is not specified, then find will exit with status
             zero.  Note that any preceding primaries will be evaluated and
             acted upon before exiting.

(Interestingly, NetBSD-find does not have -quit.)

An existing implementation is a good start to argument to add
new functionality to GNU find.

OTOH, there are ways to let a small shell snippet do the work.
Besides a specific exit code as in NetBSD's -exit primary, and checking
if there was something on stdout, yet another way is to use a 'witness file'
to achieve the same.

  touch WITNESS \
    || echo "error creating WITNESS file"
  find . ... -exec rm WITNESS \; -quit \
    || echo "error: find failed"
  test -f WITNESS \
    && echo "NOT FOUND" \
    || echo "FOUND"

There are 3 ways now to achieve what was requested.
Also, 'find's job is to find files and not to tell when it doesn't find some.
Therefore, I'm currently only 40:60 to add -exit.

Have a nice day,

reply via email to

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