Copy files in Linux, avoid the copy if files do exist in destination












22














I need to copy a /home/user folder from one hard disk to another one. It has 100,000 files and around 10G size.



I use




cp -r /origin /destination




sometimes I get some errors due to broken links, permissions and so on. So I fix the error, and need to start again the copy.



I wonder how could I tell the command "cp", once it tries to copy again, not to copy files again if they exist in the destination folder.










share|improve this question















migrated from stackoverflow.com Mar 11 '10 at 16:51


This question came from our site for professional and enthusiast programmers.











  • 1




    Don't us cp. Use rsync instead.
    – William Pursell
    Mar 11 '10 at 16:08


















22














I need to copy a /home/user folder from one hard disk to another one. It has 100,000 files and around 10G size.



I use




cp -r /origin /destination




sometimes I get some errors due to broken links, permissions and so on. So I fix the error, and need to start again the copy.



I wonder how could I tell the command "cp", once it tries to copy again, not to copy files again if they exist in the destination folder.










share|improve this question















migrated from stackoverflow.com Mar 11 '10 at 16:51


This question came from our site for professional and enthusiast programmers.











  • 1




    Don't us cp. Use rsync instead.
    – William Pursell
    Mar 11 '10 at 16:08
















22












22








22


8





I need to copy a /home/user folder from one hard disk to another one. It has 100,000 files and around 10G size.



I use




cp -r /origin /destination




sometimes I get some errors due to broken links, permissions and so on. So I fix the error, and need to start again the copy.



I wonder how could I tell the command "cp", once it tries to copy again, not to copy files again if they exist in the destination folder.










share|improve this question















I need to copy a /home/user folder from one hard disk to another one. It has 100,000 files and around 10G size.



I use




cp -r /origin /destination




sometimes I get some errors due to broken links, permissions and so on. So I fix the error, and need to start again the copy.



I wonder how could I tell the command "cp", once it tries to copy again, not to copy files again if they exist in the destination folder.







linux bash






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 11 '11 at 16:31









studiohack

11.3k1880113




11.3k1880113










asked Mar 11 '10 at 15:59









flow

2,011133964




2,011133964




migrated from stackoverflow.com Mar 11 '10 at 16:51


This question came from our site for professional and enthusiast programmers.






migrated from stackoverflow.com Mar 11 '10 at 16:51


This question came from our site for professional and enthusiast programmers.










  • 1




    Don't us cp. Use rsync instead.
    – William Pursell
    Mar 11 '10 at 16:08
















  • 1




    Don't us cp. Use rsync instead.
    – William Pursell
    Mar 11 '10 at 16:08










1




1




Don't us cp. Use rsync instead.
– William Pursell
Mar 11 '10 at 16:08






Don't us cp. Use rsync instead.
– William Pursell
Mar 11 '10 at 16:08












7 Answers
7






active

oldest

votes


















17














cp -R -u -p /source /destination



The -u (or --update) flag does just this:



From the man page for cp:




-u, --update



copy only when the SOURCE file is newer than the destination file or when the destination file is missing







share|improve this answer



















  • 4




    To literally only copy files that don't exist and not update existing ones, yes n | cp -i /source/* /destination 2>/dev/null
    – sventechie
    Nov 21 '13 at 20:14








  • 3




    -u, --update copy only when the SOURCE file is newer than the destination file or when the destination file is missing -u, --update copy only when the SOURCE file is newer than the destination file or when the destination file is missing -p same as --preserve=mode,ownership,timestamps --preserve[=ATTR_LIST] preserve the specified attributes (default: mode,ownership,time‐ stamps), if possible additional attributes: context, links, xattr, all
    – Covich
    Feb 16 '16 at 15:29





















20














Just use cp -n <source> <dest>.



From man page:




-n, --no-clobber



do NOT overwrite an existing file (overrides a previous -i option)







share|improve this answer



















  • 4




    This is the only true answer to the question.
    – sebix
    Mar 21 '17 at 18:19



















16














rsync -aq /src /dest



Apart from only copying newer files, it will even only copy the newer parts of files if the file has changed. It's intended for copying over network links where you want to minimise the amount of data - but it also works great locally.






share|improve this answer























  • I can't add `` formatting to your post because it's only two characters. How would you feel about editing this and adding `` characters around your command?
    – culix
    Apr 24 '16 at 18:02



















4














Look up the "-u" option for the cp command.






share|improve this answer

















  • 2




    Disagree that using -u is a good idea. -u copies only when source is newer or destination is missing. Original poster's issue was caused by file copy breaking for whatever reason. OP would therefore have half-written file that wasn't updated when re-running the copy. That file might be important to someone! rsync solves this problem.
    – hazymat
    Dec 21 '13 at 21:47



















1














You should be copying as root to maintain permissions/ownership



# cp -au


Also look at rsync






share|improve this answer





























    1














    All above answers are correct but if you are doing this recursively then



    you should do:



     cp -rn SOURCE_PATH DESTINATION_PATH





    share|improve this answer





























      0














      POSIX solution



      Other answers use -u or -n options of cp. Neither of these is required by POSIX; nor is rsync from yet another answer; nor is yes used in one of the comments.



      Still we can reproduce yes n with a while loop. This leads to the following POSIX solution:



      while true; do echo n; done | cp -Ri /origin /destination 2>/dev/null





      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',
        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%2fsuperuser.com%2fquestions%2f118781%2fcopy-files-in-linux-avoid-the-copy-if-files-do-exist-in-destination%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        7 Answers
        7






        active

        oldest

        votes








        7 Answers
        7






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        17














        cp -R -u -p /source /destination



        The -u (or --update) flag does just this:



        From the man page for cp:




        -u, --update



        copy only when the SOURCE file is newer than the destination file or when the destination file is missing







        share|improve this answer



















        • 4




          To literally only copy files that don't exist and not update existing ones, yes n | cp -i /source/* /destination 2>/dev/null
          – sventechie
          Nov 21 '13 at 20:14








        • 3




          -u, --update copy only when the SOURCE file is newer than the destination file or when the destination file is missing -u, --update copy only when the SOURCE file is newer than the destination file or when the destination file is missing -p same as --preserve=mode,ownership,timestamps --preserve[=ATTR_LIST] preserve the specified attributes (default: mode,ownership,time‐ stamps), if possible additional attributes: context, links, xattr, all
          – Covich
          Feb 16 '16 at 15:29


















        17














        cp -R -u -p /source /destination



        The -u (or --update) flag does just this:



        From the man page for cp:




        -u, --update



        copy only when the SOURCE file is newer than the destination file or when the destination file is missing







        share|improve this answer



















        • 4




          To literally only copy files that don't exist and not update existing ones, yes n | cp -i /source/* /destination 2>/dev/null
          – sventechie
          Nov 21 '13 at 20:14








        • 3




          -u, --update copy only when the SOURCE file is newer than the destination file or when the destination file is missing -u, --update copy only when the SOURCE file is newer than the destination file or when the destination file is missing -p same as --preserve=mode,ownership,timestamps --preserve[=ATTR_LIST] preserve the specified attributes (default: mode,ownership,time‐ stamps), if possible additional attributes: context, links, xattr, all
          – Covich
          Feb 16 '16 at 15:29
















        17












        17








        17






        cp -R -u -p /source /destination



        The -u (or --update) flag does just this:



        From the man page for cp:




        -u, --update



        copy only when the SOURCE file is newer than the destination file or when the destination file is missing







        share|improve this answer














        cp -R -u -p /source /destination



        The -u (or --update) flag does just this:



        From the man page for cp:




        -u, --update



        copy only when the SOURCE file is newer than the destination file or when the destination file is missing








        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 23 '18 at 0:39









        ifconfig

        1731215




        1731215










        answered Mar 11 '10 at 16:02









        user31894

        2,105129




        2,105129








        • 4




          To literally only copy files that don't exist and not update existing ones, yes n | cp -i /source/* /destination 2>/dev/null
          – sventechie
          Nov 21 '13 at 20:14








        • 3




          -u, --update copy only when the SOURCE file is newer than the destination file or when the destination file is missing -u, --update copy only when the SOURCE file is newer than the destination file or when the destination file is missing -p same as --preserve=mode,ownership,timestamps --preserve[=ATTR_LIST] preserve the specified attributes (default: mode,ownership,time‐ stamps), if possible additional attributes: context, links, xattr, all
          – Covich
          Feb 16 '16 at 15:29
















        • 4




          To literally only copy files that don't exist and not update existing ones, yes n | cp -i /source/* /destination 2>/dev/null
          – sventechie
          Nov 21 '13 at 20:14








        • 3




          -u, --update copy only when the SOURCE file is newer than the destination file or when the destination file is missing -u, --update copy only when the SOURCE file is newer than the destination file or when the destination file is missing -p same as --preserve=mode,ownership,timestamps --preserve[=ATTR_LIST] preserve the specified attributes (default: mode,ownership,time‐ stamps), if possible additional attributes: context, links, xattr, all
          – Covich
          Feb 16 '16 at 15:29










        4




        4




        To literally only copy files that don't exist and not update existing ones, yes n | cp -i /source/* /destination 2>/dev/null
        – sventechie
        Nov 21 '13 at 20:14






        To literally only copy files that don't exist and not update existing ones, yes n | cp -i /source/* /destination 2>/dev/null
        – sventechie
        Nov 21 '13 at 20:14






        3




        3




        -u, --update copy only when the SOURCE file is newer than the destination file or when the destination file is missing -u, --update copy only when the SOURCE file is newer than the destination file or when the destination file is missing -p same as --preserve=mode,ownership,timestamps --preserve[=ATTR_LIST] preserve the specified attributes (default: mode,ownership,time‐ stamps), if possible additional attributes: context, links, xattr, all
        – Covich
        Feb 16 '16 at 15:29






        -u, --update copy only when the SOURCE file is newer than the destination file or when the destination file is missing -u, --update copy only when the SOURCE file is newer than the destination file or when the destination file is missing -p same as --preserve=mode,ownership,timestamps --preserve[=ATTR_LIST] preserve the specified attributes (default: mode,ownership,time‐ stamps), if possible additional attributes: context, links, xattr, all
        – Covich
        Feb 16 '16 at 15:29















        20














        Just use cp -n <source> <dest>.



        From man page:




        -n, --no-clobber



        do NOT overwrite an existing file (overrides a previous -i option)







        share|improve this answer



















        • 4




          This is the only true answer to the question.
          – sebix
          Mar 21 '17 at 18:19
















        20














        Just use cp -n <source> <dest>.



        From man page:




        -n, --no-clobber



        do NOT overwrite an existing file (overrides a previous -i option)







        share|improve this answer



















        • 4




          This is the only true answer to the question.
          – sebix
          Mar 21 '17 at 18:19














        20












        20








        20






        Just use cp -n <source> <dest>.



        From man page:




        -n, --no-clobber



        do NOT overwrite an existing file (overrides a previous -i option)







        share|improve this answer














        Just use cp -n <source> <dest>.



        From man page:




        -n, --no-clobber



        do NOT overwrite an existing file (overrides a previous -i option)








        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Jan 25 '17 at 15:59









        Adalee

        312410




        312410










        answered Jan 25 '17 at 13:45









        Balmipour

        32926




        32926








        • 4




          This is the only true answer to the question.
          – sebix
          Mar 21 '17 at 18:19














        • 4




          This is the only true answer to the question.
          – sebix
          Mar 21 '17 at 18:19








        4




        4




        This is the only true answer to the question.
        – sebix
        Mar 21 '17 at 18:19




        This is the only true answer to the question.
        – sebix
        Mar 21 '17 at 18:19











        16














        rsync -aq /src /dest



        Apart from only copying newer files, it will even only copy the newer parts of files if the file has changed. It's intended for copying over network links where you want to minimise the amount of data - but it also works great locally.






        share|improve this answer























        • I can't add `` formatting to your post because it's only two characters. How would you feel about editing this and adding `` characters around your command?
          – culix
          Apr 24 '16 at 18:02
















        16














        rsync -aq /src /dest



        Apart from only copying newer files, it will even only copy the newer parts of files if the file has changed. It's intended for copying over network links where you want to minimise the amount of data - but it also works great locally.






        share|improve this answer























        • I can't add `` formatting to your post because it's only two characters. How would you feel about editing this and adding `` characters around your command?
          – culix
          Apr 24 '16 at 18:02














        16












        16








        16






        rsync -aq /src /dest



        Apart from only copying newer files, it will even only copy the newer parts of files if the file has changed. It's intended for copying over network links where you want to minimise the amount of data - but it also works great locally.






        share|improve this answer














        rsync -aq /src /dest



        Apart from only copying newer files, it will even only copy the newer parts of files if the file has changed. It's intended for copying over network links where you want to minimise the amount of data - but it also works great locally.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Apr 24 '16 at 21:17

























        answered Mar 11 '10 at 16:12









        Martin Beckett

        4,80311925




        4,80311925












        • I can't add `` formatting to your post because it's only two characters. How would you feel about editing this and adding `` characters around your command?
          – culix
          Apr 24 '16 at 18:02


















        • I can't add `` formatting to your post because it's only two characters. How would you feel about editing this and adding `` characters around your command?
          – culix
          Apr 24 '16 at 18:02
















        I can't add `` formatting to your post because it's only two characters. How would you feel about editing this and adding `` characters around your command?
        – culix
        Apr 24 '16 at 18:02




        I can't add `` formatting to your post because it's only two characters. How would you feel about editing this and adding `` characters around your command?
        – culix
        Apr 24 '16 at 18:02











        4














        Look up the "-u" option for the cp command.






        share|improve this answer

















        • 2




          Disagree that using -u is a good idea. -u copies only when source is newer or destination is missing. Original poster's issue was caused by file copy breaking for whatever reason. OP would therefore have half-written file that wasn't updated when re-running the copy. That file might be important to someone! rsync solves this problem.
          – hazymat
          Dec 21 '13 at 21:47
















        4














        Look up the "-u" option for the cp command.






        share|improve this answer

















        • 2




          Disagree that using -u is a good idea. -u copies only when source is newer or destination is missing. Original poster's issue was caused by file copy breaking for whatever reason. OP would therefore have half-written file that wasn't updated when re-running the copy. That file might be important to someone! rsync solves this problem.
          – hazymat
          Dec 21 '13 at 21:47














        4












        4








        4






        Look up the "-u" option for the cp command.






        share|improve this answer












        Look up the "-u" option for the cp command.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 11 '10 at 16:01









        Pointy

        5931520




        5931520








        • 2




          Disagree that using -u is a good idea. -u copies only when source is newer or destination is missing. Original poster's issue was caused by file copy breaking for whatever reason. OP would therefore have half-written file that wasn't updated when re-running the copy. That file might be important to someone! rsync solves this problem.
          – hazymat
          Dec 21 '13 at 21:47














        • 2




          Disagree that using -u is a good idea. -u copies only when source is newer or destination is missing. Original poster's issue was caused by file copy breaking for whatever reason. OP would therefore have half-written file that wasn't updated when re-running the copy. That file might be important to someone! rsync solves this problem.
          – hazymat
          Dec 21 '13 at 21:47








        2




        2




        Disagree that using -u is a good idea. -u copies only when source is newer or destination is missing. Original poster's issue was caused by file copy breaking for whatever reason. OP would therefore have half-written file that wasn't updated when re-running the copy. That file might be important to someone! rsync solves this problem.
        – hazymat
        Dec 21 '13 at 21:47




        Disagree that using -u is a good idea. -u copies only when source is newer or destination is missing. Original poster's issue was caused by file copy breaking for whatever reason. OP would therefore have half-written file that wasn't updated when re-running the copy. That file might be important to someone! rsync solves this problem.
        – hazymat
        Dec 21 '13 at 21:47











        1














        You should be copying as root to maintain permissions/ownership



        # cp -au


        Also look at rsync






        share|improve this answer


























          1














          You should be copying as root to maintain permissions/ownership



          # cp -au


          Also look at rsync






          share|improve this answer
























            1












            1








            1






            You should be copying as root to maintain permissions/ownership



            # cp -au


            Also look at rsync






            share|improve this answer












            You should be copying as root to maintain permissions/ownership



            # cp -au


            Also look at rsync







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 11 '10 at 16:09









            pixelbeat

            85066




            85066























                1














                All above answers are correct but if you are doing this recursively then



                you should do:



                 cp -rn SOURCE_PATH DESTINATION_PATH





                share|improve this answer


























                  1














                  All above answers are correct but if you are doing this recursively then



                  you should do:



                   cp -rn SOURCE_PATH DESTINATION_PATH





                  share|improve this answer
























                    1












                    1








                    1






                    All above answers are correct but if you are doing this recursively then



                    you should do:



                     cp -rn SOURCE_PATH DESTINATION_PATH





                    share|improve this answer












                    All above answers are correct but if you are doing this recursively then



                    you should do:



                     cp -rn SOURCE_PATH DESTINATION_PATH






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jul 8 '18 at 4:58









                    grepit

                    1236




                    1236























                        0














                        POSIX solution



                        Other answers use -u or -n options of cp. Neither of these is required by POSIX; nor is rsync from yet another answer; nor is yes used in one of the comments.



                        Still we can reproduce yes n with a while loop. This leads to the following POSIX solution:



                        while true; do echo n; done | cp -Ri /origin /destination 2>/dev/null





                        share|improve this answer


























                          0














                          POSIX solution



                          Other answers use -u or -n options of cp. Neither of these is required by POSIX; nor is rsync from yet another answer; nor is yes used in one of the comments.



                          Still we can reproduce yes n with a while loop. This leads to the following POSIX solution:



                          while true; do echo n; done | cp -Ri /origin /destination 2>/dev/null





                          share|improve this answer
























                            0












                            0








                            0






                            POSIX solution



                            Other answers use -u or -n options of cp. Neither of these is required by POSIX; nor is rsync from yet another answer; nor is yes used in one of the comments.



                            Still we can reproduce yes n with a while loop. This leads to the following POSIX solution:



                            while true; do echo n; done | cp -Ri /origin /destination 2>/dev/null





                            share|improve this answer












                            POSIX solution



                            Other answers use -u or -n options of cp. Neither of these is required by POSIX; nor is rsync from yet another answer; nor is yes used in one of the comments.



                            Still we can reproduce yes n with a while loop. This leads to the following POSIX solution:



                            while true; do echo n; done | cp -Ri /origin /destination 2>/dev/null






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 23 '18 at 6:33









                            Kamil Maciorowski

                            24.5k155277




                            24.5k155277






























                                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%2f118781%2fcopy-files-in-linux-avoid-the-copy-if-files-do-exist-in-destination%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

                                AnyDesk - Fatal Program Failure

                                How to calibrate 16:9 built-in touch-screen to a 4:3 resolution?

                                QoS: MAC-Priority for clients behind a repeater