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

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
39
down vote

favorite
12












How would one run a specific command for each file that was found by using the find command? For the purpose of the question lets say that I would simply like to delete each file found by find.







share|improve this question


























    up vote
    39
    down vote

    favorite
    12












    How would one run a specific command for each file that was found by using the find command? For the purpose of the question lets say that I would simply like to delete each file found by find.







    share|improve this question
























      up vote
      39
      down vote

      favorite
      12









      up vote
      39
      down vote

      favorite
      12






      12





      How would one run a specific command for each file that was found by using the find command? For the purpose of the question lets say that I would simply like to delete each file found by find.







      share|improve this question














      How would one run a specific command for each file that was found by using the find command? For the purpose of the question lets say that I would simply like to delete each file found by find.









      share|improve this question













      share|improve this question




      share|improve this question








      edited 10 hours ago









      Rui F Ribeiro

      34.8k1269113




      34.8k1269113










      asked Dec 2 '11 at 10:33









      FailedDev

      298136




      298136




















          5 Answers
          5






          active

          oldest

          votes

















          up vote
          46
          down vote



          accepted










          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.





          share|improve this answer






















          • The "find" command provided by Busybox does not support the -execdir option, hence it may be necessary to use one of the pipe / xargs methods mentioned below.
            – MikeW
            Aug 9 '17 at 9:41

















          up vote
          6
          down vote













          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.






          share|improve this answer






















          • You can -exec with more files if you end it with + instead of ;. See caleb's answer.
            – Kevin
            Dec 2 '11 at 12:44











          • @Kevin you are right, I updated the answer.
            – phunehehe
            Dec 2 '11 at 15:21

















          up vote
          3
          down vote













          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.






          share|improve this answer



























            up vote
            1
            down vote













            I was searching for an answer to this and I stumbled upon this thread.
            The answers gave me and idea on how I could achieve it.
            Suppose you want to find the mediainfo of all JPEG files



            This would append mediainfo " at the beginning and " at the end of every matched file (To escape special characters as much as possible) , put it to a script and run the script:



            find . -name *.jpg | sed -e 's/^/mediainfo "/g;' | sed -e 's/$/"/g;' > foo.sh && sh foo.sh


            In case you are worried something can go wrong, you can skip redirecting the output to a file and just see the result in the terminal before running the script.






            share|improve this answer



























              up vote
              0
              down vote













              You can accomplish this using the xargs command. xargs essentially runs a command once for each instruction of its standard input. So, if you need to delete all .jpg files in a directory for example, a quick way on the command line is:



              $ find ./ -name "*.jpg" | xargs rm 


              You can also use the backtick (above the Tab button) to do this (note that this is the backquote character, not the single quote character):



              $ rm `find ./ -name "*.jpg"`


              Note that due to the way xargs and shells process their input, the xargs method only works if none of the file names and directory names involved contain whitespace or any of "'; the backquote method only works if none of the file names and directory names involved contain whitespace or any of [?*.






              share|improve this answer


















              • 3




                Both of these methods are potentially very dangerous, especially the backtick one. There are numerous potential problems with un-escaped characters in file names that could cause these methods to break.
                – Caleb
                Dec 2 '11 at 10:41






              • 1




                I see your point, but these commands can also be used along with other tools besides find, so I think they are worth mentioning here.
                – IG83
                Dec 2 '11 at 10:59










              • They might be worth mentioning, but when not to use them is important to specify. The OP's question here specifically asked for handling the output of find, for which -exec is generally a better solution. If you want to specify these as alternates, at least explain how to use find -print0 | xargs -0 for safe file name break handling and elaborate on when to be careful about backticks.
                – Caleb
                Dec 2 '11 at 11:09






              • 1




                Welcome to the site by the way, I see this is your first answer. Sorry to jump all over it. It's important to teach people up front with the issues are so they don't make mistakes that are hard to catch later, but I do still remember the days when I didn't understand why this was such a big issue too, so please don't take the correction as personal.
                – Caleb
                Dec 2 '11 at 11:11






              • 3




                Thank you for the welcome! Of course there are no hard feelings, you are certainly right in that -exec is the appropriate way to handle this. I am really beginning to see how great this platform is, you are learning new stuff even from the comments:)
                – IG83
                Dec 2 '11 at 11:52










              Your Answer







              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "106"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              convertImagesToLinks: false,
              noModals: false,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );








               

              draft saved


              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f25921%2fhow-can-i-run-a-specific-command-for-each-find-result%23new-answer', 'question_page');

              );

              Post as a guest






























              5 Answers
              5






              active

              oldest

              votes








              5 Answers
              5






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              46
              down vote



              accepted










              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.





              share|improve this answer






















              • The "find" command provided by Busybox does not support the -execdir option, hence it may be necessary to use one of the pipe / xargs methods mentioned below.
                – MikeW
                Aug 9 '17 at 9:41














              up vote
              46
              down vote



              accepted










              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.





              share|improve this answer






















              • The "find" command provided by Busybox does not support the -execdir option, hence it may be necessary to use one of the pipe / xargs methods mentioned below.
                – MikeW
                Aug 9 '17 at 9:41












              up vote
              46
              down vote



              accepted







              up vote
              46
              down vote



              accepted






              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.





              share|improve this answer














              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.






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Apr 26 '16 at 7:14

























              answered Dec 2 '11 at 10:39









              Caleb

              48.8k9144183




              48.8k9144183











              • The "find" command provided by Busybox does not support the -execdir option, hence it may be necessary to use one of the pipe / xargs methods mentioned below.
                – MikeW
                Aug 9 '17 at 9:41
















              • The "find" command provided by Busybox does not support the -execdir option, hence it may be necessary to use one of the pipe / xargs methods mentioned below.
                – MikeW
                Aug 9 '17 at 9:41















              The "find" command provided by Busybox does not support the -execdir option, hence it may be necessary to use one of the pipe / xargs methods mentioned below.
              – MikeW
              Aug 9 '17 at 9:41




              The "find" command provided by Busybox does not support the -execdir option, hence it may be necessary to use one of the pipe / xargs methods mentioned below.
              – MikeW
              Aug 9 '17 at 9:41












              up vote
              6
              down vote













              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.






              share|improve this answer






















              • You can -exec with more files if you end it with + instead of ;. See caleb's answer.
                – Kevin
                Dec 2 '11 at 12:44











              • @Kevin you are right, I updated the answer.
                – phunehehe
                Dec 2 '11 at 15:21














              up vote
              6
              down vote













              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.






              share|improve this answer






















              • You can -exec with more files if you end it with + instead of ;. See caleb's answer.
                – Kevin
                Dec 2 '11 at 12:44











              • @Kevin you are right, I updated the answer.
                – phunehehe
                Dec 2 '11 at 15:21












              up vote
              6
              down vote










              up vote
              6
              down vote









              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.






              share|improve this answer














              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.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Dec 2 '11 at 15:21

























              answered Dec 2 '11 at 12:00









              phunehehe

              11.9k1779136




              11.9k1779136











              • You can -exec with more files if you end it with + instead of ;. See caleb's answer.
                – Kevin
                Dec 2 '11 at 12:44











              • @Kevin you are right, I updated the answer.
                – phunehehe
                Dec 2 '11 at 15:21
















              • You can -exec with more files if you end it with + instead of ;. See caleb's answer.
                – Kevin
                Dec 2 '11 at 12:44











              • @Kevin you are right, I updated the answer.
                – phunehehe
                Dec 2 '11 at 15:21















              You can -exec with more files if you end it with + instead of ;. See caleb's answer.
              – Kevin
              Dec 2 '11 at 12:44





              You can -exec with more files if you end it with + instead of ;. See caleb's answer.
              – Kevin
              Dec 2 '11 at 12:44













              @Kevin you are right, I updated the answer.
              – phunehehe
              Dec 2 '11 at 15:21




              @Kevin you are right, I updated the answer.
              – phunehehe
              Dec 2 '11 at 15:21










              up vote
              3
              down vote













              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.






              share|improve this answer
























                up vote
                3
                down vote













                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.






                share|improve this answer






















                  up vote
                  3
                  down vote










                  up vote
                  3
                  down vote









                  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.






                  share|improve this answer












                  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.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Dec 2 '11 at 16:47









                  SaultDon

                  25114




                  25114




















                      up vote
                      1
                      down vote













                      I was searching for an answer to this and I stumbled upon this thread.
                      The answers gave me and idea on how I could achieve it.
                      Suppose you want to find the mediainfo of all JPEG files



                      This would append mediainfo " at the beginning and " at the end of every matched file (To escape special characters as much as possible) , put it to a script and run the script:



                      find . -name *.jpg | sed -e 's/^/mediainfo "/g;' | sed -e 's/$/"/g;' > foo.sh && sh foo.sh


                      In case you are worried something can go wrong, you can skip redirecting the output to a file and just see the result in the terminal before running the script.






                      share|improve this answer
























                        up vote
                        1
                        down vote













                        I was searching for an answer to this and I stumbled upon this thread.
                        The answers gave me and idea on how I could achieve it.
                        Suppose you want to find the mediainfo of all JPEG files



                        This would append mediainfo " at the beginning and " at the end of every matched file (To escape special characters as much as possible) , put it to a script and run the script:



                        find . -name *.jpg | sed -e 's/^/mediainfo "/g;' | sed -e 's/$/"/g;' > foo.sh && sh foo.sh


                        In case you are worried something can go wrong, you can skip redirecting the output to a file and just see the result in the terminal before running the script.






                        share|improve this answer






















                          up vote
                          1
                          down vote










                          up vote
                          1
                          down vote









                          I was searching for an answer to this and I stumbled upon this thread.
                          The answers gave me and idea on how I could achieve it.
                          Suppose you want to find the mediainfo of all JPEG files



                          This would append mediainfo " at the beginning and " at the end of every matched file (To escape special characters as much as possible) , put it to a script and run the script:



                          find . -name *.jpg | sed -e 's/^/mediainfo "/g;' | sed -e 's/$/"/g;' > foo.sh && sh foo.sh


                          In case you are worried something can go wrong, you can skip redirecting the output to a file and just see the result in the terminal before running the script.






                          share|improve this answer












                          I was searching for an answer to this and I stumbled upon this thread.
                          The answers gave me and idea on how I could achieve it.
                          Suppose you want to find the mediainfo of all JPEG files



                          This would append mediainfo " at the beginning and " at the end of every matched file (To escape special characters as much as possible) , put it to a script and run the script:



                          find . -name *.jpg | sed -e 's/^/mediainfo "/g;' | sed -e 's/$/"/g;' > foo.sh && sh foo.sh


                          In case you are worried something can go wrong, you can skip redirecting the output to a file and just see the result in the terminal before running the script.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 18 '17 at 22:46









                          RuMAN S

                          111




                          111




















                              up vote
                              0
                              down vote













                              You can accomplish this using the xargs command. xargs essentially runs a command once for each instruction of its standard input. So, if you need to delete all .jpg files in a directory for example, a quick way on the command line is:



                              $ find ./ -name "*.jpg" | xargs rm 


                              You can also use the backtick (above the Tab button) to do this (note that this is the backquote character, not the single quote character):



                              $ rm `find ./ -name "*.jpg"`


                              Note that due to the way xargs and shells process their input, the xargs method only works if none of the file names and directory names involved contain whitespace or any of "'; the backquote method only works if none of the file names and directory names involved contain whitespace or any of [?*.






                              share|improve this answer


















                              • 3




                                Both of these methods are potentially very dangerous, especially the backtick one. There are numerous potential problems with un-escaped characters in file names that could cause these methods to break.
                                – Caleb
                                Dec 2 '11 at 10:41






                              • 1




                                I see your point, but these commands can also be used along with other tools besides find, so I think they are worth mentioning here.
                                – IG83
                                Dec 2 '11 at 10:59










                              • They might be worth mentioning, but when not to use them is important to specify. The OP's question here specifically asked for handling the output of find, for which -exec is generally a better solution. If you want to specify these as alternates, at least explain how to use find -print0 | xargs -0 for safe file name break handling and elaborate on when to be careful about backticks.
                                – Caleb
                                Dec 2 '11 at 11:09






                              • 1




                                Welcome to the site by the way, I see this is your first answer. Sorry to jump all over it. It's important to teach people up front with the issues are so they don't make mistakes that are hard to catch later, but I do still remember the days when I didn't understand why this was such a big issue too, so please don't take the correction as personal.
                                – Caleb
                                Dec 2 '11 at 11:11






                              • 3




                                Thank you for the welcome! Of course there are no hard feelings, you are certainly right in that -exec is the appropriate way to handle this. I am really beginning to see how great this platform is, you are learning new stuff even from the comments:)
                                – IG83
                                Dec 2 '11 at 11:52














                              up vote
                              0
                              down vote













                              You can accomplish this using the xargs command. xargs essentially runs a command once for each instruction of its standard input. So, if you need to delete all .jpg files in a directory for example, a quick way on the command line is:



                              $ find ./ -name "*.jpg" | xargs rm 


                              You can also use the backtick (above the Tab button) to do this (note that this is the backquote character, not the single quote character):



                              $ rm `find ./ -name "*.jpg"`


                              Note that due to the way xargs and shells process their input, the xargs method only works if none of the file names and directory names involved contain whitespace or any of "'; the backquote method only works if none of the file names and directory names involved contain whitespace or any of [?*.






                              share|improve this answer


















                              • 3




                                Both of these methods are potentially very dangerous, especially the backtick one. There are numerous potential problems with un-escaped characters in file names that could cause these methods to break.
                                – Caleb
                                Dec 2 '11 at 10:41






                              • 1




                                I see your point, but these commands can also be used along with other tools besides find, so I think they are worth mentioning here.
                                – IG83
                                Dec 2 '11 at 10:59










                              • They might be worth mentioning, but when not to use them is important to specify. The OP's question here specifically asked for handling the output of find, for which -exec is generally a better solution. If you want to specify these as alternates, at least explain how to use find -print0 | xargs -0 for safe file name break handling and elaborate on when to be careful about backticks.
                                – Caleb
                                Dec 2 '11 at 11:09






                              • 1




                                Welcome to the site by the way, I see this is your first answer. Sorry to jump all over it. It's important to teach people up front with the issues are so they don't make mistakes that are hard to catch later, but I do still remember the days when I didn't understand why this was such a big issue too, so please don't take the correction as personal.
                                – Caleb
                                Dec 2 '11 at 11:11






                              • 3




                                Thank you for the welcome! Of course there are no hard feelings, you are certainly right in that -exec is the appropriate way to handle this. I am really beginning to see how great this platform is, you are learning new stuff even from the comments:)
                                – IG83
                                Dec 2 '11 at 11:52












                              up vote
                              0
                              down vote










                              up vote
                              0
                              down vote









                              You can accomplish this using the xargs command. xargs essentially runs a command once for each instruction of its standard input. So, if you need to delete all .jpg files in a directory for example, a quick way on the command line is:



                              $ find ./ -name "*.jpg" | xargs rm 


                              You can also use the backtick (above the Tab button) to do this (note that this is the backquote character, not the single quote character):



                              $ rm `find ./ -name "*.jpg"`


                              Note that due to the way xargs and shells process their input, the xargs method only works if none of the file names and directory names involved contain whitespace or any of "'; the backquote method only works if none of the file names and directory names involved contain whitespace or any of [?*.






                              share|improve this answer














                              You can accomplish this using the xargs command. xargs essentially runs a command once for each instruction of its standard input. So, if you need to delete all .jpg files in a directory for example, a quick way on the command line is:



                              $ find ./ -name "*.jpg" | xargs rm 


                              You can also use the backtick (above the Tab button) to do this (note that this is the backquote character, not the single quote character):



                              $ rm `find ./ -name "*.jpg"`


                              Note that due to the way xargs and shells process their input, the xargs method only works if none of the file names and directory names involved contain whitespace or any of "'; the backquote method only works if none of the file names and directory names involved contain whitespace or any of [?*.







                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Dec 2 '11 at 23:12









                              Gilles

                              505k1199981526




                              505k1199981526










                              answered Dec 2 '11 at 10:39









                              IG83

                              365




                              365







                              • 3




                                Both of these methods are potentially very dangerous, especially the backtick one. There are numerous potential problems with un-escaped characters in file names that could cause these methods to break.
                                – Caleb
                                Dec 2 '11 at 10:41






                              • 1




                                I see your point, but these commands can also be used along with other tools besides find, so I think they are worth mentioning here.
                                – IG83
                                Dec 2 '11 at 10:59










                              • They might be worth mentioning, but when not to use them is important to specify. The OP's question here specifically asked for handling the output of find, for which -exec is generally a better solution. If you want to specify these as alternates, at least explain how to use find -print0 | xargs -0 for safe file name break handling and elaborate on when to be careful about backticks.
                                – Caleb
                                Dec 2 '11 at 11:09






                              • 1




                                Welcome to the site by the way, I see this is your first answer. Sorry to jump all over it. It's important to teach people up front with the issues are so they don't make mistakes that are hard to catch later, but I do still remember the days when I didn't understand why this was such a big issue too, so please don't take the correction as personal.
                                – Caleb
                                Dec 2 '11 at 11:11






                              • 3




                                Thank you for the welcome! Of course there are no hard feelings, you are certainly right in that -exec is the appropriate way to handle this. I am really beginning to see how great this platform is, you are learning new stuff even from the comments:)
                                – IG83
                                Dec 2 '11 at 11:52












                              • 3




                                Both of these methods are potentially very dangerous, especially the backtick one. There are numerous potential problems with un-escaped characters in file names that could cause these methods to break.
                                – Caleb
                                Dec 2 '11 at 10:41






                              • 1




                                I see your point, but these commands can also be used along with other tools besides find, so I think they are worth mentioning here.
                                – IG83
                                Dec 2 '11 at 10:59










                              • They might be worth mentioning, but when not to use them is important to specify. The OP's question here specifically asked for handling the output of find, for which -exec is generally a better solution. If you want to specify these as alternates, at least explain how to use find -print0 | xargs -0 for safe file name break handling and elaborate on when to be careful about backticks.
                                – Caleb
                                Dec 2 '11 at 11:09






                              • 1




                                Welcome to the site by the way, I see this is your first answer. Sorry to jump all over it. It's important to teach people up front with the issues are so they don't make mistakes that are hard to catch later, but I do still remember the days when I didn't understand why this was such a big issue too, so please don't take the correction as personal.
                                – Caleb
                                Dec 2 '11 at 11:11






                              • 3




                                Thank you for the welcome! Of course there are no hard feelings, you are certainly right in that -exec is the appropriate way to handle this. I am really beginning to see how great this platform is, you are learning new stuff even from the comments:)
                                – IG83
                                Dec 2 '11 at 11:52







                              3




                              3




                              Both of these methods are potentially very dangerous, especially the backtick one. There are numerous potential problems with un-escaped characters in file names that could cause these methods to break.
                              – Caleb
                              Dec 2 '11 at 10:41




                              Both of these methods are potentially very dangerous, especially the backtick one. There are numerous potential problems with un-escaped characters in file names that could cause these methods to break.
                              – Caleb
                              Dec 2 '11 at 10:41




                              1




                              1




                              I see your point, but these commands can also be used along with other tools besides find, so I think they are worth mentioning here.
                              – IG83
                              Dec 2 '11 at 10:59




                              I see your point, but these commands can also be used along with other tools besides find, so I think they are worth mentioning here.
                              – IG83
                              Dec 2 '11 at 10:59












                              They might be worth mentioning, but when not to use them is important to specify. The OP's question here specifically asked for handling the output of find, for which -exec is generally a better solution. If you want to specify these as alternates, at least explain how to use find -print0 | xargs -0 for safe file name break handling and elaborate on when to be careful about backticks.
                              – Caleb
                              Dec 2 '11 at 11:09




                              They might be worth mentioning, but when not to use them is important to specify. The OP's question here specifically asked for handling the output of find, for which -exec is generally a better solution. If you want to specify these as alternates, at least explain how to use find -print0 | xargs -0 for safe file name break handling and elaborate on when to be careful about backticks.
                              – Caleb
                              Dec 2 '11 at 11:09




                              1




                              1




                              Welcome to the site by the way, I see this is your first answer. Sorry to jump all over it. It's important to teach people up front with the issues are so they don't make mistakes that are hard to catch later, but I do still remember the days when I didn't understand why this was such a big issue too, so please don't take the correction as personal.
                              – Caleb
                              Dec 2 '11 at 11:11




                              Welcome to the site by the way, I see this is your first answer. Sorry to jump all over it. It's important to teach people up front with the issues are so they don't make mistakes that are hard to catch later, but I do still remember the days when I didn't understand why this was such a big issue too, so please don't take the correction as personal.
                              – Caleb
                              Dec 2 '11 at 11:11




                              3




                              3




                              Thank you for the welcome! Of course there are no hard feelings, you are certainly right in that -exec is the appropriate way to handle this. I am really beginning to see how great this platform is, you are learning new stuff even from the comments:)
                              – IG83
                              Dec 2 '11 at 11:52




                              Thank you for the welcome! Of course there are no hard feelings, you are certainly right in that -exec is the appropriate way to handle this. I am really beginning to see how great this platform is, you are learning new stuff even from the comments:)
                              – IG83
                              Dec 2 '11 at 11:52












                               

                              draft saved


                              draft discarded


























                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f25921%2fhow-can-i-run-a-specific-command-for-each-find-result%23new-answer', 'question_page');

                              );

                              Post as a guest













































































                              Popular posts from this blog

                              How to check contact read email or not when send email to Individual?

                              Bahrain

                              Postfix configuration issue with fips on centos 7; mailgun relay