How to copy symlinks as symlinks from one machine to another

file-copyremotesftpsymlink

Let's assume I have two identical systems. On the first system I've created a symbolic link. On the second one I want to copy that symlink via sftp and the symlink shall work the same (i.e if the symlink links to /etc/, after copying it over, only the symlink will be copied and only the symlink (not the linked files) )

Any ideas on how I can do this / if I can do this? I only want to copy the symbolic link, nothing else; just the reference.

Best Answer

If you tar up the symlink, then you can copy the tarball over to the remote machine any way you like. When you untar it, you'll find that you've only copied the symbolic link and nothing else. For example:

To test this, first I created a text file.

echo this is a test > file.txt

Then create a symlink to the file and compress it into a tar gzip.

ln -s file.txt link.lnk
tar czvf tarball.tar.gz link.lnk

Transfer this file to your other machine and then uncompress it:

tar xzvf tarball.tar.gz

You will find that the symlink is there, but not the original file that it had pointed to.

Of course, if you wanted to transfer both the original file and the symlink while preserving their relationship, you can put them both in the tarball and they would both be transferred, and the symlink should still be correctly pointed to the original file when you uncompress the tarball in the destination location.

Related Question