[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: Stephane Chazelas
Subject: Re: Is there a way to let find return non-zero when nothing is found?
Date: Thu, 14 Nov 2019 08:19:19 +0000
User-agent: NeoMutt/20171215

2019-11-12 23:54:45 +0100, Bernhard Voelker:
> I see the following in 'man find' on NetBSD 8.0:
>      -exit [status]
> (Interestingly, NetBSD-find does not have -quit.)

-exit was added there in 2006. GNU's -quit was added in 4.2.3
(2004) so that makes GNU's -quit the first indeed.

POSIXly, one can do:

find ... -exec sh -c 'kill -s PIPE "$PPID"' \;

if [ "$(kill -l "$?")" -eq PIPE ]; then...

(well, strictly speaking, I guess POSIX doesn't mandate sh to be
a direct child of a single find process here, but that's what
all implementations I know do).

Here using SIGPIPE for bash which for other signals reports an
error message. It's a bit brittle in that SIGPIPE could be
delivered for other reasons.

> 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"

That is not alway possible or desirable. You'd generally want
that WITNESS to be outside the area you're calling find on (for
the general case), you'd want to avoid writing it in a world
writable area with a fixed name. You need to clean-up after,


reply via email to

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