Is there a way to check whether `cp -r` would overwrite anything?












2















Is there a way to check if cp -r would overwrite anything, without actually copying anything?



-n seems to copy files that don't collide, which is not what I want.










share|improve this question

























  • Possible duplicate of Linux how to copy but not overwrite?

    – l'L'l
    Jan 1 at 18:13











  • Cross-site duplicate: unix.stackexchange.com/questions/187937/…

    – tripleee
    Jan 2 at 5:12
















2















Is there a way to check if cp -r would overwrite anything, without actually copying anything?



-n seems to copy files that don't collide, which is not what I want.










share|improve this question

























  • Possible duplicate of Linux how to copy but not overwrite?

    – l'L'l
    Jan 1 at 18:13











  • Cross-site duplicate: unix.stackexchange.com/questions/187937/…

    – tripleee
    Jan 2 at 5:12














2












2








2








Is there a way to check if cp -r would overwrite anything, without actually copying anything?



-n seems to copy files that don't collide, which is not what I want.










share|improve this question
















Is there a way to check if cp -r would overwrite anything, without actually copying anything?



-n seems to copy files that don't collide, which is not what I want.







bash cp






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 10:43









common sense

2,57351625




2,57351625










asked Jan 1 at 17:59









Filip HaglundFilip Haglund

7,10064281




7,10064281













  • Possible duplicate of Linux how to copy but not overwrite?

    – l'L'l
    Jan 1 at 18:13











  • Cross-site duplicate: unix.stackexchange.com/questions/187937/…

    – tripleee
    Jan 2 at 5:12



















  • Possible duplicate of Linux how to copy but not overwrite?

    – l'L'l
    Jan 1 at 18:13











  • Cross-site duplicate: unix.stackexchange.com/questions/187937/…

    – tripleee
    Jan 2 at 5:12

















Possible duplicate of Linux how to copy but not overwrite?

– l'L'l
Jan 1 at 18:13





Possible duplicate of Linux how to copy but not overwrite?

– l'L'l
Jan 1 at 18:13













Cross-site duplicate: unix.stackexchange.com/questions/187937/…

– tripleee
Jan 2 at 5:12





Cross-site duplicate: unix.stackexchange.com/questions/187937/…

– tripleee
Jan 2 at 5:12












3 Answers
3






active

oldest

votes


















2














The easiest way is to use the rsync command there you have a lot of parameters to override the target or delete files on the target if you want.



https://linux.die.net/man/1/rsync



or like mentioned in the comments you could use cp -n for --no-clobber. But in general i would suggest rsync there you have a lot more possibilities.






share|improve this answer


























  • Agree. If you need to know, cp is not the right tool.

    – Thorbjørn Ravn Andersen
    Jan 1 at 18:03






  • 1





    Note that cp has the -i --interactive option and the -n, --no-clobber option.

    – Roadowl
    Jan 1 at 18:11



















0














cp has the -i --interactive option and the -n, --no-clobber option.
See `man 1 cp'.






share|improve this answer































    0














    To only check if the file exists in the target location, but not actually perform any copying you can use the diff command with the -r option.



    From man diff:




    -r, --recursive

    recursively compare any subdirectories found




    diff -r --brief --report-identical-files /source-folder/ /target-folder/ | grep -v '^Only in'




    • --brief is listing files that differ (same as -q)


    • --report-identical-files does the same for files that don't differ (same as -s)

    • The grep -v '^Only in' removes files that are only in one directory tree, because those will not be overwritten.


    The output will look something like this:



    Files /source-folder/file1.txt and /target-folder/file1.txt are identical
    Files /source-folder/file2.jpg and /target-folder/file2.jpg differ





    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%2f53997692%2fis-there-a-way-to-check-whether-cp-r-would-overwrite-anything%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      The easiest way is to use the rsync command there you have a lot of parameters to override the target or delete files on the target if you want.



      https://linux.die.net/man/1/rsync



      or like mentioned in the comments you could use cp -n for --no-clobber. But in general i would suggest rsync there you have a lot more possibilities.






      share|improve this answer


























      • Agree. If you need to know, cp is not the right tool.

        – Thorbjørn Ravn Andersen
        Jan 1 at 18:03






      • 1





        Note that cp has the -i --interactive option and the -n, --no-clobber option.

        – Roadowl
        Jan 1 at 18:11
















      2














      The easiest way is to use the rsync command there you have a lot of parameters to override the target or delete files on the target if you want.



      https://linux.die.net/man/1/rsync



      or like mentioned in the comments you could use cp -n for --no-clobber. But in general i would suggest rsync there you have a lot more possibilities.






      share|improve this answer


























      • Agree. If you need to know, cp is not the right tool.

        – Thorbjørn Ravn Andersen
        Jan 1 at 18:03






      • 1





        Note that cp has the -i --interactive option and the -n, --no-clobber option.

        – Roadowl
        Jan 1 at 18:11














      2












      2








      2







      The easiest way is to use the rsync command there you have a lot of parameters to override the target or delete files on the target if you want.



      https://linux.die.net/man/1/rsync



      or like mentioned in the comments you could use cp -n for --no-clobber. But in general i would suggest rsync there you have a lot more possibilities.






      share|improve this answer















      The easiest way is to use the rsync command there you have a lot of parameters to override the target or delete files on the target if you want.



      https://linux.die.net/man/1/rsync



      or like mentioned in the comments you could use cp -n for --no-clobber. But in general i would suggest rsync there you have a lot more possibilities.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Jan 1 at 18:15

























      answered Jan 1 at 18:02









      René HöhleRené Höhle

      20.4k135366




      20.4k135366













      • Agree. If you need to know, cp is not the right tool.

        – Thorbjørn Ravn Andersen
        Jan 1 at 18:03






      • 1





        Note that cp has the -i --interactive option and the -n, --no-clobber option.

        – Roadowl
        Jan 1 at 18:11



















      • Agree. If you need to know, cp is not the right tool.

        – Thorbjørn Ravn Andersen
        Jan 1 at 18:03






      • 1





        Note that cp has the -i --interactive option and the -n, --no-clobber option.

        – Roadowl
        Jan 1 at 18:11

















      Agree. If you need to know, cp is not the right tool.

      – Thorbjørn Ravn Andersen
      Jan 1 at 18:03





      Agree. If you need to know, cp is not the right tool.

      – Thorbjørn Ravn Andersen
      Jan 1 at 18:03




      1




      1





      Note that cp has the -i --interactive option and the -n, --no-clobber option.

      – Roadowl
      Jan 1 at 18:11





      Note that cp has the -i --interactive option and the -n, --no-clobber option.

      – Roadowl
      Jan 1 at 18:11













      0














      cp has the -i --interactive option and the -n, --no-clobber option.
      See `man 1 cp'.






      share|improve this answer




























        0














        cp has the -i --interactive option and the -n, --no-clobber option.
        See `man 1 cp'.






        share|improve this answer


























          0












          0








          0







          cp has the -i --interactive option and the -n, --no-clobber option.
          See `man 1 cp'.






          share|improve this answer













          cp has the -i --interactive option and the -n, --no-clobber option.
          See `man 1 cp'.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 1 at 18:13









          RoadowlRoadowl

          128310




          128310























              0














              To only check if the file exists in the target location, but not actually perform any copying you can use the diff command with the -r option.



              From man diff:




              -r, --recursive

              recursively compare any subdirectories found




              diff -r --brief --report-identical-files /source-folder/ /target-folder/ | grep -v '^Only in'




              • --brief is listing files that differ (same as -q)


              • --report-identical-files does the same for files that don't differ (same as -s)

              • The grep -v '^Only in' removes files that are only in one directory tree, because those will not be overwritten.


              The output will look something like this:



              Files /source-folder/file1.txt and /target-folder/file1.txt are identical
              Files /source-folder/file2.jpg and /target-folder/file2.jpg differ





              share|improve this answer




























                0














                To only check if the file exists in the target location, but not actually perform any copying you can use the diff command with the -r option.



                From man diff:




                -r, --recursive

                recursively compare any subdirectories found




                diff -r --brief --report-identical-files /source-folder/ /target-folder/ | grep -v '^Only in'




                • --brief is listing files that differ (same as -q)


                • --report-identical-files does the same for files that don't differ (same as -s)

                • The grep -v '^Only in' removes files that are only in one directory tree, because those will not be overwritten.


                The output will look something like this:



                Files /source-folder/file1.txt and /target-folder/file1.txt are identical
                Files /source-folder/file2.jpg and /target-folder/file2.jpg differ





                share|improve this answer


























                  0












                  0








                  0







                  To only check if the file exists in the target location, but not actually perform any copying you can use the diff command with the -r option.



                  From man diff:




                  -r, --recursive

                  recursively compare any subdirectories found




                  diff -r --brief --report-identical-files /source-folder/ /target-folder/ | grep -v '^Only in'




                  • --brief is listing files that differ (same as -q)


                  • --report-identical-files does the same for files that don't differ (same as -s)

                  • The grep -v '^Only in' removes files that are only in one directory tree, because those will not be overwritten.


                  The output will look something like this:



                  Files /source-folder/file1.txt and /target-folder/file1.txt are identical
                  Files /source-folder/file2.jpg and /target-folder/file2.jpg differ





                  share|improve this answer













                  To only check if the file exists in the target location, but not actually perform any copying you can use the diff command with the -r option.



                  From man diff:




                  -r, --recursive

                  recursively compare any subdirectories found




                  diff -r --brief --report-identical-files /source-folder/ /target-folder/ | grep -v '^Only in'




                  • --brief is listing files that differ (same as -q)


                  • --report-identical-files does the same for files that don't differ (same as -s)

                  • The grep -v '^Only in' removes files that are only in one directory tree, because those will not be overwritten.


                  The output will look something like this:



                  Files /source-folder/file1.txt and /target-folder/file1.txt are identical
                  Files /source-folder/file2.jpg and /target-folder/file2.jpg differ






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 2 at 11:09









                  Samuel KirschnerSamuel Kirschner

                  9351717




                  9351717






























                      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%2f53997692%2fis-there-a-way-to-check-whether-cp-r-would-overwrite-anything%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

                      'app-layout' is not a known element: how to share Component with different Modules

                      android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

                      WPF add header to Image with URL pettitions [duplicate]