Copying recursively files with spaces

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











up vote
3
down vote

favorite












At the moment I have a set of files of the form of:



/dir one/a picture.jpg
/dir two/some picture.jpg


What I want to do is copy and change the end to -fanart.jpg to the filename to:



/dir one/a picture.jpg
/a picture-fanart.jpg
/dir two/some picture.jpg
/some picture-fanart.jpg


I've managed to get it working for the situation where there are no spaces:



% for i in `find . -name "*.jpg"`; do cp $i "$i%.*"-fanart.jpg ;done;


but I to get it working where there are spaces.










share|improve this question



























    up vote
    3
    down vote

    favorite












    At the moment I have a set of files of the form of:



    /dir one/a picture.jpg
    /dir two/some picture.jpg


    What I want to do is copy and change the end to -fanart.jpg to the filename to:



    /dir one/a picture.jpg
    /a picture-fanart.jpg
    /dir two/some picture.jpg
    /some picture-fanart.jpg


    I've managed to get it working for the situation where there are no spaces:



    % for i in `find . -name "*.jpg"`; do cp $i "$i%.*"-fanart.jpg ;done;


    but I to get it working where there are spaces.










    share|improve this question

























      up vote
      3
      down vote

      favorite









      up vote
      3
      down vote

      favorite











      At the moment I have a set of files of the form of:



      /dir one/a picture.jpg
      /dir two/some picture.jpg


      What I want to do is copy and change the end to -fanart.jpg to the filename to:



      /dir one/a picture.jpg
      /a picture-fanart.jpg
      /dir two/some picture.jpg
      /some picture-fanart.jpg


      I've managed to get it working for the situation where there are no spaces:



      % for i in `find . -name "*.jpg"`; do cp $i "$i%.*"-fanart.jpg ;done;


      but I to get it working where there are spaces.










      share|improve this question















      At the moment I have a set of files of the form of:



      /dir one/a picture.jpg
      /dir two/some picture.jpg


      What I want to do is copy and change the end to -fanart.jpg to the filename to:



      /dir one/a picture.jpg
      /a picture-fanart.jpg
      /dir two/some picture.jpg
      /some picture-fanart.jpg


      I've managed to get it working for the situation where there are no spaces:



      % for i in `find . -name "*.jpg"`; do cp $i "$i%.*"-fanart.jpg ;done;


      but I to get it working where there are spaces.







      command-line cp






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 17 at 1:19









      Rui F Ribeiro

      38.2k1475123




      38.2k1475123










      asked Jan 21 '14 at 3:24









      user3217458

      233




      233




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          3
          down vote



          accepted










          Command substitution (`...` or $(...)) is split on newline, tab and space character (not only newline), and filename generation (globbing) is performed on each word resulting of that splitting. That's the split+glob operator. You could improve things by setting $IFS to newline and disable globbing, but here, best is to write it the proper way:



          find . -name "*.jpg" -type f -exec sh -c '
          for i do
          cp "$i" "$i%.*-fanart.jpg"
          done' sh +


          You could also use pax for that:



          pax -rws'/.jpg$/-fanart&/' -s'/.*//' . .


          Or zsh's zmv:



          autoload zmv
          zmv -QC '(**/)(*)(.jpg)(D.)' '$1$2-fanart$3'





          share|improve this answer





























            up vote
            2
            down vote













            Word expansion is performed during the expansion of unquoted command substitutions, which is why this does not work.



            If you have bash4+, just use globstar:



            shopt -s globstar
            for file in **/*.jpg; do
            cp -- "$file" "$file%.*-fanart.jpg"
            done





            share|improve this answer




















            • That assumes there is no symlink to directories in there. If you're going to use **/ best is to use zsh or ksh93 which don't have the problem.
              – Stéphane Chazelas
              Jan 21 '14 at 7:14










            • @StephaneChazelas Traversing symlinks to directories is not universally undesirable (although I agree, it is not ideal in many situations). I am still pushing for there to be an option to disable recursion in 4.3.
              – Chris Down
              Jan 21 '14 at 7:51










            • I agree make **/ not follow (like in zsh, ksh93 or tcsh) and make the "following symlinks" an option would be better. However, options a la shglob are nice but sometimes add more issues than they solve. It's better to enable the unexpected behaviour per-glob than globally. Using ***/ like in zsh or tcsh would be better. Having globbing qualifiers to enable the noglob, nullglob, nocaseglob, failglob... per-glob like in zsh would also be a lot better.
              – Stéphane Chazelas
              Jan 21 '14 at 10:30











            • @StephaneChazelas Considering how long bash 4 has been out now, I highly doubt that Chet would be willing to break backwards compatibility. *** would be reasonable, otherwise.
              – Chris Down
              Jan 21 '14 at 10:43










            • Chet has done far worse backwards compatibility breaking before (for instance to align =~ with ksh93's). Changing **/ is more likely to fix scripts than break them. GNU grep has broken a (decade long) backward compatibility by making -r stop follow symlinks at some point.
              – Stéphane Chazelas
              Jan 21 '14 at 11:12











            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: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            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%2funix.stackexchange.com%2fquestions%2f110210%2fcopying-recursively-files-with-spaces%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








            up vote
            3
            down vote



            accepted










            Command substitution (`...` or $(...)) is split on newline, tab and space character (not only newline), and filename generation (globbing) is performed on each word resulting of that splitting. That's the split+glob operator. You could improve things by setting $IFS to newline and disable globbing, but here, best is to write it the proper way:



            find . -name "*.jpg" -type f -exec sh -c '
            for i do
            cp "$i" "$i%.*-fanart.jpg"
            done' sh +


            You could also use pax for that:



            pax -rws'/.jpg$/-fanart&/' -s'/.*//' . .


            Or zsh's zmv:



            autoload zmv
            zmv -QC '(**/)(*)(.jpg)(D.)' '$1$2-fanart$3'





            share|improve this answer


























              up vote
              3
              down vote



              accepted










              Command substitution (`...` or $(...)) is split on newline, tab and space character (not only newline), and filename generation (globbing) is performed on each word resulting of that splitting. That's the split+glob operator. You could improve things by setting $IFS to newline and disable globbing, but here, best is to write it the proper way:



              find . -name "*.jpg" -type f -exec sh -c '
              for i do
              cp "$i" "$i%.*-fanart.jpg"
              done' sh +


              You could also use pax for that:



              pax -rws'/.jpg$/-fanart&/' -s'/.*//' . .


              Or zsh's zmv:



              autoload zmv
              zmv -QC '(**/)(*)(.jpg)(D.)' '$1$2-fanart$3'





              share|improve this answer
























                up vote
                3
                down vote



                accepted







                up vote
                3
                down vote



                accepted






                Command substitution (`...` or $(...)) is split on newline, tab and space character (not only newline), and filename generation (globbing) is performed on each word resulting of that splitting. That's the split+glob operator. You could improve things by setting $IFS to newline and disable globbing, but here, best is to write it the proper way:



                find . -name "*.jpg" -type f -exec sh -c '
                for i do
                cp "$i" "$i%.*-fanart.jpg"
                done' sh +


                You could also use pax for that:



                pax -rws'/.jpg$/-fanart&/' -s'/.*//' . .


                Or zsh's zmv:



                autoload zmv
                zmv -QC '(**/)(*)(.jpg)(D.)' '$1$2-fanart$3'





                share|improve this answer














                Command substitution (`...` or $(...)) is split on newline, tab and space character (not only newline), and filename generation (globbing) is performed on each word resulting of that splitting. That's the split+glob operator. You could improve things by setting $IFS to newline and disable globbing, but here, best is to write it the proper way:



                find . -name "*.jpg" -type f -exec sh -c '
                for i do
                cp "$i" "$i%.*-fanart.jpg"
                done' sh +


                You could also use pax for that:



                pax -rws'/.jpg$/-fanart&/' -s'/.*//' . .


                Or zsh's zmv:



                autoload zmv
                zmv -QC '(**/)(*)(.jpg)(D.)' '$1$2-fanart$3'






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Jan 21 '14 at 10:25

























                answered Jan 21 '14 at 7:19









                Stéphane Chazelas

                294k54551893




                294k54551893






















                    up vote
                    2
                    down vote













                    Word expansion is performed during the expansion of unquoted command substitutions, which is why this does not work.



                    If you have bash4+, just use globstar:



                    shopt -s globstar
                    for file in **/*.jpg; do
                    cp -- "$file" "$file%.*-fanart.jpg"
                    done





                    share|improve this answer




















                    • That assumes there is no symlink to directories in there. If you're going to use **/ best is to use zsh or ksh93 which don't have the problem.
                      – Stéphane Chazelas
                      Jan 21 '14 at 7:14










                    • @StephaneChazelas Traversing symlinks to directories is not universally undesirable (although I agree, it is not ideal in many situations). I am still pushing for there to be an option to disable recursion in 4.3.
                      – Chris Down
                      Jan 21 '14 at 7:51










                    • I agree make **/ not follow (like in zsh, ksh93 or tcsh) and make the "following symlinks" an option would be better. However, options a la shglob are nice but sometimes add more issues than they solve. It's better to enable the unexpected behaviour per-glob than globally. Using ***/ like in zsh or tcsh would be better. Having globbing qualifiers to enable the noglob, nullglob, nocaseglob, failglob... per-glob like in zsh would also be a lot better.
                      – Stéphane Chazelas
                      Jan 21 '14 at 10:30











                    • @StephaneChazelas Considering how long bash 4 has been out now, I highly doubt that Chet would be willing to break backwards compatibility. *** would be reasonable, otherwise.
                      – Chris Down
                      Jan 21 '14 at 10:43










                    • Chet has done far worse backwards compatibility breaking before (for instance to align =~ with ksh93's). Changing **/ is more likely to fix scripts than break them. GNU grep has broken a (decade long) backward compatibility by making -r stop follow symlinks at some point.
                      – Stéphane Chazelas
                      Jan 21 '14 at 11:12















                    up vote
                    2
                    down vote













                    Word expansion is performed during the expansion of unquoted command substitutions, which is why this does not work.



                    If you have bash4+, just use globstar:



                    shopt -s globstar
                    for file in **/*.jpg; do
                    cp -- "$file" "$file%.*-fanart.jpg"
                    done





                    share|improve this answer




















                    • That assumes there is no symlink to directories in there. If you're going to use **/ best is to use zsh or ksh93 which don't have the problem.
                      – Stéphane Chazelas
                      Jan 21 '14 at 7:14










                    • @StephaneChazelas Traversing symlinks to directories is not universally undesirable (although I agree, it is not ideal in many situations). I am still pushing for there to be an option to disable recursion in 4.3.
                      – Chris Down
                      Jan 21 '14 at 7:51










                    • I agree make **/ not follow (like in zsh, ksh93 or tcsh) and make the "following symlinks" an option would be better. However, options a la shglob are nice but sometimes add more issues than they solve. It's better to enable the unexpected behaviour per-glob than globally. Using ***/ like in zsh or tcsh would be better. Having globbing qualifiers to enable the noglob, nullglob, nocaseglob, failglob... per-glob like in zsh would also be a lot better.
                      – Stéphane Chazelas
                      Jan 21 '14 at 10:30











                    • @StephaneChazelas Considering how long bash 4 has been out now, I highly doubt that Chet would be willing to break backwards compatibility. *** would be reasonable, otherwise.
                      – Chris Down
                      Jan 21 '14 at 10:43










                    • Chet has done far worse backwards compatibility breaking before (for instance to align =~ with ksh93's). Changing **/ is more likely to fix scripts than break them. GNU grep has broken a (decade long) backward compatibility by making -r stop follow symlinks at some point.
                      – Stéphane Chazelas
                      Jan 21 '14 at 11:12













                    up vote
                    2
                    down vote










                    up vote
                    2
                    down vote









                    Word expansion is performed during the expansion of unquoted command substitutions, which is why this does not work.



                    If you have bash4+, just use globstar:



                    shopt -s globstar
                    for file in **/*.jpg; do
                    cp -- "$file" "$file%.*-fanart.jpg"
                    done





                    share|improve this answer












                    Word expansion is performed during the expansion of unquoted command substitutions, which is why this does not work.



                    If you have bash4+, just use globstar:



                    shopt -s globstar
                    for file in **/*.jpg; do
                    cp -- "$file" "$file%.*-fanart.jpg"
                    done






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jan 21 '14 at 3:29









                    Chris Down

                    77.9k13186199




                    77.9k13186199











                    • That assumes there is no symlink to directories in there. If you're going to use **/ best is to use zsh or ksh93 which don't have the problem.
                      – Stéphane Chazelas
                      Jan 21 '14 at 7:14










                    • @StephaneChazelas Traversing symlinks to directories is not universally undesirable (although I agree, it is not ideal in many situations). I am still pushing for there to be an option to disable recursion in 4.3.
                      – Chris Down
                      Jan 21 '14 at 7:51










                    • I agree make **/ not follow (like in zsh, ksh93 or tcsh) and make the "following symlinks" an option would be better. However, options a la shglob are nice but sometimes add more issues than they solve. It's better to enable the unexpected behaviour per-glob than globally. Using ***/ like in zsh or tcsh would be better. Having globbing qualifiers to enable the noglob, nullglob, nocaseglob, failglob... per-glob like in zsh would also be a lot better.
                      – Stéphane Chazelas
                      Jan 21 '14 at 10:30











                    • @StephaneChazelas Considering how long bash 4 has been out now, I highly doubt that Chet would be willing to break backwards compatibility. *** would be reasonable, otherwise.
                      – Chris Down
                      Jan 21 '14 at 10:43










                    • Chet has done far worse backwards compatibility breaking before (for instance to align =~ with ksh93's). Changing **/ is more likely to fix scripts than break them. GNU grep has broken a (decade long) backward compatibility by making -r stop follow symlinks at some point.
                      – Stéphane Chazelas
                      Jan 21 '14 at 11:12

















                    • That assumes there is no symlink to directories in there. If you're going to use **/ best is to use zsh or ksh93 which don't have the problem.
                      – Stéphane Chazelas
                      Jan 21 '14 at 7:14










                    • @StephaneChazelas Traversing symlinks to directories is not universally undesirable (although I agree, it is not ideal in many situations). I am still pushing for there to be an option to disable recursion in 4.3.
                      – Chris Down
                      Jan 21 '14 at 7:51










                    • I agree make **/ not follow (like in zsh, ksh93 or tcsh) and make the "following symlinks" an option would be better. However, options a la shglob are nice but sometimes add more issues than they solve. It's better to enable the unexpected behaviour per-glob than globally. Using ***/ like in zsh or tcsh would be better. Having globbing qualifiers to enable the noglob, nullglob, nocaseglob, failglob... per-glob like in zsh would also be a lot better.
                      – Stéphane Chazelas
                      Jan 21 '14 at 10:30











                    • @StephaneChazelas Considering how long bash 4 has been out now, I highly doubt that Chet would be willing to break backwards compatibility. *** would be reasonable, otherwise.
                      – Chris Down
                      Jan 21 '14 at 10:43










                    • Chet has done far worse backwards compatibility breaking before (for instance to align =~ with ksh93's). Changing **/ is more likely to fix scripts than break them. GNU grep has broken a (decade long) backward compatibility by making -r stop follow symlinks at some point.
                      – Stéphane Chazelas
                      Jan 21 '14 at 11:12
















                    That assumes there is no symlink to directories in there. If you're going to use **/ best is to use zsh or ksh93 which don't have the problem.
                    – Stéphane Chazelas
                    Jan 21 '14 at 7:14




                    That assumes there is no symlink to directories in there. If you're going to use **/ best is to use zsh or ksh93 which don't have the problem.
                    – Stéphane Chazelas
                    Jan 21 '14 at 7:14












                    @StephaneChazelas Traversing symlinks to directories is not universally undesirable (although I agree, it is not ideal in many situations). I am still pushing for there to be an option to disable recursion in 4.3.
                    – Chris Down
                    Jan 21 '14 at 7:51




                    @StephaneChazelas Traversing symlinks to directories is not universally undesirable (although I agree, it is not ideal in many situations). I am still pushing for there to be an option to disable recursion in 4.3.
                    – Chris Down
                    Jan 21 '14 at 7:51












                    I agree make **/ not follow (like in zsh, ksh93 or tcsh) and make the "following symlinks" an option would be better. However, options a la shglob are nice but sometimes add more issues than they solve. It's better to enable the unexpected behaviour per-glob than globally. Using ***/ like in zsh or tcsh would be better. Having globbing qualifiers to enable the noglob, nullglob, nocaseglob, failglob... per-glob like in zsh would also be a lot better.
                    – Stéphane Chazelas
                    Jan 21 '14 at 10:30





                    I agree make **/ not follow (like in zsh, ksh93 or tcsh) and make the "following symlinks" an option would be better. However, options a la shglob are nice but sometimes add more issues than they solve. It's better to enable the unexpected behaviour per-glob than globally. Using ***/ like in zsh or tcsh would be better. Having globbing qualifiers to enable the noglob, nullglob, nocaseglob, failglob... per-glob like in zsh would also be a lot better.
                    – Stéphane Chazelas
                    Jan 21 '14 at 10:30













                    @StephaneChazelas Considering how long bash 4 has been out now, I highly doubt that Chet would be willing to break backwards compatibility. *** would be reasonable, otherwise.
                    – Chris Down
                    Jan 21 '14 at 10:43




                    @StephaneChazelas Considering how long bash 4 has been out now, I highly doubt that Chet would be willing to break backwards compatibility. *** would be reasonable, otherwise.
                    – Chris Down
                    Jan 21 '14 at 10:43












                    Chet has done far worse backwards compatibility breaking before (for instance to align =~ with ksh93's). Changing **/ is more likely to fix scripts than break them. GNU grep has broken a (decade long) backward compatibility by making -r stop follow symlinks at some point.
                    – Stéphane Chazelas
                    Jan 21 '14 at 11:12





                    Chet has done far worse backwards compatibility breaking before (for instance to align =~ with ksh93's). Changing **/ is more likely to fix scripts than break them. GNU grep has broken a (decade long) backward compatibility by making -r stop follow symlinks at some point.
                    – Stéphane Chazelas
                    Jan 21 '14 at 11:12


















                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f110210%2fcopying-recursively-files-with-spaces%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

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

                    Displaying single band from multi-band raster using QGIS

                    How many registers does an x86_64 CPU actually have?