bugfindutils - Bugs: bug #54730, Add additional valuable example of...


bug #54730: Add additional valuable example of find -quit

Submitted by:  jidanni
Submitted on:  Wed 26 Sep 2018 02:34:51 AM UTC  
Category:  documentation Severity:  3 - Normal
Item Group:  None Status:  None
Privacy:  Public Assigned to:  None
Originator Name:  Open/Closed:  Open
Release:  4.6.0 Fixed Release:  None

Add a New Comment (Rich Markup)

You are not logged in

Please log in, so followups can be emailed to you.


( Jump to the original submission)

Thu 27 Sep 2018 11:23:27 AM UTC, comment #8: 

Compare with the current:
"find /tmp/foo /tmp/bar -print -quit will print only /tmp/foo."

Thu 27 Sep 2018 11:20:34 AM UTC, comment #7: 

You guys are brilliant. Here's some more:

When just one is enough:
# find /proc -name 11 -print | wc -l
# find /proc -name 11 -print -quit | wc -l

Or better yet:

Where did I put that file called "needle2"?
No reason to keep looking for more after we find it,
so we use:
$ find / -name needle2 -print -quit

Thu 27 Sep 2018 09:47:54 AM UTC, comment #6: 

wild idea: to affect the exit code of find, we could add -quit1 or even -quit=<N> (e.g. -quit=1).

But that's a different story.  What do you think about the doc example?

Bernhard Voelker <berny>
Project Administrator
Thu 27 Sep 2018 08:07:55 AM UTC, comment #5: 

Hmm, given that -exec ... ; does not affect the exit status, it is unfortunate that there is no other way for the user to (directly) affect it.   Viz:

jupiter:~$ find / -exec false \; -o -quit
jupiter:~$ echo $?

The most obvious workaround (making use of the fact that the -exec + form does affect the exit status) is pretty horrible:

jupiter:~$ find / -exec false \; -o \( -exec false {} \+  -quit \) ; echo $?

James Youngman <jay>
Project Administrator
Thu 27 Sep 2018 07:19:26 AM UTC, comment #4: 

I agree.  I'm trying to find a good example.

What about the following?

  Find the name of a file in the current directory (and below) which contains
  the word "needle", and stop when the first such file is found.

    # Prepare 4 files, 2 of them contain the word "needle".
    $ touch a b c d   
    $ echo needle > b
    $ echo needle > dc

  Without -quit, 'find' would call 'grep' for every file (and there's
  currently no option to make 'grep' stop after a match across files):

    $ find -type f \
        -printf 'Checking: %p ...\n'  \
        -exec grep -FH needle '{}' \;
    Checking: ./c ...
    Checking: ./d ...
    Checking: ./a ...
    Checking: ./b ...

  With -quit, 'find' stops after the first match:

    $ find -type f \
        -printf 'Checking: %p ...\n' \
        -exec grep -FH needle '{}' \; \
    Checking: ./c ...

Bernhard Voelker <berny>
Project Administrator
Wed 26 Sep 2018 03:52:50 PM UTC, comment #3: 

I think that -exec ... \;  -quit is a valuable example, and we should add this (to both the manpage and the Texinfo manual), even if the example is long (i.e. we should choose "realistic" over "brief").

James Youngman <jay>
Project Administrator
Wed 26 Sep 2018 08:29:31 AM UTC, comment #2: 

Yes there should be many examples added.

The syntax error was on purpose.

The idea is: use -quit as an emergency brake while perfecting one's find
recipe. Let's say of every five adjustments one is a syntax error.
In that case one would want it to quit out right away, with no need to
repeat the same syntax error on every file in the tree.
So that is a good example to use. Though perhaps use python instead of
perl, etc.

Your "For each file in '/dev', run stat(1)" is good, except I am afraid
that the whole thing could be done just using find itself, so perhaps
use a test that find couldn't already do.

As far as the
find: '1': No such file or directory
find: '2': No such file or directory
example, maybe you can make one where
we are not banging around and causing error messages.

Wed 26 Sep 2018 06:33:11 AM UTC, comment #1: 

I'm not sure what you want to achieve with the perl example,
because there is a syntax error in it - probably due to missing

Here's another example:

For each file in '/dev', run stat(1) to print its file type and name,
and stop at the first block device found:

 $ find /dev \
     -exec stat -c "%F: %n" '{}' \; \
     \( -exec test ! -b '{}' \; -o -quit \)
  directory: /dev
  character special file: /dev/vcsa10
  character special file: /dev/vcs10
  fifo: /dev/xconsole
  character special file: /dev/vcs2
  block special file: /dev/loop7

This example is more complex than the one in the man page.
Maybe we should expand on the man page by saying that find stops at
the first existing file:

  # Prepare files 3 4 5, ensuring that 1 and 2 do not exist. 
  $ rm -f 1 2 ; touch 3 4 5

  # Run find to print the file name of and stop at the first existing one.
  $ find 1 2 3 4 5 -print -quit
  find: '1': No such file or directory
  find: '2': No such file or directory

Do you mean something like that?  Or what real-life example would
you prefer?

Bernhard Voelker <berny>
Project Administrator
Wed 26 Sep 2018 02:34:51 AM UTC, original submission:  

On man find we see:

       -quit  Exit  immediately.   No  child  processes will be left running, but no more paths specified on the command
              line will be processed.  For example, find /tmp/foo /tmp/bar -print -quit will print only  /tmp/foo.   Any
              command lines which have been built up with -execdir ... {} + will be invoked before find exits.  The exit
              status may or may not be zero, depending on whether an error has already occurred.

Also add an example:

Indeed this can be used to quit immediately if any -exec has an error:

$ find \( -exec perl -e if {} \; -o -quit \)
syntax error at -e line 1, at EOF
Execution of -e aborted due to compilation errors.

Instead of the error repeating 1000 times.
(The error message came from perl.)



(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:

No files currently attached


Depends on the following items: None found

Items that depend on this one: None found


Carbon-Copy List
  • -email is unavailable- added by jay (Posted a comment)
  • -email is unavailable- added by berny (Posted a comment)

    Do you think this task is very important?
    If so, you can add your encouragement to it.
    This task has 0 encouragements so far.

    Only logged-in users can vote.




    No changes have been made to this item

    Back to the top

    Powered by Savane 3.5