Copy only certain file types from a folder structure to another

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












4















I have a top folder with many sub-folders. It's named "a". There are many .png and .jpg files in there. I'd like to recursively copy "a" into a new folder "b", but only copy the .png and .jpg files. How do I achieve that?










share|improve this question



















  • 2





    rsync -a --include='*.png' --include='*.jpg' --include='*/' --exclude='*' a/ b/ If you want to prune empty dirs add the -m switch : rsync -am ....

    – don_crissti
    Feb 21 '16 at 20:18






  • 1





    Please specify whether you want to recreate the directory structure (including only the jpg/png files) under b or you just want to recursively search for jpg/png under a and copy them to b ?

    – don_crissti
    Feb 21 '16 at 21:02







  • 2





    If you want to copy the directory tree recursively, this is a duplicate of unix.stackexchange.com/questions/2161/… . If you want to put all the files directly under b, I can't find a duplicate. Which is it?

    – Gilles
    Feb 21 '16 at 21:50















4















I have a top folder with many sub-folders. It's named "a". There are many .png and .jpg files in there. I'd like to recursively copy "a" into a new folder "b", but only copy the .png and .jpg files. How do I achieve that?










share|improve this question



















  • 2





    rsync -a --include='*.png' --include='*.jpg' --include='*/' --exclude='*' a/ b/ If you want to prune empty dirs add the -m switch : rsync -am ....

    – don_crissti
    Feb 21 '16 at 20:18






  • 1





    Please specify whether you want to recreate the directory structure (including only the jpg/png files) under b or you just want to recursively search for jpg/png under a and copy them to b ?

    – don_crissti
    Feb 21 '16 at 21:02







  • 2





    If you want to copy the directory tree recursively, this is a duplicate of unix.stackexchange.com/questions/2161/… . If you want to put all the files directly under b, I can't find a duplicate. Which is it?

    – Gilles
    Feb 21 '16 at 21:50













4












4








4


1






I have a top folder with many sub-folders. It's named "a". There are many .png and .jpg files in there. I'd like to recursively copy "a" into a new folder "b", but only copy the .png and .jpg files. How do I achieve that?










share|improve this question
















I have a top folder with many sub-folders. It's named "a". There are many .png and .jpg files in there. I'd like to recursively copy "a" into a new folder "b", but only copy the .png and .jpg files. How do I achieve that?







files file-copy recursive






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 21 '16 at 23:35









don_crissti

51.4k15138167




51.4k15138167










asked Feb 21 '16 at 17:14









JasonGenXJasonGenX

12314




12314







  • 2





    rsync -a --include='*.png' --include='*.jpg' --include='*/' --exclude='*' a/ b/ If you want to prune empty dirs add the -m switch : rsync -am ....

    – don_crissti
    Feb 21 '16 at 20:18






  • 1





    Please specify whether you want to recreate the directory structure (including only the jpg/png files) under b or you just want to recursively search for jpg/png under a and copy them to b ?

    – don_crissti
    Feb 21 '16 at 21:02







  • 2





    If you want to copy the directory tree recursively, this is a duplicate of unix.stackexchange.com/questions/2161/… . If you want to put all the files directly under b, I can't find a duplicate. Which is it?

    – Gilles
    Feb 21 '16 at 21:50












  • 2





    rsync -a --include='*.png' --include='*.jpg' --include='*/' --exclude='*' a/ b/ If you want to prune empty dirs add the -m switch : rsync -am ....

    – don_crissti
    Feb 21 '16 at 20:18






  • 1





    Please specify whether you want to recreate the directory structure (including only the jpg/png files) under b or you just want to recursively search for jpg/png under a and copy them to b ?

    – don_crissti
    Feb 21 '16 at 21:02







  • 2





    If you want to copy the directory tree recursively, this is a duplicate of unix.stackexchange.com/questions/2161/… . If you want to put all the files directly under b, I can't find a duplicate. Which is it?

    – Gilles
    Feb 21 '16 at 21:50







2




2





rsync -a --include='*.png' --include='*.jpg' --include='*/' --exclude='*' a/ b/ If you want to prune empty dirs add the -m switch : rsync -am ....

– don_crissti
Feb 21 '16 at 20:18





rsync -a --include='*.png' --include='*.jpg' --include='*/' --exclude='*' a/ b/ If you want to prune empty dirs add the -m switch : rsync -am ....

– don_crissti
Feb 21 '16 at 20:18




1




1





Please specify whether you want to recreate the directory structure (including only the jpg/png files) under b or you just want to recursively search for jpg/png under a and copy them to b ?

– don_crissti
Feb 21 '16 at 21:02






Please specify whether you want to recreate the directory structure (including only the jpg/png files) under b or you just want to recursively search for jpg/png under a and copy them to b ?

– don_crissti
Feb 21 '16 at 21:02





2




2





If you want to copy the directory tree recursively, this is a duplicate of unix.stackexchange.com/questions/2161/… . If you want to put all the files directly under b, I can't find a duplicate. Which is it?

– Gilles
Feb 21 '16 at 21:50





If you want to copy the directory tree recursively, this is a duplicate of unix.stackexchange.com/questions/2161/… . If you want to put all the files directly under b, I can't find a duplicate. Which is it?

– Gilles
Feb 21 '16 at 21:50










4 Answers
4






active

oldest

votes


















2














find a ( -name "*.png" -or -name "*.jpg" ) -exec cp b ;





share|improve this answer

























  • Whoops, I accidentally used *.png twice and forgot .jpg. This is what happens when I try to answer questions while listening to music. Thanks for catching my mistake.

    – gardenhead
    Feb 21 '16 at 20:56











  • OK, I think I got it this time. I learned something new about find today. Thanks.

    – gardenhead
    Feb 21 '16 at 21:06











  • One more thing: stick with -o instead of -or, it's more portable.

    – don_crissti
    Feb 21 '16 at 21:13


















1














for file in $(find a -name "*.jpg" -o -name "*.png")
do
cp $file b/$file
done





share|improve this answer
































    1














    One-liner



    cp $(find a -name "*.jpg" -o -name "*.png") b





    share|improve this answer






























      0














      A bit shorter if there are more file types, using brace expansion



      cp -r source_directory/*.png,jpg,jpeg target_directory


      This gives an error whenever a file of a type does not exist. see https://serverfault.com/a/153893

      Add 2>/dev/null to hide these errors

      Add || : to not cause an exit code



      cp -r source_directory/*.png,jpg,jpeg target_directory 2>/dev/null || :





      share|improve this answer






















        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',
        autoActivateHeartbeat: false,
        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%2f264798%2fcopy-only-certain-file-types-from-a-folder-structure-to-another%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









        2














        find a ( -name "*.png" -or -name "*.jpg" ) -exec cp b ;





        share|improve this answer

























        • Whoops, I accidentally used *.png twice and forgot .jpg. This is what happens when I try to answer questions while listening to music. Thanks for catching my mistake.

          – gardenhead
          Feb 21 '16 at 20:56











        • OK, I think I got it this time. I learned something new about find today. Thanks.

          – gardenhead
          Feb 21 '16 at 21:06











        • One more thing: stick with -o instead of -or, it's more portable.

          – don_crissti
          Feb 21 '16 at 21:13















        2














        find a ( -name "*.png" -or -name "*.jpg" ) -exec cp b ;





        share|improve this answer

























        • Whoops, I accidentally used *.png twice and forgot .jpg. This is what happens when I try to answer questions while listening to music. Thanks for catching my mistake.

          – gardenhead
          Feb 21 '16 at 20:56











        • OK, I think I got it this time. I learned something new about find today. Thanks.

          – gardenhead
          Feb 21 '16 at 21:06











        • One more thing: stick with -o instead of -or, it's more portable.

          – don_crissti
          Feb 21 '16 at 21:13













        2












        2








        2







        find a ( -name "*.png" -or -name "*.jpg" ) -exec cp b ;





        share|improve this answer















        find a ( -name "*.png" -or -name "*.jpg" ) -exec cp b ;






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Feb 21 '16 at 21:06

























        answered Feb 21 '16 at 20:46









        gardenheadgardenhead

        1,3072710




        1,3072710












        • Whoops, I accidentally used *.png twice and forgot .jpg. This is what happens when I try to answer questions while listening to music. Thanks for catching my mistake.

          – gardenhead
          Feb 21 '16 at 20:56











        • OK, I think I got it this time. I learned something new about find today. Thanks.

          – gardenhead
          Feb 21 '16 at 21:06











        • One more thing: stick with -o instead of -or, it's more portable.

          – don_crissti
          Feb 21 '16 at 21:13

















        • Whoops, I accidentally used *.png twice and forgot .jpg. This is what happens when I try to answer questions while listening to music. Thanks for catching my mistake.

          – gardenhead
          Feb 21 '16 at 20:56











        • OK, I think I got it this time. I learned something new about find today. Thanks.

          – gardenhead
          Feb 21 '16 at 21:06











        • One more thing: stick with -o instead of -or, it's more portable.

          – don_crissti
          Feb 21 '16 at 21:13
















        Whoops, I accidentally used *.png twice and forgot .jpg. This is what happens when I try to answer questions while listening to music. Thanks for catching my mistake.

        – gardenhead
        Feb 21 '16 at 20:56





        Whoops, I accidentally used *.png twice and forgot .jpg. This is what happens when I try to answer questions while listening to music. Thanks for catching my mistake.

        – gardenhead
        Feb 21 '16 at 20:56













        OK, I think I got it this time. I learned something new about find today. Thanks.

        – gardenhead
        Feb 21 '16 at 21:06





        OK, I think I got it this time. I learned something new about find today. Thanks.

        – gardenhead
        Feb 21 '16 at 21:06













        One more thing: stick with -o instead of -or, it's more portable.

        – don_crissti
        Feb 21 '16 at 21:13





        One more thing: stick with -o instead of -or, it's more portable.

        – don_crissti
        Feb 21 '16 at 21:13













        1














        for file in $(find a -name "*.jpg" -o -name "*.png")
        do
        cp $file b/$file
        done





        share|improve this answer





























          1














          for file in $(find a -name "*.jpg" -o -name "*.png")
          do
          cp $file b/$file
          done





          share|improve this answer



























            1












            1








            1







            for file in $(find a -name "*.jpg" -o -name "*.png")
            do
            cp $file b/$file
            done





            share|improve this answer















            for file in $(find a -name "*.jpg" -o -name "*.png")
            do
            cp $file b/$file
            done






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Feb 21 '16 at 18:35









            Jakuje

            16.5k53155




            16.5k53155










            answered Feb 21 '16 at 17:43









            MelBurslanMelBurslan

            5,33611533




            5,33611533





















                1














                One-liner



                cp $(find a -name "*.jpg" -o -name "*.png") b





                share|improve this answer



























                  1














                  One-liner



                  cp $(find a -name "*.jpg" -o -name "*.png") b





                  share|improve this answer

























                    1












                    1








                    1







                    One-liner



                    cp $(find a -name "*.jpg" -o -name "*.png") b





                    share|improve this answer













                    One-liner



                    cp $(find a -name "*.jpg" -o -name "*.png") b






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Feb 21 '16 at 18:48









                    MarwareMarware

                    386210




                    386210





















                        0














                        A bit shorter if there are more file types, using brace expansion



                        cp -r source_directory/*.png,jpg,jpeg target_directory


                        This gives an error whenever a file of a type does not exist. see https://serverfault.com/a/153893

                        Add 2>/dev/null to hide these errors

                        Add || : to not cause an exit code



                        cp -r source_directory/*.png,jpg,jpeg target_directory 2>/dev/null || :





                        share|improve this answer



























                          0














                          A bit shorter if there are more file types, using brace expansion



                          cp -r source_directory/*.png,jpg,jpeg target_directory


                          This gives an error whenever a file of a type does not exist. see https://serverfault.com/a/153893

                          Add 2>/dev/null to hide these errors

                          Add || : to not cause an exit code



                          cp -r source_directory/*.png,jpg,jpeg target_directory 2>/dev/null || :





                          share|improve this answer

























                            0












                            0








                            0







                            A bit shorter if there are more file types, using brace expansion



                            cp -r source_directory/*.png,jpg,jpeg target_directory


                            This gives an error whenever a file of a type does not exist. see https://serverfault.com/a/153893

                            Add 2>/dev/null to hide these errors

                            Add || : to not cause an exit code



                            cp -r source_directory/*.png,jpg,jpeg target_directory 2>/dev/null || :





                            share|improve this answer













                            A bit shorter if there are more file types, using brace expansion



                            cp -r source_directory/*.png,jpg,jpeg target_directory


                            This gives an error whenever a file of a type does not exist. see https://serverfault.com/a/153893

                            Add 2>/dev/null to hide these errors

                            Add || : to not cause an exit code



                            cp -r source_directory/*.png,jpg,jpeg target_directory 2>/dev/null || :






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Feb 15 at 13:53









                            Roger BartonRoger Barton

                            1




                            1



























                                draft saved

                                draft discarded
















































                                Thanks for contributing an answer to Unix & Linux Stack Exchange!


                                • 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%2funix.stackexchange.com%2fquestions%2f264798%2fcopy-only-certain-file-types-from-a-folder-structure-to-another%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

                                Peggy Mitchell

                                Palaiologos

                                The Forum (Inglewood, California)