I can use rm to delete the old folder, then use cp to copy the new folder. But how to do it in one go?


Using rsync:

rsync -av --delete source/ target

This would delete all the contents of the directory target that does not match the contents of the directory source, and would additionally copy the contents of source there.

The trailing / at the end of source/ is significant as without it, you would get a directory at target/source instead of making target a copy of source.

The -a (or --archive) option makes rsync copy timestamps and other metadata, and the -v (or --verbose) option makes rsync operate verbosely. Without --delete, no existing contents in target would be deleted (unless it had the same name as things in source in which case it would be updated).


That is one go. rm -fr olddir/* ; cp -a newdir/* olddir

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.