シンボリックリンクを作成して、rsyncを使用してフォルダを別のフォルダに「複製」できますか?

シンボリックリンクを作成して、rsyncを使用してフォルダを別のフォルダに「複製」できますか?

rsyncを使用してフォルダを新しいフォルダに「複製」しますが、ソースへのシンボリックリンクで新しいフォルダツリー構造を作成することは可能ですか?

cp -as SOURCE DEST

-s, --symbolic-link コピーの代わりにシンボリックリンクを生成する

上記のコマンドで問題は解決しましたが、cpコマンドを再実行してもDESTに手動で追加したファイルは削除されません。だからrsyncを使用しようとしました。

これを達成する方法について提案がありますか?

ベストアンサー1

rsyncシンボリックリンクは作成されませんが、ハードリンクは作成できます。

$ ls -lR test-source
total 4
-rw-r--r--  1 kk  wheel    0 Oct 22 18:54 a
-rw-r--r--  1 kk  wheel    0 Oct 22 18:54 b
-rw-r--r--  1 kk  wheel    0 Oct 22 18:54 c
-rw-r--r--  1 kk  wheel    0 Oct 22 18:54 d
drwxr-xr-x  2 kk  wheel  512 Oct 22 18:54 dir

test-source/dir:
total 0
-rw-r--r--  1 kk  wheel  0 Oct 22 18:54 e
-rw-r--r--  1 kk  wheel  0 Oct 22 18:54 f
-rw-r--r--  1 kk  wheel  0 Oct 22 18:54 g
-rw-r--r--  1 kk  wheel  0 Oct 22 18:54 h

--link-destフラグの使用:

$ rsync -av --link-dest="$PWD/test-source" test-source/ test-destination/
sending incremental file list
created directory test-destination

sent 191 bytes  received 52 bytes  486.00 bytes/sec
total size is 0  speedup is 0.00

これで、ターゲットファイルがソースディレクトリにハードリンクされました(2出力の2番目の列を参照ls -l)。

$ ls -lR test-destination
total 4
-rw-r--r--  2 kk  wheel    0 Oct 22 18:54 a
-rw-r--r--  2 kk  wheel    0 Oct 22 18:54 b
-rw-r--r--  2 kk  wheel    0 Oct 22 18:54 c
-rw-r--r--  2 kk  wheel    0 Oct 22 18:54 d
drwxr-xr-x  2 kk  wheel  512 Oct 22 18:54 dir

test-destination/dir:
total 0
-rw-r--r--  2 kk  wheel  0 Oct 22 18:54 e
-rw-r--r--  2 kk  wheel  0 Oct 22 18:54 f
-rw-r--r--  2 kk  wheel  0 Oct 22 18:54 g
-rw-r--r--  2 kk  wheel  0 Oct 22 18:54 h

ソースディレクトリのファイルリンク数も増加します(明らかに)。

$ ls -lR test-source
total 4
-rw-r--r--  2 kk  wheel    0 Oct 22 18:54 a
-rw-r--r--  2 kk  wheel    0 Oct 22 18:54 b
-rw-r--r--  2 kk  wheel    0 Oct 22 18:54 c
-rw-r--r--  2 kk  wheel    0 Oct 22 18:54 d
drwxr-xr-x  2 kk  wheel  512 Oct 22 18:54 dir

test-source/dir:
total 0
-rw-r--r--  2 kk  wheel  0 Oct 22 18:54 e
-rw-r--r--  2 kk  wheel  0 Oct 22 18:54 f
-rw-r--r--  2 kk  wheel  0 Oct 22 18:54 g
-rw-r--r--  2 kk  wheel  0 Oct 22 18:54 h

ソースディレクトリに存在しないファイルをターゲットディレクトリから削除するには、次のフラグを使用します--delete

$ touch test-destination/delete_me

$ rsync -av --delete --link-dest="$PWD/test-source" test-source/ test-destination/
sending incremental file list
deleting delete_me
./

sent 194 bytes  received 29 bytes  446.00 bytes/sec
total size is 0  speedup is 0.00

おすすめ記事