Is there a way to use > operator in find -exec











up vote
2
down vote

favorite












I'm trying to empty lots of files under a certain folder.



>file or cat /dev/null > file or echo "" > file can empty file.
find . -type f -exec blahblah {} ; can find files and do something on them.



I tried to use the > operator in find ... -exec but the result is different to what I expected.



Is there a way to use > operator in the find command?










share|improve this question




























    up vote
    2
    down vote

    favorite












    I'm trying to empty lots of files under a certain folder.



    >file or cat /dev/null > file or echo "" > file can empty file.
    find . -type f -exec blahblah {} ; can find files and do something on them.



    I tried to use the > operator in find ... -exec but the result is different to what I expected.



    Is there a way to use > operator in the find command?










    share|improve this question


























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I'm trying to empty lots of files under a certain folder.



      >file or cat /dev/null > file or echo "" > file can empty file.
      find . -type f -exec blahblah {} ; can find files and do something on them.



      I tried to use the > operator in find ... -exec but the result is different to what I expected.



      Is there a way to use > operator in the find command?










      share|improve this question















      I'm trying to empty lots of files under a certain folder.



      >file or cat /dev/null > file or echo "" > file can empty file.
      find . -type f -exec blahblah {} ; can find files and do something on them.



      I tried to use the > operator in find ... -exec but the result is different to what I expected.



      Is there a way to use > operator in the find command?







      find exec






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jul 22 '14 at 13:21









      An Dorfer

      1,2032713




      1,2032713










      asked Feb 11 '14 at 14:14









      Sencer H.

      796617




      796617






















          4 Answers
          4






          active

          oldest

          votes

















          up vote
          4
          down vote



          accepted










          You can't use it directly, since it will be interpreted as an actual redirection. You have to wrap the call in another shell:



          find . -type f -exec sh -c 'cat /dev/null >| $0' {} ;


          If sh is Bash, you can also do:



          find . -type f -exec sh -c '> $0' {} ;





          share|improve this answer

















          • 1




            Ouch! My head! How I can't remember that :) Thank you.
            – Sencer H.
            Feb 11 '14 at 14:42


















          up vote
          2
          down vote













          Or you could redirect the output of the find command with process substitution:



          while IFS= read -r -d '' file
          do cat /dev/null > "$file"
          done < <(find . type -f print0)





          share|improve this answer






























            up vote
            1
            down vote













            parallel allows escaping > as >:



            find . -type f|parallel >{}


            Or just use read:



            find . -type f|while read f;do >"$f";done


            You don't need -r, -d '', or IFS= unless the paths contain backslashes or newlines or start or end with characters in IFS.






            share|improve this answer





















            • +1 for being a good solution. Probably not the one the OP was looking for, but very nice to have on the site.
              – Hennes
              Mar 8 '14 at 13:54


















            up vote
            1
            down vote













            Alternatively, one could just use the appropriately named truncate command.



            Like this:



            truncate -s 0 file.blob


            The GNU coreutils version of truncate also handles a lot of fascinating things:




            SIZE may also be prefixed by one of the following modifying characters: '+' extend by, '-' reduce by, '<' at most, '>' at least, '/' round down to multiple of, '%' round up to multiple of.




            An even simpler, although less appropriately “named” method would be



            cp /dev/null file.blob





            share|improve this answer





















              Your Answer








              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "3"
              };
              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: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f715386%2fis-there-a-way-to-use-operator-in-find-exec%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              4 Answers
              4






              active

              oldest

              votes








              4 Answers
              4






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              4
              down vote



              accepted










              You can't use it directly, since it will be interpreted as an actual redirection. You have to wrap the call in another shell:



              find . -type f -exec sh -c 'cat /dev/null >| $0' {} ;


              If sh is Bash, you can also do:



              find . -type f -exec sh -c '> $0' {} ;





              share|improve this answer

















              • 1




                Ouch! My head! How I can't remember that :) Thank you.
                – Sencer H.
                Feb 11 '14 at 14:42















              up vote
              4
              down vote



              accepted










              You can't use it directly, since it will be interpreted as an actual redirection. You have to wrap the call in another shell:



              find . -type f -exec sh -c 'cat /dev/null >| $0' {} ;


              If sh is Bash, you can also do:



              find . -type f -exec sh -c '> $0' {} ;





              share|improve this answer

















              • 1




                Ouch! My head! How I can't remember that :) Thank you.
                – Sencer H.
                Feb 11 '14 at 14:42













              up vote
              4
              down vote



              accepted







              up vote
              4
              down vote



              accepted






              You can't use it directly, since it will be interpreted as an actual redirection. You have to wrap the call in another shell:



              find . -type f -exec sh -c 'cat /dev/null >| $0' {} ;


              If sh is Bash, you can also do:



              find . -type f -exec sh -c '> $0' {} ;





              share|improve this answer












              You can't use it directly, since it will be interpreted as an actual redirection. You have to wrap the call in another shell:



              find . -type f -exec sh -c 'cat /dev/null >| $0' {} ;


              If sh is Bash, you can also do:



              find . -type f -exec sh -c '> $0' {} ;






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Feb 11 '14 at 14:21









              slhck

              158k47436461




              158k47436461








              • 1




                Ouch! My head! How I can't remember that :) Thank you.
                – Sencer H.
                Feb 11 '14 at 14:42














              • 1




                Ouch! My head! How I can't remember that :) Thank you.
                – Sencer H.
                Feb 11 '14 at 14:42








              1




              1




              Ouch! My head! How I can't remember that :) Thank you.
              – Sencer H.
              Feb 11 '14 at 14:42




              Ouch! My head! How I can't remember that :) Thank you.
              – Sencer H.
              Feb 11 '14 at 14:42












              up vote
              2
              down vote













              Or you could redirect the output of the find command with process substitution:



              while IFS= read -r -d '' file
              do cat /dev/null > "$file"
              done < <(find . type -f print0)





              share|improve this answer



























                up vote
                2
                down vote













                Or you could redirect the output of the find command with process substitution:



                while IFS= read -r -d '' file
                do cat /dev/null > "$file"
                done < <(find . type -f print0)





                share|improve this answer

























                  up vote
                  2
                  down vote










                  up vote
                  2
                  down vote









                  Or you could redirect the output of the find command with process substitution:



                  while IFS= read -r -d '' file
                  do cat /dev/null > "$file"
                  done < <(find . type -f print0)





                  share|improve this answer














                  Or you could redirect the output of the find command with process substitution:



                  while IFS= read -r -d '' file
                  do cat /dev/null > "$file"
                  done < <(find . type -f print0)






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Feb 13 '14 at 11:50









                  slhck

                  158k47436461




                  158k47436461










                  answered Feb 13 '14 at 11:31









                  stib

                  2,30341930




                  2,30341930






















                      up vote
                      1
                      down vote













                      parallel allows escaping > as >:



                      find . -type f|parallel >{}


                      Or just use read:



                      find . -type f|while read f;do >"$f";done


                      You don't need -r, -d '', or IFS= unless the paths contain backslashes or newlines or start or end with characters in IFS.






                      share|improve this answer





















                      • +1 for being a good solution. Probably not the one the OP was looking for, but very nice to have on the site.
                        – Hennes
                        Mar 8 '14 at 13:54















                      up vote
                      1
                      down vote













                      parallel allows escaping > as >:



                      find . -type f|parallel >{}


                      Or just use read:



                      find . -type f|while read f;do >"$f";done


                      You don't need -r, -d '', or IFS= unless the paths contain backslashes or newlines or start or end with characters in IFS.






                      share|improve this answer





















                      • +1 for being a good solution. Probably not the one the OP was looking for, but very nice to have on the site.
                        – Hennes
                        Mar 8 '14 at 13:54













                      up vote
                      1
                      down vote










                      up vote
                      1
                      down vote









                      parallel allows escaping > as >:



                      find . -type f|parallel >{}


                      Or just use read:



                      find . -type f|while read f;do >"$f";done


                      You don't need -r, -d '', or IFS= unless the paths contain backslashes or newlines or start or end with characters in IFS.






                      share|improve this answer












                      parallel allows escaping > as >:



                      find . -type f|parallel >{}


                      Or just use read:



                      find . -type f|while read f;do >"$f";done


                      You don't need -r, -d '', or IFS= unless the paths contain backslashes or newlines or start or end with characters in IFS.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Mar 8 '14 at 13:43









                      user495470

                      30.8k586125




                      30.8k586125












                      • +1 for being a good solution. Probably not the one the OP was looking for, but very nice to have on the site.
                        – Hennes
                        Mar 8 '14 at 13:54


















                      • +1 for being a good solution. Probably not the one the OP was looking for, but very nice to have on the site.
                        – Hennes
                        Mar 8 '14 at 13:54
















                      +1 for being a good solution. Probably not the one the OP was looking for, but very nice to have on the site.
                      – Hennes
                      Mar 8 '14 at 13:54




                      +1 for being a good solution. Probably not the one the OP was looking for, but very nice to have on the site.
                      – Hennes
                      Mar 8 '14 at 13:54










                      up vote
                      1
                      down vote













                      Alternatively, one could just use the appropriately named truncate command.



                      Like this:



                      truncate -s 0 file.blob


                      The GNU coreutils version of truncate also handles a lot of fascinating things:




                      SIZE may also be prefixed by one of the following modifying characters: '+' extend by, '-' reduce by, '<' at most, '>' at least, '/' round down to multiple of, '%' round up to multiple of.




                      An even simpler, although less appropriately “named” method would be



                      cp /dev/null file.blob





                      share|improve this answer

























                        up vote
                        1
                        down vote













                        Alternatively, one could just use the appropriately named truncate command.



                        Like this:



                        truncate -s 0 file.blob


                        The GNU coreutils version of truncate also handles a lot of fascinating things:




                        SIZE may also be prefixed by one of the following modifying characters: '+' extend by, '-' reduce by, '<' at most, '>' at least, '/' round down to multiple of, '%' round up to multiple of.




                        An even simpler, although less appropriately “named” method would be



                        cp /dev/null file.blob





                        share|improve this answer























                          up vote
                          1
                          down vote










                          up vote
                          1
                          down vote









                          Alternatively, one could just use the appropriately named truncate command.



                          Like this:



                          truncate -s 0 file.blob


                          The GNU coreutils version of truncate also handles a lot of fascinating things:




                          SIZE may also be prefixed by one of the following modifying characters: '+' extend by, '-' reduce by, '<' at most, '>' at least, '/' round down to multiple of, '%' round up to multiple of.




                          An even simpler, although less appropriately “named” method would be



                          cp /dev/null file.blob





                          share|improve this answer












                          Alternatively, one could just use the appropriately named truncate command.



                          Like this:



                          truncate -s 0 file.blob


                          The GNU coreutils version of truncate also handles a lot of fascinating things:




                          SIZE may also be prefixed by one of the following modifying characters: '+' extend by, '-' reduce by, '<' at most, '>' at least, '/' round down to multiple of, '%' round up to multiple of.




                          An even simpler, although less appropriately “named” method would be



                          cp /dev/null file.blob






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Jul 22 '14 at 13:37









                          Daniel B

                          32.8k75986




                          32.8k75986






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Super User!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.





                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                              Please pay close attention to the following guidance:


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f715386%2fis-there-a-way-to-use-operator-in-find-exec%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Probability when a professor distributes a quiz and homework assignment to a class of n students.

                              Aardman Animations

                              Are they similar matrix