Home Explore Blog CI



man-pages

13th chunk of `find.man`
a61abdc105ef2577d7deea31020631e4b4e49e91f8f3c4a90000000100000fd7
 removal failed, an error message is issued and find’s exit status will be nonzero (when it eventually exits).

              Warning: Don’t forget that find evaluates the command line as an expression, so putting -delete first will make find try to delete everything below the starting points you specified.

              The use of the -delete action on the command line automatically turns on the -depth option.  As in turn -depth makes -prune ineffective, the -delete action cannot usefully be combined with -prune.

              Often,  the user might want to test a find command line with -print prior to adding -delete for the actual removal run.  To avoid surprising results, it is usually best to remember to use -depth explicitly dur‐
              ing those earlier test runs.

              The -delete action will fail to remove a directory unless it is empty.

              Together with the -ignore_readdir_race option, find will ignore errors of the -delete action in the case the file has disappeared since the parent directory was read: it will not output an error diagnostic, not
              change the exit code to nonzero, and the return code of the -delete action will be true.

       -exec command ;
              Execute command; true if 0 status is returned.  All following arguments to find are taken to be arguments to the command until an argument consisting of ‘;’ is encountered.  The string ‘{}’ is replaced  by  the
              current  file  name  being processed everywhere it occurs in the arguments to the command, not just in arguments where it is alone, as in some versions of find.  Both of these constructions might need to be es‐
              caped (with a ‘\’) or quoted to protect them from expansion by the shell.  See the EXAMPLES section for examples of the use of the -exec option.  The specified command is run once for each  matched  file.   The
              command is executed in the starting directory.  There are unavoidable security problems surrounding use of the -exec action; you should use the -execdir option instead.

       -exec command {} +
              This  variant  of the -exec action runs the specified command on the selected files, but the command line is built by appending each selected file name at the end; the total number of invocations of the command
              will be much less than the number of matched files.  The command line is built in much the same way that xargs builds its command lines.  Only one instance of ‘{}’ is allowed within the command, and it must ap‐
              pear at the end, immediately before the ‘+’; it needs to be escaped (with a ‘\’) or quoted to protect it from interpretation by the shell.  The command is executed in the starting directory.  If any  invocation
              with the ‘+’ form returns a non‐zero value as exit status, then find returns a non‐zero exit status.  If find encounters an error, this can sometimes cause an immediate exit, so some pending commands may not be
              run at all.  For this reason -exec my‐command ... {} + -quit may not result in my‐command actually being run.  This variant of -exec always returns true.

       -execdir command ;

       -execdir command {} +
              Like  -exec, but the specified command is run from the subdirectory containing the matched file, which is not normally the directory in which you started find.  As with -exec, the {} should be quoted if find is
              being invoked from a shell.  This a much more secure method for invoking commands, as it avoids race conditions during resolution of the paths to the matched files.  As with the -exec action, the  ‘+’  form  of
              -execdir  will  build a command line to process more than one matched file, but any given invocation of command will only list files that exist in the same subdirectory.  If you use this option, you must ensure
              that your PATH environment variable does not reference ‘.’; otherwise, an

Title: find Actions (continued): -delete, -exec, -exec command {} +, -execdir, -execdir command {} +
Summary
This section details more `find` actions. It expands on the `-delete` action, covering error handling and interactions with `-depth` and `-prune`. It then describes the `-exec` action for executing commands on matched files, highlighting security concerns and the safer alternative, `-execdir`. Both `-exec` and `-execdir` have variants that append multiple file names to the command line for efficiency.