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:  Fixed
Privacy:  Public Assigned to:  James Youngman <jay>
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)

Sun 01 Sep 2019 12:02:04 PM UTC, comment #10: 

I'm about to apply the attached patch which adds some examples similar to the ones we discussed.

(file #47417)

James Youngman <jay>
Project AdministratorIn charge of this item.
Sun 01 Sep 2019 11:09:14 AM UTC, comment #9: 

My second example in #5 is incorrect since -exec ... {} + is not guaranteed to have launched a child process before the -quit is processed.   See also the wording in the EXIT STATUS section of the manual page:

When some error occurs, find may stop immediately, without completing all the actions specified.  For example, some starting points may not have been examined or some pending program invocations for -exec ... {} + or -execdir ... {} + may not have been performed.

James Youngman <jay>
Project AdministratorIn charge of this item.
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."

jidanni
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
178
# find /proc -name 11 -print -quit | wc -l
1

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

jidanni
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 $?
0

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 $?
1

James Youngman <jay>
Project AdministratorIn charge of this item.
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 ...
    ./c:needle
    Checking: ./d ...
    Checking: ./a ...
    Checking: ./b ...
    ./b:needle

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

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

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 AdministratorIn charge of this item.
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.

jidanni
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
quoting.

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
  3

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.)

jidanni

 

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

Attach Files:
   
   
Comment:
   

 

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.

     

     

     

    Follow 4 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2019-09-01 jay StatusIn Progress => Fixed
    2019-09-01 jay Attached File- => Added 0001-Fix-Savannah-bug-54730-additional-examples-for-quit.patch, #47417
    2019-09-01 jay StatusNone => In Progress
        Assigned toNone => jay

    Back to the top


    Powered by Savane 3.5