How can I run a specific command for each find result?

Edit: While the following answer explains the general usage case, I should note that deleting files and directories is a special case. Instead of of using the -execdir rm {} \; construct, just use -delete, as in:

find -iname '*.txt' -delete

This handles a bunch of edge cases you might not think about including what order files and directories need to be deleted to not run into errors. For other use cases...

The best way to handle running commands of results of a find is usually to use the various -exec options to the find command. In particular you should try to use -execdir whenever possible since it runs inside the directory of the file that was found and is generally safer (in the sense of preventing stupid mistakes being disastrous) than other options.

The -exec options are followed by the command you would like to run with {} denoting the spot where the file found by find should be included and are terminated by either \; to run the command once for each file or + to replace {} with a list of arguments of all the matches. Note that the semicolon terminator is escaped so that it is not understood by the shell to be a separator leading to a new command.

Lets say you were finding all text files:

find -iname '*.txt' -execdir rm {} \;

Here is the relevant bit from the find manual (man find):

   -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 escaped (with a ‘\’) or quoted to
          protect them from expansion by the shell.  See the EXAMPLES sec-
          tion for examples of the use of the -exec option.  The specified
          command is run once for each matched file.  The command is  exe-
          cuted  in  the starting directory.   There are unavoidable secu-
          rity 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 invoca-
          tions 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.  The command is executed in the
          starting directory.


   -execdir command ;

   -execdir command {} +
          Like -exec, but the specified command is run from the  subdirec-
          tory  containing  the  matched  file,  which is not normally the
          directory in which you started find.  This a  much  more  secure
          method  for invoking commands, as it avoids race conditions dur-
          ing 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 subdirec-
          tory.  If you use this option, you must ensure that  your  $PATH
          environment  variable  does  not  reference  ‘.’;  otherwise, an
          attacker can run any commands they like by leaving an  appropri-
          ately-named  file in a directory in which you will run -execdir.
          The same applies to having entries in $PATH which are  empty  or
          which are not absolute directory names.

An alternative is to pipe the output and parse it with subsequent commands. The only safe way to do so is to use the -print0 option, which tells find to use a null character as the results delimiter. The receiving commands must have a ability to recognize null delimited input. Example:

find /home/phunehehe -iregex '.*\.png$' -print0 | xargs -0 file

Note that the -0 option tells xargs to treat the input as null delimited.


Find has a built in delete command if that is all you need to do.

find . -name "*.txt" -delete

Any .txt file found will be deleted using the command above.