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
Related videos on Youtube
Author by
Stack_Exchange_User_adsf
Updated on September 18, 2022Comments
-
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 almost 9 yearsI used rsync in my answer because your question has a rsync tag.
-
Stack_Exchange_User_adsf almost 9 yearsI 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 almost 9 yearsYou 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.