RFC: "-exit" option for find(1)
Oliver Eikemeier
eikemeier at fillmore-labs.com
Sat Jul 17 11:33:55 PDT 2004
Peter Pentchev wrote:
> On Fri, Jul 16, 2004 at 05:28:57PM +0300, Peter Pentchev wrote:
>> On Fri, Jul 16, 2004 at 11:58:07AM +0400, Denis Antrushin wrote:
>>> Alfred Perlstein wrote:
>>>> I'm up too late, this doesn't work because find returns
>>>> success whenever it successfully runs thought everything.
>>>>
>>>> Perhaps the primary change to just "-exit" which would
>>>> make find exit successfully, and if the primary is never
>>>> encountered (ie. our find logic never hits it) find would
>>>> exit with a non-zero exit status?
>>>>
>>>> Ideas? Better ideas?
>>>>
>>>> The reason I want this is to avoid extracting a tarball
>>>> over a directory that has files in it that are newer than
>>>> the tarball.
>>>>
>>>> Neither tar nor find seem to make this easy...
>>> What about this:
>>>
>>> test -n "`find . -type f -newer ../src.tar.gz`" && echo hi
>>
>> I believe Alfred's problem with this is that it will still traverse the
>> whole hierarchy even after a match is found. In some cases, the
>> hierarchy may be huge, and if the match is within the first 100-200
>> files, well... :)
>>
>> I wonder if it wouldn't be a bit better to add to find(1) something
>> like
>> -maxmatches N, similar to Alfred's idea, but not limited to a single
>> match?
>
> Well, I've just gone ahead and implemented it: say hello to the new
> -maxmatch N and -minmatch N primaries:
>
> -maxmatch n
> Always true; exits after printing out n matching filenames. If
> any -maxmatch primary is specified, it applies to the entire
> expression even if it would not normally be evaluated. -maxmatch
> 0 makes find exit immediately without performing any filesystem
> traversal.
>
> -minmatch n
> Always true; exits with a non-zero exit code if less than n
> matching filenames were printed out at the end of the search. If
> any -minmatch primary is specified, it applies to the entire
> expression even if it would not normally be evaluated.
>
> Thus, -maxmatch 1 would help in Alfred's case. Patch attached.
I don't really like this because:
a) It's global
b) It is counting printed lines, while I might want to use -exec or
something else
c) It does not honour find(1)s [+-]n semantic
How about two primaries:
- -exit n, like proposed in the first post (sucess could be determined
by a printed line or an -exec)
- -match n, which is true when *this* primary has been matched n times
so
find [path] ... -match 1 -print -exit 0
will do the trick, while
find [path] ... -match -10 -print
will print the first 9 matches?
-Oliver
More information about the freebsd-hackers
mailing list