cp file to a directory with a file with same name

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












1















Sorry if I missed something in the cp manual, but is there a way to copy a file to a directory where there may be a file with the same name? Something like adding a postfix to the name of the copied file if there is a file with the same name on the destination directory. Something like:



ls foo
file
cp file foo/
ls foo
file
file*


The OS, that I am running, is Ubuntu Gnu/Linux.










share|improve this question
























  • you want the postfix added if/when there's a file with the same name?

    – Jeff Schaller
    Feb 28 at 16:10











  • yes, like a star or anything. Edited

    – Bleuderk
    Feb 28 at 16:12











  • For there to be a good answer, I think you should settle on what you want :)

    – Jeff Schaller
    Feb 28 at 16:15











  • Also let us know whether you're on a Linux system or some other Unix. GNU cp has built-in support for backing existing files up (yes, that's not exactly what you want to do, but it's there).

    – Kusalananda
    Feb 28 at 16:21












  • Edited. Thanks for feedbacks as a new user of this Forum. Will read advices for newcommers :)

    – Bleuderk
    Feb 28 at 16:26















1















Sorry if I missed something in the cp manual, but is there a way to copy a file to a directory where there may be a file with the same name? Something like adding a postfix to the name of the copied file if there is a file with the same name on the destination directory. Something like:



ls foo
file
cp file foo/
ls foo
file
file*


The OS, that I am running, is Ubuntu Gnu/Linux.










share|improve this question
























  • you want the postfix added if/when there's a file with the same name?

    – Jeff Schaller
    Feb 28 at 16:10











  • yes, like a star or anything. Edited

    – Bleuderk
    Feb 28 at 16:12











  • For there to be a good answer, I think you should settle on what you want :)

    – Jeff Schaller
    Feb 28 at 16:15











  • Also let us know whether you're on a Linux system or some other Unix. GNU cp has built-in support for backing existing files up (yes, that's not exactly what you want to do, but it's there).

    – Kusalananda
    Feb 28 at 16:21












  • Edited. Thanks for feedbacks as a new user of this Forum. Will read advices for newcommers :)

    – Bleuderk
    Feb 28 at 16:26













1












1








1








Sorry if I missed something in the cp manual, but is there a way to copy a file to a directory where there may be a file with the same name? Something like adding a postfix to the name of the copied file if there is a file with the same name on the destination directory. Something like:



ls foo
file
cp file foo/
ls foo
file
file*


The OS, that I am running, is Ubuntu Gnu/Linux.










share|improve this question
















Sorry if I missed something in the cp manual, but is there a way to copy a file to a directory where there may be a file with the same name? Something like adding a postfix to the name of the copied file if there is a file with the same name on the destination directory. Something like:



ls foo
file
cp file foo/
ls foo
file
file*


The OS, that I am running, is Ubuntu Gnu/Linux.







linux cp






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 28 at 16:31









ctrl-alt-delor

12.1k42561




12.1k42561










asked Feb 28 at 16:06









BleuderkBleuderk

1084




1084












  • you want the postfix added if/when there's a file with the same name?

    – Jeff Schaller
    Feb 28 at 16:10











  • yes, like a star or anything. Edited

    – Bleuderk
    Feb 28 at 16:12











  • For there to be a good answer, I think you should settle on what you want :)

    – Jeff Schaller
    Feb 28 at 16:15











  • Also let us know whether you're on a Linux system or some other Unix. GNU cp has built-in support for backing existing files up (yes, that's not exactly what you want to do, but it's there).

    – Kusalananda
    Feb 28 at 16:21












  • Edited. Thanks for feedbacks as a new user of this Forum. Will read advices for newcommers :)

    – Bleuderk
    Feb 28 at 16:26

















  • you want the postfix added if/when there's a file with the same name?

    – Jeff Schaller
    Feb 28 at 16:10











  • yes, like a star or anything. Edited

    – Bleuderk
    Feb 28 at 16:12











  • For there to be a good answer, I think you should settle on what you want :)

    – Jeff Schaller
    Feb 28 at 16:15











  • Also let us know whether you're on a Linux system or some other Unix. GNU cp has built-in support for backing existing files up (yes, that's not exactly what you want to do, but it's there).

    – Kusalananda
    Feb 28 at 16:21












  • Edited. Thanks for feedbacks as a new user of this Forum. Will read advices for newcommers :)

    – Bleuderk
    Feb 28 at 16:26
















you want the postfix added if/when there's a file with the same name?

– Jeff Schaller
Feb 28 at 16:10





you want the postfix added if/when there's a file with the same name?

– Jeff Schaller
Feb 28 at 16:10













yes, like a star or anything. Edited

– Bleuderk
Feb 28 at 16:12





yes, like a star or anything. Edited

– Bleuderk
Feb 28 at 16:12













For there to be a good answer, I think you should settle on what you want :)

– Jeff Schaller
Feb 28 at 16:15





For there to be a good answer, I think you should settle on what you want :)

– Jeff Schaller
Feb 28 at 16:15













Also let us know whether you're on a Linux system or some other Unix. GNU cp has built-in support for backing existing files up (yes, that's not exactly what you want to do, but it's there).

– Kusalananda
Feb 28 at 16:21






Also let us know whether you're on a Linux system or some other Unix. GNU cp has built-in support for backing existing files up (yes, that's not exactly what you want to do, but it's there).

– Kusalananda
Feb 28 at 16:21














Edited. Thanks for feedbacks as a new user of this Forum. Will read advices for newcommers :)

– Bleuderk
Feb 28 at 16:26





Edited. Thanks for feedbacks as a new user of this Forum. Will read advices for newcommers :)

– Bleuderk
Feb 28 at 16:26










2 Answers
2






active

oldest

votes


















0














Adding a positive integer to the target name if the name is already taken, and also incrementing that integer until a free name is found:



mycp () 
local source="$1"
local target="$2"

local n

# If the target pathname is a directory, add the source filename
# the end of it.
if [ -d "$target" ]; then
target+="/$(basename "$source")"
fi

# Increment n until a free name is found
# (this may leave n unset if the source filename is free).
while [ -e "$target$n" ]; do
n=$(( n + 1 ))
done

cp "$source" "$target$n"



Note: This function does not take any other arguments than a source and target pathname. It also assumes that you are using the bash shell.



To "install" it, just run the above code in your shell, or add it to wherever you usually add aliases and functions.



Testing:



$ ls
dir file
$ ls dir/




$ mycp file dir
$ ls dir/
file




$ mycp file dir
$ ls dir/
file file1




$ mycp file dir
$ ls dir/
file file1 file2





share|improve this answer
































    1














    You can roll your own function. This will keep adding underscores until there is not a duplicate:



    mycp() 
    if [[ -f "$2" ]]; then
    mycp "$1" "$2_"
    else
    cp "$1" "$2"
    fi



    Not compatible with passing arguments (e. g. cp -p). The better option is to use cp -n, which will not overwrite an existing file.






    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%2f503581%2fcp-file-to-a-directory-with-a-file-with-same-name%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      Adding a positive integer to the target name if the name is already taken, and also incrementing that integer until a free name is found:



      mycp () 
      local source="$1"
      local target="$2"

      local n

      # If the target pathname is a directory, add the source filename
      # the end of it.
      if [ -d "$target" ]; then
      target+="/$(basename "$source")"
      fi

      # Increment n until a free name is found
      # (this may leave n unset if the source filename is free).
      while [ -e "$target$n" ]; do
      n=$(( n + 1 ))
      done

      cp "$source" "$target$n"



      Note: This function does not take any other arguments than a source and target pathname. It also assumes that you are using the bash shell.



      To "install" it, just run the above code in your shell, or add it to wherever you usually add aliases and functions.



      Testing:



      $ ls
      dir file
      $ ls dir/




      $ mycp file dir
      $ ls dir/
      file




      $ mycp file dir
      $ ls dir/
      file file1




      $ mycp file dir
      $ ls dir/
      file file1 file2





      share|improve this answer





























        0














        Adding a positive integer to the target name if the name is already taken, and also incrementing that integer until a free name is found:



        mycp () 
        local source="$1"
        local target="$2"

        local n

        # If the target pathname is a directory, add the source filename
        # the end of it.
        if [ -d "$target" ]; then
        target+="/$(basename "$source")"
        fi

        # Increment n until a free name is found
        # (this may leave n unset if the source filename is free).
        while [ -e "$target$n" ]; do
        n=$(( n + 1 ))
        done

        cp "$source" "$target$n"



        Note: This function does not take any other arguments than a source and target pathname. It also assumes that you are using the bash shell.



        To "install" it, just run the above code in your shell, or add it to wherever you usually add aliases and functions.



        Testing:



        $ ls
        dir file
        $ ls dir/




        $ mycp file dir
        $ ls dir/
        file




        $ mycp file dir
        $ ls dir/
        file file1




        $ mycp file dir
        $ ls dir/
        file file1 file2





        share|improve this answer



























          0












          0








          0







          Adding a positive integer to the target name if the name is already taken, and also incrementing that integer until a free name is found:



          mycp () 
          local source="$1"
          local target="$2"

          local n

          # If the target pathname is a directory, add the source filename
          # the end of it.
          if [ -d "$target" ]; then
          target+="/$(basename "$source")"
          fi

          # Increment n until a free name is found
          # (this may leave n unset if the source filename is free).
          while [ -e "$target$n" ]; do
          n=$(( n + 1 ))
          done

          cp "$source" "$target$n"



          Note: This function does not take any other arguments than a source and target pathname. It also assumes that you are using the bash shell.



          To "install" it, just run the above code in your shell, or add it to wherever you usually add aliases and functions.



          Testing:



          $ ls
          dir file
          $ ls dir/




          $ mycp file dir
          $ ls dir/
          file




          $ mycp file dir
          $ ls dir/
          file file1




          $ mycp file dir
          $ ls dir/
          file file1 file2





          share|improve this answer















          Adding a positive integer to the target name if the name is already taken, and also incrementing that integer until a free name is found:



          mycp () 
          local source="$1"
          local target="$2"

          local n

          # If the target pathname is a directory, add the source filename
          # the end of it.
          if [ -d "$target" ]; then
          target+="/$(basename "$source")"
          fi

          # Increment n until a free name is found
          # (this may leave n unset if the source filename is free).
          while [ -e "$target$n" ]; do
          n=$(( n + 1 ))
          done

          cp "$source" "$target$n"



          Note: This function does not take any other arguments than a source and target pathname. It also assumes that you are using the bash shell.



          To "install" it, just run the above code in your shell, or add it to wherever you usually add aliases and functions.



          Testing:



          $ ls
          dir file
          $ ls dir/




          $ mycp file dir
          $ ls dir/
          file




          $ mycp file dir
          $ ls dir/
          file file1




          $ mycp file dir
          $ ls dir/
          file file1 file2






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Feb 28 at 16:51

























          answered Feb 28 at 16:36









          KusalanandaKusalananda

          138k17258426




          138k17258426























              1














              You can roll your own function. This will keep adding underscores until there is not a duplicate:



              mycp() 
              if [[ -f "$2" ]]; then
              mycp "$1" "$2_"
              else
              cp "$1" "$2"
              fi



              Not compatible with passing arguments (e. g. cp -p). The better option is to use cp -n, which will not overwrite an existing file.






              share|improve this answer



























                1














                You can roll your own function. This will keep adding underscores until there is not a duplicate:



                mycp() 
                if [[ -f "$2" ]]; then
                mycp "$1" "$2_"
                else
                cp "$1" "$2"
                fi



                Not compatible with passing arguments (e. g. cp -p). The better option is to use cp -n, which will not overwrite an existing file.






                share|improve this answer

























                  1












                  1








                  1







                  You can roll your own function. This will keep adding underscores until there is not a duplicate:



                  mycp() 
                  if [[ -f "$2" ]]; then
                  mycp "$1" "$2_"
                  else
                  cp "$1" "$2"
                  fi



                  Not compatible with passing arguments (e. g. cp -p). The better option is to use cp -n, which will not overwrite an existing file.






                  share|improve this answer













                  You can roll your own function. This will keep adding underscores until there is not a duplicate:



                  mycp() 
                  if [[ -f "$2" ]]; then
                  mycp "$1" "$2_"
                  else
                  cp "$1" "$2"
                  fi



                  Not compatible with passing arguments (e. g. cp -p). The better option is to use cp -n, which will not overwrite an existing file.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Feb 28 at 16:16









                  DopeGhotiDopeGhoti

                  46.6k56190




                  46.6k56190



























                      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%2f503581%2fcp-file-to-a-directory-with-a-file-with-same-name%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown






                      Popular posts from this blog

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

                      Displaying single band from multi-band raster using QGIS

                      How many registers does an x86_64 CPU actually have?