I am doing backups of a directory with a simple:

cp -R /production/directory /backups/location

Sometimes I need to restore a backup, but doing:

cp -R /backups/location/* /production/directory


cp -RT /backups/location /production/directory

has the unwanted (in my case) effect, of keeping files present in /production/directory but not in the backup, while I want them removed, to get things in the exact same state as when the backup was done.

Is there any magic switch, or other simple command, to perform that, or do I need to manually remove the whole directory first ?

  • You could use rsync as follow rsync -r --delete-during /backup/location /production/directory which will scan directories while syncing and put the same content of /backup/location/ to /production/directory Jan 14, 2015 at 14:55
  • This will create an additional directory (location with your command, or directory if I add that to source), inside the /production/directory.
    – Gnurfos
    Jan 14, 2015 at 15:18
  • 1
    so include the trailing slash. Use the following: rsync -r --delete-during /backup/location/ /production/directory Jan 14, 2015 at 15:32
  • Thanks, this works perfectly. I was searching for an option and missed the trailing slash trick in the manual. Can you turn it into an answer ?
    – Gnurfos
    Jan 14, 2015 at 16:23

2 Answers 2


You can use rsync to achieve what you want.

rsync -r --delete-during /backup/location/ /production/directory

For more on see man rsync


Well there is a command called rm -R that removes a whole directory. So you start your restore with rm -R /production/directory, followed by the restore command.

  • Thanks for the suggestion. I mentioned manual remove in my question though, as a last resort. I was searching for something more elegant (I think rsync is probably faster too).
    – Gnurfos
    Jan 14, 2015 at 18:16

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.