Can cp completely overwrite a directory?

10,704

Command rsync in combination with --delete option, more in man rsync:

$ rsync -av --delete /from-path /to-path
Share:
10,704

Related videos on Youtube

Stack_Exchange_User_adsf
Author by

Stack_Exchange_User_adsf

Updated on September 18, 2022

Comments

  • Stack_Exchange_User_adsf
    Stack_Exchange_User_adsf over 1 year

    I'm wondering if there is a way to completely overwrite a directory that is being copied to without first needing to erase it with the rm command, meaning I don’t want to have files remaining in the destination directory that don’t exist in the source directory. Is this possible with the cp command? I would like results similar to the commands below but only by issuing one command. I have tried rsync but I didn’t have any luck getting it to behave that way with it’s delete options.

    rm -r /dest_dir/;  cp -a /source_dir/.  /dest_dir
    
  • Mike
    Mike almost 9 years
    I used rsync in my answer because your question has a rsync tag.
  • Stack_Exchange_User_adsf
    Stack_Exchange_User_adsf almost 9 years
    I tried that, but the test directory I had in the destination directory remained in place after issuing that command. I’m surprised the cp command doesn’t have such a capability..
  • Mike
    Mike almost 9 years
    You have to make sure rsync has enough privileges to delete files in destination directory, try running it with sudo maybe. The example I gave you works for me on daily basis.