Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to achieve a mirror copy in Linux?

Tags:

linux

shell

copy

I want to make two directories synchronized. I know there is a robocopy /MIR command in Windows that can achieve this. Ex:

robocopy D:\test1 E:\Backup /MIR

When there is a file updated or deleted, the backup directory can keep synchronization.

I have tried in Linux through the rsync command like this:

rsync -a /usr/test /usr/backup

but when I delete a file under /usr/test and then run this command, the deleted file still exists in /backup/test — the two directories can't keep asynchronization. So, how can I achieve the synchronization of two directories in Linux?

like image 889
elsonwx Avatar asked Nov 20 '25 11:11

elsonwx


1 Answers

rsync has a --delete option which does what was asked:

 --delete                delete extraneous files from dest dirs

It has a lot of related options.

Further reading:

  • rsync(1)
  • Commonly Used rsync Arguments
like image 107
Thomas Dickey Avatar answered Nov 23 '25 02:11

Thomas Dickey