Why `cp` is designed this way to overwrite existing files silently?

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











up vote
1
down vote

favorite












I create such commands to test cp commands



In [98]: ls 
first.html second.html third.html

In [99]: less first.html second.html third.html

In [100]: cat first.html
first

In [101]: cat second.html
second

In [102]: cat third.html
third


When I copy first.html to second.html,



In [103]: cp first.html second.html 
In [104]: cat second.html
first


The second.html is overwritten silently without any error prompts.

Nonetheless, If I do it in the desktop GUI, draw a file in with the same name, it will be suffixed an first1.html automatically.

Doing this avoided to overwrite an existing file accidentally.



Why not follow this pattern to design cp but to overwrite silently.



I am learning python now and found that it's shutil.copy completely inherit from the idea of command cp.



I assume it not a good design and could be easily amended by insert an if check.



Why cp is designed this way to overwrite existing files silently?










share|improve this question









New contributor




rider dragon is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 3




    I imagine only the coreutils designers can truly answer the question, but it's just the way it works for now. Usually the apps are built assuming the user really means what they're doing and to minimize the extra prompting. If you want to change the behavior, alias 'cp' to 'cp -i' or 'cp -n'.
    – kevlinux
    2 hours ago











  • Is it "do one thing at one time"? @kevlinux
    – rider dragon
    1 hour ago














up vote
1
down vote

favorite












I create such commands to test cp commands



In [98]: ls 
first.html second.html third.html

In [99]: less first.html second.html third.html

In [100]: cat first.html
first

In [101]: cat second.html
second

In [102]: cat third.html
third


When I copy first.html to second.html,



In [103]: cp first.html second.html 
In [104]: cat second.html
first


The second.html is overwritten silently without any error prompts.

Nonetheless, If I do it in the desktop GUI, draw a file in with the same name, it will be suffixed an first1.html automatically.

Doing this avoided to overwrite an existing file accidentally.



Why not follow this pattern to design cp but to overwrite silently.



I am learning python now and found that it's shutil.copy completely inherit from the idea of command cp.



I assume it not a good design and could be easily amended by insert an if check.



Why cp is designed this way to overwrite existing files silently?










share|improve this question









New contributor




rider dragon is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 3




    I imagine only the coreutils designers can truly answer the question, but it's just the way it works for now. Usually the apps are built assuming the user really means what they're doing and to minimize the extra prompting. If you want to change the behavior, alias 'cp' to 'cp -i' or 'cp -n'.
    – kevlinux
    2 hours ago











  • Is it "do one thing at one time"? @kevlinux
    – rider dragon
    1 hour ago












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I create such commands to test cp commands



In [98]: ls 
first.html second.html third.html

In [99]: less first.html second.html third.html

In [100]: cat first.html
first

In [101]: cat second.html
second

In [102]: cat third.html
third


When I copy first.html to second.html,



In [103]: cp first.html second.html 
In [104]: cat second.html
first


The second.html is overwritten silently without any error prompts.

Nonetheless, If I do it in the desktop GUI, draw a file in with the same name, it will be suffixed an first1.html automatically.

Doing this avoided to overwrite an existing file accidentally.



Why not follow this pattern to design cp but to overwrite silently.



I am learning python now and found that it's shutil.copy completely inherit from the idea of command cp.



I assume it not a good design and could be easily amended by insert an if check.



Why cp is designed this way to overwrite existing files silently?










share|improve this question









New contributor




rider dragon is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I create such commands to test cp commands



In [98]: ls 
first.html second.html third.html

In [99]: less first.html second.html third.html

In [100]: cat first.html
first

In [101]: cat second.html
second

In [102]: cat third.html
third


When I copy first.html to second.html,



In [103]: cp first.html second.html 
In [104]: cat second.html
first


The second.html is overwritten silently without any error prompts.

Nonetheless, If I do it in the desktop GUI, draw a file in with the same name, it will be suffixed an first1.html automatically.

Doing this avoided to overwrite an existing file accidentally.



Why not follow this pattern to design cp but to overwrite silently.



I am learning python now and found that it's shutil.copy completely inherit from the idea of command cp.



I assume it not a good design and could be easily amended by insert an if check.



Why cp is designed this way to overwrite existing files silently?







cp history






share|improve this question









New contributor




rider dragon is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




rider dragon is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 2 hours ago









dhag

10.9k32842




10.9k32842






New contributor




rider dragon is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 3 hours ago









rider dragon

1083




1083




New contributor




rider dragon is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





rider dragon is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






rider dragon is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







  • 3




    I imagine only the coreutils designers can truly answer the question, but it's just the way it works for now. Usually the apps are built assuming the user really means what they're doing and to minimize the extra prompting. If you want to change the behavior, alias 'cp' to 'cp -i' or 'cp -n'.
    – kevlinux
    2 hours ago











  • Is it "do one thing at one time"? @kevlinux
    – rider dragon
    1 hour ago












  • 3




    I imagine only the coreutils designers can truly answer the question, but it's just the way it works for now. Usually the apps are built assuming the user really means what they're doing and to minimize the extra prompting. If you want to change the behavior, alias 'cp' to 'cp -i' or 'cp -n'.
    – kevlinux
    2 hours ago











  • Is it "do one thing at one time"? @kevlinux
    – rider dragon
    1 hour ago







3




3




I imagine only the coreutils designers can truly answer the question, but it's just the way it works for now. Usually the apps are built assuming the user really means what they're doing and to minimize the extra prompting. If you want to change the behavior, alias 'cp' to 'cp -i' or 'cp -n'.
– kevlinux
2 hours ago





I imagine only the coreutils designers can truly answer the question, but it's just the way it works for now. Usually the apps are built assuming the user really means what they're doing and to minimize the extra prompting. If you want to change the behavior, alias 'cp' to 'cp -i' or 'cp -n'.
– kevlinux
2 hours ago













Is it "do one thing at one time"? @kevlinux
– rider dragon
1 hour ago




Is it "do one thing at one time"? @kevlinux
– rider dragon
1 hour ago










2 Answers
2






active

oldest

votes

















up vote
3
down vote



accepted










The default overwrite behavior of cp is specified in POSIX.





  1. If source_file is of type regular file, the following steps shall be taken:



    3.a. The behavior is unspecified if dest_file exists and was written by a previous step. Otherwise, if dest_file exists, the following steps shall be taken:



    3.a.i. If the -i option is in effect, the cp utility shall write a prompt to the standard error and read a line from the standard input. If the response is not affirmative, cp shall do nothing more with source_file and go on to any remaining files.



    3.a.ii. A file descriptor for dest_file shall be obtained by performing actions equivalent to the open() function defined in the System Interfaces volume of POSIX.1-2017 called using dest_file as the path argument, and the bitwise-inclusive OR of O_WRONLY and O_TRUNC as the oflag argument.



    3.a.iii. If the attempt to obtain a file descriptor fails and the -f option is in effect, cp shall attempt to remove the file by performing actions equivalent to the unlink() function defined in the System Interfaces volume of POSIX.1-2017 called using dest_file as the path argument. If this attempt succeeds, cp shall continue with step 3b.





When the POSIX specification was written, there already was a large number of scripts in existence, with a built-in assumption for the default overwrite behavior. Many of those scripts were designed to run without direct user presence, e.g. as cron jobs or other background tasks. Changing the behavior would have broken them. Reviewing and modifying them all to add an option to force overwriting wherever needed was probably considered a huge task with minimal benefits.



Also, the Unix command line was always designed to allow an experienced user to work efficiently, even at the expense of a hard learning curve for a beginner. When the user enters a command, the computer is to expect that the user really means it, without any second-guessing; it is the user's responsibility to be careful with potentially destructive commands.






share|improve this answer



























    up vote
    2
    down vote













    You may insert the "if check" yourself with



    alias cp='cp -i'


    The -i option to cp makes it ask interactively if the operation would overwrite the target and with the above alias, your interactive use of cp would enable this option by default.



    The cp utility is often used in scripts. These scripts may not always be run manually, but may be part of system maintenance tasks run by via cron or some similar facility. In those cases, there are no humans to ask for confirmation.



    To have a script avoid overwriting a file with cp one may insert an explicit check:



    if [ -e "$target" ]; then
    printf '%s already exists, refusing to overwriten' "$target" >&2
    exit 1
    fi
    cp "$source" "$target"


    I'm opting for exiting the script with an error message if $target exists in the above example. The existence of the target is obviously unexpected, and allowing the script to continue after detecting this may lead to other more severe issues.






    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',
      convertImagesToLinks: false,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );






      rider dragon is a new contributor. Be nice, and check out our Code of Conduct.









       

      draft saved


      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f477401%2fwhy-cp-is-designed-this-way-to-overwrite-existing-files-silently%23new-answer', 'question_page');

      );

      Post as a guest






























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      3
      down vote



      accepted










      The default overwrite behavior of cp is specified in POSIX.





      1. If source_file is of type regular file, the following steps shall be taken:



        3.a. The behavior is unspecified if dest_file exists and was written by a previous step. Otherwise, if dest_file exists, the following steps shall be taken:



        3.a.i. If the -i option is in effect, the cp utility shall write a prompt to the standard error and read a line from the standard input. If the response is not affirmative, cp shall do nothing more with source_file and go on to any remaining files.



        3.a.ii. A file descriptor for dest_file shall be obtained by performing actions equivalent to the open() function defined in the System Interfaces volume of POSIX.1-2017 called using dest_file as the path argument, and the bitwise-inclusive OR of O_WRONLY and O_TRUNC as the oflag argument.



        3.a.iii. If the attempt to obtain a file descriptor fails and the -f option is in effect, cp shall attempt to remove the file by performing actions equivalent to the unlink() function defined in the System Interfaces volume of POSIX.1-2017 called using dest_file as the path argument. If this attempt succeeds, cp shall continue with step 3b.





      When the POSIX specification was written, there already was a large number of scripts in existence, with a built-in assumption for the default overwrite behavior. Many of those scripts were designed to run without direct user presence, e.g. as cron jobs or other background tasks. Changing the behavior would have broken them. Reviewing and modifying them all to add an option to force overwriting wherever needed was probably considered a huge task with minimal benefits.



      Also, the Unix command line was always designed to allow an experienced user to work efficiently, even at the expense of a hard learning curve for a beginner. When the user enters a command, the computer is to expect that the user really means it, without any second-guessing; it is the user's responsibility to be careful with potentially destructive commands.






      share|improve this answer
























        up vote
        3
        down vote



        accepted










        The default overwrite behavior of cp is specified in POSIX.





        1. If source_file is of type regular file, the following steps shall be taken:



          3.a. The behavior is unspecified if dest_file exists and was written by a previous step. Otherwise, if dest_file exists, the following steps shall be taken:



          3.a.i. If the -i option is in effect, the cp utility shall write a prompt to the standard error and read a line from the standard input. If the response is not affirmative, cp shall do nothing more with source_file and go on to any remaining files.



          3.a.ii. A file descriptor for dest_file shall be obtained by performing actions equivalent to the open() function defined in the System Interfaces volume of POSIX.1-2017 called using dest_file as the path argument, and the bitwise-inclusive OR of O_WRONLY and O_TRUNC as the oflag argument.



          3.a.iii. If the attempt to obtain a file descriptor fails and the -f option is in effect, cp shall attempt to remove the file by performing actions equivalent to the unlink() function defined in the System Interfaces volume of POSIX.1-2017 called using dest_file as the path argument. If this attempt succeeds, cp shall continue with step 3b.





        When the POSIX specification was written, there already was a large number of scripts in existence, with a built-in assumption for the default overwrite behavior. Many of those scripts were designed to run without direct user presence, e.g. as cron jobs or other background tasks. Changing the behavior would have broken them. Reviewing and modifying them all to add an option to force overwriting wherever needed was probably considered a huge task with minimal benefits.



        Also, the Unix command line was always designed to allow an experienced user to work efficiently, even at the expense of a hard learning curve for a beginner. When the user enters a command, the computer is to expect that the user really means it, without any second-guessing; it is the user's responsibility to be careful with potentially destructive commands.






        share|improve this answer






















          up vote
          3
          down vote



          accepted







          up vote
          3
          down vote



          accepted






          The default overwrite behavior of cp is specified in POSIX.





          1. If source_file is of type regular file, the following steps shall be taken:



            3.a. The behavior is unspecified if dest_file exists and was written by a previous step. Otherwise, if dest_file exists, the following steps shall be taken:



            3.a.i. If the -i option is in effect, the cp utility shall write a prompt to the standard error and read a line from the standard input. If the response is not affirmative, cp shall do nothing more with source_file and go on to any remaining files.



            3.a.ii. A file descriptor for dest_file shall be obtained by performing actions equivalent to the open() function defined in the System Interfaces volume of POSIX.1-2017 called using dest_file as the path argument, and the bitwise-inclusive OR of O_WRONLY and O_TRUNC as the oflag argument.



            3.a.iii. If the attempt to obtain a file descriptor fails and the -f option is in effect, cp shall attempt to remove the file by performing actions equivalent to the unlink() function defined in the System Interfaces volume of POSIX.1-2017 called using dest_file as the path argument. If this attempt succeeds, cp shall continue with step 3b.





          When the POSIX specification was written, there already was a large number of scripts in existence, with a built-in assumption for the default overwrite behavior. Many of those scripts were designed to run without direct user presence, e.g. as cron jobs or other background tasks. Changing the behavior would have broken them. Reviewing and modifying them all to add an option to force overwriting wherever needed was probably considered a huge task with minimal benefits.



          Also, the Unix command line was always designed to allow an experienced user to work efficiently, even at the expense of a hard learning curve for a beginner. When the user enters a command, the computer is to expect that the user really means it, without any second-guessing; it is the user's responsibility to be careful with potentially destructive commands.






          share|improve this answer












          The default overwrite behavior of cp is specified in POSIX.





          1. If source_file is of type regular file, the following steps shall be taken:



            3.a. The behavior is unspecified if dest_file exists and was written by a previous step. Otherwise, if dest_file exists, the following steps shall be taken:



            3.a.i. If the -i option is in effect, the cp utility shall write a prompt to the standard error and read a line from the standard input. If the response is not affirmative, cp shall do nothing more with source_file and go on to any remaining files.



            3.a.ii. A file descriptor for dest_file shall be obtained by performing actions equivalent to the open() function defined in the System Interfaces volume of POSIX.1-2017 called using dest_file as the path argument, and the bitwise-inclusive OR of O_WRONLY and O_TRUNC as the oflag argument.



            3.a.iii. If the attempt to obtain a file descriptor fails and the -f option is in effect, cp shall attempt to remove the file by performing actions equivalent to the unlink() function defined in the System Interfaces volume of POSIX.1-2017 called using dest_file as the path argument. If this attempt succeeds, cp shall continue with step 3b.





          When the POSIX specification was written, there already was a large number of scripts in existence, with a built-in assumption for the default overwrite behavior. Many of those scripts were designed to run without direct user presence, e.g. as cron jobs or other background tasks. Changing the behavior would have broken them. Reviewing and modifying them all to add an option to force overwriting wherever needed was probably considered a huge task with minimal benefits.



          Also, the Unix command line was always designed to allow an experienced user to work efficiently, even at the expense of a hard learning curve for a beginner. When the user enters a command, the computer is to expect that the user really means it, without any second-guessing; it is the user's responsibility to be careful with potentially destructive commands.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 1 hour ago









          telcoM

          13.1k11440




          13.1k11440






















              up vote
              2
              down vote













              You may insert the "if check" yourself with



              alias cp='cp -i'


              The -i option to cp makes it ask interactively if the operation would overwrite the target and with the above alias, your interactive use of cp would enable this option by default.



              The cp utility is often used in scripts. These scripts may not always be run manually, but may be part of system maintenance tasks run by via cron or some similar facility. In those cases, there are no humans to ask for confirmation.



              To have a script avoid overwriting a file with cp one may insert an explicit check:



              if [ -e "$target" ]; then
              printf '%s already exists, refusing to overwriten' "$target" >&2
              exit 1
              fi
              cp "$source" "$target"


              I'm opting for exiting the script with an error message if $target exists in the above example. The existence of the target is obviously unexpected, and allowing the script to continue after detecting this may lead to other more severe issues.






              share|improve this answer


























                up vote
                2
                down vote













                You may insert the "if check" yourself with



                alias cp='cp -i'


                The -i option to cp makes it ask interactively if the operation would overwrite the target and with the above alias, your interactive use of cp would enable this option by default.



                The cp utility is often used in scripts. These scripts may not always be run manually, but may be part of system maintenance tasks run by via cron or some similar facility. In those cases, there are no humans to ask for confirmation.



                To have a script avoid overwriting a file with cp one may insert an explicit check:



                if [ -e "$target" ]; then
                printf '%s already exists, refusing to overwriten' "$target" >&2
                exit 1
                fi
                cp "$source" "$target"


                I'm opting for exiting the script with an error message if $target exists in the above example. The existence of the target is obviously unexpected, and allowing the script to continue after detecting this may lead to other more severe issues.






                share|improve this answer
























                  up vote
                  2
                  down vote










                  up vote
                  2
                  down vote









                  You may insert the "if check" yourself with



                  alias cp='cp -i'


                  The -i option to cp makes it ask interactively if the operation would overwrite the target and with the above alias, your interactive use of cp would enable this option by default.



                  The cp utility is often used in scripts. These scripts may not always be run manually, but may be part of system maintenance tasks run by via cron or some similar facility. In those cases, there are no humans to ask for confirmation.



                  To have a script avoid overwriting a file with cp one may insert an explicit check:



                  if [ -e "$target" ]; then
                  printf '%s already exists, refusing to overwriten' "$target" >&2
                  exit 1
                  fi
                  cp "$source" "$target"


                  I'm opting for exiting the script with an error message if $target exists in the above example. The existence of the target is obviously unexpected, and allowing the script to continue after detecting this may lead to other more severe issues.






                  share|improve this answer














                  You may insert the "if check" yourself with



                  alias cp='cp -i'


                  The -i option to cp makes it ask interactively if the operation would overwrite the target and with the above alias, your interactive use of cp would enable this option by default.



                  The cp utility is often used in scripts. These scripts may not always be run manually, but may be part of system maintenance tasks run by via cron or some similar facility. In those cases, there are no humans to ask for confirmation.



                  To have a script avoid overwriting a file with cp one may insert an explicit check:



                  if [ -e "$target" ]; then
                  printf '%s already exists, refusing to overwriten' "$target" >&2
                  exit 1
                  fi
                  cp "$source" "$target"


                  I'm opting for exiting the script with an error message if $target exists in the above example. The existence of the target is obviously unexpected, and allowing the script to continue after detecting this may lead to other more severe issues.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited 4 mins ago

























                  answered 13 mins ago









                  Kusalananda

                  111k15216342




                  111k15216342




















                      rider dragon is a new contributor. Be nice, and check out our Code of Conduct.









                       

                      draft saved


                      draft discarded


















                      rider dragon is a new contributor. Be nice, and check out our Code of Conduct.












                      rider dragon is a new contributor. Be nice, and check out our Code of Conduct.











                      rider dragon is a new contributor. Be nice, and check out our Code of Conduct.













                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f477401%2fwhy-cp-is-designed-this-way-to-overwrite-existing-files-silently%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Comments

                      Popular posts from this blog

                      Long meetings (6-7 hours a day): Being “babysat” by supervisor

                      Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

                      Confectionery