Questo inserisce la cartella A nella cartella B:
rsync -avu --delete "/home/user/A" "/home/user/B"
Se desideri i contenuti delle cartelle A e B per essere lo stesso, inserisci /home/user/A/
(con la barra) come fonte. Questo non prende la cartella A ma tutto il suo contenuto e lo inserisce nella cartella B. In questo modo:
rsync -avu --delete "/home/user/A/" "/home/user/B"
-a
Esegui la sincronizzazione preservando tutti gli attributi del filesystem-v
eseguire in modo prolisso-u
copiare solo i file con un'ora di modifica più recente (o una differenza di dimensioni se le ore sono uguali)--delete
elimina i file nella cartella di destinazione che non esistono nell'origine
Manpage:https://download.samba.org/pub/rsync/rsync.html
Potresti unison
strumento sviluppato da Benjamin Pierce presso U Penn.
Supponiamo che tu abbia due directory,
/home/user/Documents/dirA/
e /home/user/Documents/dirB/
Per sincronizzare questi due, puoi usare:
~$unison -ui text /home/user/Documents/dirA/ /home/user/Documents/dirB/
In uscita, unison
visualizzerà ogni singola directory e file che è diverso nelle due directory che hai chiesto di sincronizzare. Consiglierà di sincronizzare in modo additivo (replicare il file mancante in entrambe le posizioni) durante l'esecuzione iniziale, quindi creare e mantenere un albero di sincronizzazione sulla macchina e nelle esecuzioni successive implementerà la vera sincronizzazione (ad esempio, se elimini un file da .../dirA
, verrà eliminato da .../dirB
anche. Puoi anche confrontare ogni singola modifica e, facoltativamente, scegliere di inoltrare o inverso sincronizzare tra le due directory.
Facoltativamente, per avviare l'interfaccia grafica, è sufficiente rimuovere -ui text
opzione dal tuo comando, anche se trovo cli
più semplice e veloce da usare.
Maggiori informazioni su questo:Tutorial Unison nella documentazione per l'utente Unison.
La risposta di TuxForLife è abbastanza buona, ma ti consiglio caldamente di usare -c
durante la sincronizzazione locale. Puoi sostenere che non vale la penalità di tempo/rete per farlo per le sincronizzazioni remote, ma ne vale assolutamente la pena per i file locali perché la velocità è eccezionale.
-c, --checksum This forces the sender to checksum every regular file using a 128-bit MD4 checksum. It does this during the initial file-system scan as it builds the list of all available files. The receiver then checksums its version of each file (if it exists and it has the same size as its sender-side counterpart) in order to decide which files need to be updated: files with either a changed size or a changed checksum are selected for transfer. Since this whole-file checksumming of all files on both sides of the con- nection occurs in addition to the automatic checksum verifications that occur during a file's transfer, this option can be quite slow. Note that rsync always verifies that each transferred file was correctly reconstructed on the receiving side by checking its whole-file checksum, but that automatic after-the-transfer verification has nothing to do with this option's before-the-transfer "Does this file need to be updated?" check.
Questo mostra come avere le stesse dimensioni e timestamp può fallire.
La configurazione
$ cd /tmp
$ mkdir -p {A,b}/1/2/{3,4}
$ echo "\___________from A" | \
tee A/1/2/x | tee A/1/2/3/y | tee A/1/2/4/z | \
tr A b | \
tee b/1/2/x | tee b/1/2/3/y | tee b/1/2/4/z | \
tee b/1/2/x0 | tee b/1/2/3/y0 > b/1/2/4/z0
$ find A b -type f | xargs -I% sh -c "echo %; cat %;"
A/1/2/3/y
\___________from A
A/1/2/4/z
\___________from A
A/1/2/x
\___________from A
b/1/2/3/y
\___________from b
b/1/2/3/y0
\___________from b
b/1/2/4/z
\___________from b
b/1/2/4/z0
\___________from b
b/1/2/x
\___________from b
b/1/2/x0
\___________from b
L'rsync che non copia nulla perché i file hanno tutti la stessa dimensione e timestamp
$ rsync -avu A/ b
building file list ... done
sent 138 bytes received 20 bytes 316.00 bytes/sec
total size is 57 speedup is 0.36
$ find A b -type f | xargs -I% sh -c "echo %; cat %;"
A/1/2/3/y
\___________from A
A/1/2/4/z
\___________from A
A/1/2/x
\___________from A
b/1/2/3/y
\___________from b
b/1/2/3/y0
\___________from b
b/1/2/4/z
\___________from b
b/1/2/4/z0
\___________from b
b/1/2/x
\___________from b
b/1/2/x0
\___________from b
L'rsync che funziona correttamente perché confronta i checksum
$ rsync -cavu A/ b
building file list ... done
1/2/x
1/2/3/y
1/2/4/z
sent 381 bytes received 86 bytes 934.00 bytes/sec
total size is 57 speedup is 0.12
$ find A b -type f | xargs -I% sh -c "echo %; cat %;"
A/1/2/3/y
\___________from A
A/1/2/4/z
\___________from A
A/1/2/x
\___________from A
b/1/2/3/y
\___________from A
b/1/2/3/y0
\___________from b
b/1/2/4/z
\___________from A
b/1/2/4/z0
\___________from b
b/1/2/x
\___________from A
b/1/2/x0
\___________from b