2

Is it possible to use rsync to "clone" a folder to a new folder but create the new folder tree structure as a symbolic link to the SOURCE.

cp -as SOURCE DEST

-s, --symbolic-link make symbolic links instead of copying

The above command do the trick but it will not remove files that added manually to DEST if i run the cp command again. that why i thought of using using rsync.

any suggestion on how to achieve that?

1
  • Some shell script that lists files on origin and destination, removes duplicates from the list and then erase the extra files from disk?
    – Zip
    Commented Oct 22, 2017 at 16:48

1 Answer 1

3

rsync would not create symbolic links but may create hard links for you:

$ 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

Use the --link-dest flag:

$ 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

The destination files are now hard-linked to the source directory (see the 2 in the second column of the ls -l output):

$ 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

The link count has also increased on the files in the source directory (obviously):

$ 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

To remove files in the destination directory that does not exist in the source directory, use the --delete flag:

$ 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
2
  • i ‫appreciate your well written answer . do you think using hard link will be better?
    – Asaf Magen
    Commented Oct 22, 2017 at 20:59
  • @AsafMagen "Better"? The only case where it will not work is when the source and destination directories are on two different filesystems (partitions). I can say nothing about whether it would be "better".
    – Kusalananda
    Commented Oct 22, 2017 at 21:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .