Bash script to move all png files in folder and its subfolders to another directory?












2















In ~/Desktop/a/ , I have .png files, and there are also subfolders within this that also have .png files.



I'd like to move all of those .png files to another folder.



This is my code so far. It runs, but nothing is placed into the target folder. What is the problem?



#!/bin/bash
cd ~/Desktop/a/
for f in $(find . -type f -name "*.png")
do
mv $f ~/Desktop/new/
done









share|improve this question


















  • 2





    This might help: How to debug a bash script?

    – Cyrus
    Nov 25 '18 at 8:25











  • See: Copy every file of entire directory structure into base path of another

    – Cyrus
    Nov 25 '18 at 8:28











  • I do not see why this script should fail silently. But in any case I would enclose $f in double quotes. You may want to try echo instead of mv.

    – tif
    Nov 25 '18 at 8:30











  • for f in $(find ...) is basically a bug. See mywiki.wooledge.org/BashPitfalls#for_f_in_.24.28ls_.2A.mp3.29.

    – melpomene
    Nov 25 '18 at 8:32
















2















In ~/Desktop/a/ , I have .png files, and there are also subfolders within this that also have .png files.



I'd like to move all of those .png files to another folder.



This is my code so far. It runs, but nothing is placed into the target folder. What is the problem?



#!/bin/bash
cd ~/Desktop/a/
for f in $(find . -type f -name "*.png")
do
mv $f ~/Desktop/new/
done









share|improve this question


















  • 2





    This might help: How to debug a bash script?

    – Cyrus
    Nov 25 '18 at 8:25











  • See: Copy every file of entire directory structure into base path of another

    – Cyrus
    Nov 25 '18 at 8:28











  • I do not see why this script should fail silently. But in any case I would enclose $f in double quotes. You may want to try echo instead of mv.

    – tif
    Nov 25 '18 at 8:30











  • for f in $(find ...) is basically a bug. See mywiki.wooledge.org/BashPitfalls#for_f_in_.24.28ls_.2A.mp3.29.

    – melpomene
    Nov 25 '18 at 8:32














2












2








2








In ~/Desktop/a/ , I have .png files, and there are also subfolders within this that also have .png files.



I'd like to move all of those .png files to another folder.



This is my code so far. It runs, but nothing is placed into the target folder. What is the problem?



#!/bin/bash
cd ~/Desktop/a/
for f in $(find . -type f -name "*.png")
do
mv $f ~/Desktop/new/
done









share|improve this question














In ~/Desktop/a/ , I have .png files, and there are also subfolders within this that also have .png files.



I'd like to move all of those .png files to another folder.



This is my code so far. It runs, but nothing is placed into the target folder. What is the problem?



#!/bin/bash
cd ~/Desktop/a/
for f in $(find . -type f -name "*.png")
do
mv $f ~/Desktop/new/
done






bash






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 25 '18 at 8:22









user10701455user10701455

152




152








  • 2





    This might help: How to debug a bash script?

    – Cyrus
    Nov 25 '18 at 8:25











  • See: Copy every file of entire directory structure into base path of another

    – Cyrus
    Nov 25 '18 at 8:28











  • I do not see why this script should fail silently. But in any case I would enclose $f in double quotes. You may want to try echo instead of mv.

    – tif
    Nov 25 '18 at 8:30











  • for f in $(find ...) is basically a bug. See mywiki.wooledge.org/BashPitfalls#for_f_in_.24.28ls_.2A.mp3.29.

    – melpomene
    Nov 25 '18 at 8:32














  • 2





    This might help: How to debug a bash script?

    – Cyrus
    Nov 25 '18 at 8:25











  • See: Copy every file of entire directory structure into base path of another

    – Cyrus
    Nov 25 '18 at 8:28











  • I do not see why this script should fail silently. But in any case I would enclose $f in double quotes. You may want to try echo instead of mv.

    – tif
    Nov 25 '18 at 8:30











  • for f in $(find ...) is basically a bug. See mywiki.wooledge.org/BashPitfalls#for_f_in_.24.28ls_.2A.mp3.29.

    – melpomene
    Nov 25 '18 at 8:32








2




2





This might help: How to debug a bash script?

– Cyrus
Nov 25 '18 at 8:25





This might help: How to debug a bash script?

– Cyrus
Nov 25 '18 at 8:25













See: Copy every file of entire directory structure into base path of another

– Cyrus
Nov 25 '18 at 8:28





See: Copy every file of entire directory structure into base path of another

– Cyrus
Nov 25 '18 at 8:28













I do not see why this script should fail silently. But in any case I would enclose $f in double quotes. You may want to try echo instead of mv.

– tif
Nov 25 '18 at 8:30





I do not see why this script should fail silently. But in any case I would enclose $f in double quotes. You may want to try echo instead of mv.

– tif
Nov 25 '18 at 8:30













for f in $(find ...) is basically a bug. See mywiki.wooledge.org/BashPitfalls#for_f_in_.24.28ls_.2A.mp3.29.

– melpomene
Nov 25 '18 at 8:32





for f in $(find ...) is basically a bug. See mywiki.wooledge.org/BashPitfalls#for_f_in_.24.28ls_.2A.mp3.29.

– melpomene
Nov 25 '18 at 8:32












2 Answers
2






active

oldest

votes


















2














I guess that these image filenames maybe include spaces or other special characters.



find ~/Desktop/a/ -type f -name "*.png" -exec mv "{}" ~/Desktop/new/ ;


or



find ~/Desktop/a/ -type f -name "*.png" -print0 | xargs -0 -I{} mv "{}" ~/Desktop/new/





share|improve this answer































    1














    If your bash is new enough, you can also use globstar:



    cd ~/Desktop/a || exit 1
    shopt -s globstar
    mv -- **/*.png ~/Desktop/new


    Or (if there are too many files to fit in a single command line):



    shopt -s globstar
    for f in ~/Desktop/a/**/*.png; do
    mv -- "$f" ~/Desktop/new
    done





    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      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',
      autoActivateHeartbeat: false,
      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%2fstackoverflow.com%2fquestions%2f53465806%2fbash-script-to-move-all-png-files-in-folder-and-its-subfolders-to-another-direct%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      I guess that these image filenames maybe include spaces or other special characters.



      find ~/Desktop/a/ -type f -name "*.png" -exec mv "{}" ~/Desktop/new/ ;


      or



      find ~/Desktop/a/ -type f -name "*.png" -print0 | xargs -0 -I{} mv "{}" ~/Desktop/new/





      share|improve this answer




























        2














        I guess that these image filenames maybe include spaces or other special characters.



        find ~/Desktop/a/ -type f -name "*.png" -exec mv "{}" ~/Desktop/new/ ;


        or



        find ~/Desktop/a/ -type f -name "*.png" -print0 | xargs -0 -I{} mv "{}" ~/Desktop/new/





        share|improve this answer


























          2












          2








          2







          I guess that these image filenames maybe include spaces or other special characters.



          find ~/Desktop/a/ -type f -name "*.png" -exec mv "{}" ~/Desktop/new/ ;


          or



          find ~/Desktop/a/ -type f -name "*.png" -print0 | xargs -0 -I{} mv "{}" ~/Desktop/new/





          share|improve this answer













          I guess that these image filenames maybe include spaces or other special characters.



          find ~/Desktop/a/ -type f -name "*.png" -exec mv "{}" ~/Desktop/new/ ;


          or



          find ~/Desktop/a/ -type f -name "*.png" -print0 | xargs -0 -I{} mv "{}" ~/Desktop/new/






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 25 '18 at 8:33









          FengFeng

          1,0881811




          1,0881811

























              1














              If your bash is new enough, you can also use globstar:



              cd ~/Desktop/a || exit 1
              shopt -s globstar
              mv -- **/*.png ~/Desktop/new


              Or (if there are too many files to fit in a single command line):



              shopt -s globstar
              for f in ~/Desktop/a/**/*.png; do
              mv -- "$f" ~/Desktop/new
              done





              share|improve this answer




























                1














                If your bash is new enough, you can also use globstar:



                cd ~/Desktop/a || exit 1
                shopt -s globstar
                mv -- **/*.png ~/Desktop/new


                Or (if there are too many files to fit in a single command line):



                shopt -s globstar
                for f in ~/Desktop/a/**/*.png; do
                mv -- "$f" ~/Desktop/new
                done





                share|improve this answer


























                  1












                  1








                  1







                  If your bash is new enough, you can also use globstar:



                  cd ~/Desktop/a || exit 1
                  shopt -s globstar
                  mv -- **/*.png ~/Desktop/new


                  Or (if there are too many files to fit in a single command line):



                  shopt -s globstar
                  for f in ~/Desktop/a/**/*.png; do
                  mv -- "$f" ~/Desktop/new
                  done





                  share|improve this answer













                  If your bash is new enough, you can also use globstar:



                  cd ~/Desktop/a || exit 1
                  shopt -s globstar
                  mv -- **/*.png ~/Desktop/new


                  Or (if there are too many files to fit in a single command line):



                  shopt -s globstar
                  for f in ~/Desktop/a/**/*.png; do
                  mv -- "$f" ~/Desktop/new
                  done






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 25 '18 at 8:39









                  melpomenemelpomene

                  61.6k54994




                  61.6k54994






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • 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%2fstackoverflow.com%2fquestions%2f53465806%2fbash-script-to-move-all-png-files-in-folder-and-its-subfolders-to-another-direct%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

                      Create new schema in PostgreSQL using DBeaver

                      Deepest pit of an array with Javascript: test on Codility

                      Costa Masnaga