force cp to overwrite existing directory

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bilibytes
    New Member
    • Jun 2008
    • 128

    #1

    force cp to overwrite existing directory

    hi there,

    i am fighting against my unix box to be able to copy a directory and replace the destination if already existing.
    i'm using "Bash"

    i use:

    Code:
    cp -fR /source/src_directory /destination/dest_directory
    what this will do is that it will create a directory like this:
    (assuming destination/dest_directory already exists)
    Code:
    /destination/dest_directory/dest_directory
    and i want:

    Code:
    /destination/dest_directory
    if you know how to obtain the last result please let me know, because i wont be able to sleep until then! lol

    thank you

    best regards

    bilibytes
  • bilibytes
    New Member
    • Jun 2008
    • 128

    #2
    I came with sort of a patch solution:

    in my .profile under the home directory, i created a function: cpForce which looks like this:

    cpForce(){
    sudo rm -fR $1
    sudo cp -fR $1
    }


    regards.

    Comment

    • mac11
      Contributor
      • Apr 2007
      • 256

      #3
      I think it would be sufficient to do

      Code:
      cp -fR source/src_directory destination/
      That is, don't specify the destination directory, just the path up to it (if that makes sense). So if you have

      /source/files/filetocopy
      /source/files/filetocopy2
      /dest/files

      you just do

      Code:
      cp -fR /source/files /dest
      and you end up with

      /source/files/filetocopy
      /source/files/filetocopy2
      /dest/files/filetocopy
      /dest/files/filetocopy2

      Maybe I'm not understanding your issue though.

      Comment

      • bilibytes
        New Member
        • Jun 2008
        • 128

        #4
        ok thanks, i now understood the problem.

        The first time you copy a folder to another directory, if the folder does not exist in the directory, you have to specify the name of the folder as:

        cp -fR /source_director y/folder /destination_dir ectory/folder

        and it will copy the /source_director y/folder as /destination_dir ectory/folder.

        However the second time, /destination_dir ectory/folder already exists and running the same command will do this:

        /destination_dir ectory/folder/folder

        so it copies the folder inside the first copied folder.

        and that is the weird thing.

        but the destination I see that folder must be the final folder -1
        however if you want the destination folder to have a different name than the source my alias in the post above works fine.

        regards

        Comment

        Working...