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.

  • it doesn't work just as you describe already?
    – mikeserv
    Nov 10, 2015 at 10:34
  • 1
    sftp won't let you do that. rsync can, as can ssh cp ... Nov 10, 2015 at 14:00
  • The thing I am trying to achieve is copying over the symbolic map... I dont want the files.. I just want the bit of code that says FOLDER > ETC ... I just want to create a symlink off the server and move it to the server its needed on... im trying to hack out of an enclosed file
    – TheHidden
    Nov 10, 2015 at 16:17
  • 3
    @roaima ssh cp? as in scp? how? Oct 6, 2018 at 23:25

8 Answers 8


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.

  • this is rather interesting but sadly I dont think it will help me, I dont have any control over the slave all these SFTPs are done via APIs to locked down folders, but I am trying to hack the box using symlinks because there is a bit of an emergency and I need to get access from the master which pushes selected updates, for safety reasons the files i send dont execute, but if I can symlink I can inject code into an already existing process (theoretically) by replacing the file via the symlink ... but to do it I need to copy a symlink over 100% raw :(
    – TheHidden
    Nov 10, 2015 at 13:25
  • @user1779617 It is still kinda unclear what you mean with "slave" and "master", because you seem to use both terms for both server and client interchangeably. If you need to create a symlink on the server, you can do that with SFTP, however depending on the configuration, you might not be able to look outside of the chrooted realm of the SFTP-daemon. Please refer to the man page of sftp (linux.die.net/man/1/sftp)! There is the command symlink specifically for what you seem to need.
    – polemon
    Nov 11, 2015 at 10:16

It is a little unclear how exactly you want to deal with the symlink. As I understand it, you want to recreate the symlink on the other system. Symlinks are filesystem dependent, the protocol used to copy files must be aware of that. A good example is using rsync with the -a option.

more specifically, the -l option, but -a is probably what you want.

It will recreate symlinks without pulling the targets over (dereferencing the links).

  • I see hm, so there would be no way to copy it like a file and retain the link? I only have the ability to directly move files over I was hoping to build the symlink on the identical system and copy it over or am I just being to hopeful?
    – TheHidden
    Nov 10, 2015 at 11:16
  • @user1779617 first of all: when you say "move", do you actually mean "move" or "copy"? Because "move" implies deleting the original file (this was also the reason why I've edited your question). Second thing: are you limited to a specific file transfer protocol (like FTP only)? If so, we should discuss it within the scope of that, and the filesystems we're talking about.
    – polemon
    Nov 10, 2015 at 12:29
  • SFTP only, download only from the machine that needs the symlink, so I mean copy from the master to the slave SFTP only and the sftp connection is from the slave only.
    – TheHidden
    Nov 10, 2015 at 12:32
  • @user1779617 I'm assuming you mean "client" (slave) and "server" (master). sftp will traverse subdirectories with the -r switch, but not follow symlinks, as it abstracts filesystems. sftp supports creating symlinks on the host. You should at least try downloading just the symlink (when the symlink is ~/etc -> /etc/ do get etc without the trailing /) and see what happens. It depends on the FS of the server and the client. Please edit your original question accordingly!
    – polemon
    Nov 10, 2015 at 12:51

I know @polemon has already answered this.

But doing my tad bit for quick solution.

I had to do man rsync to know which switch I wanna use...

rsync -avz -e ssh /file or sym link/to/copy/ user@ip:/dest/path/

Although, in your case you need to make sure that links are not broken!


A very easy solution (for the home directory):

tar -c SYMLINKFILE | ssh machine 'tar -x'


tar -c SYMLINKFILE | ssh machine 'tar -xC /some/other/location  '


tar -c .bashrc | ssh another_machine 'tar -x'

to provide a symlinked .bashrc placing to another (e.g. network) destination.

  • This isn't what the OP was trying to do - he wanted the symlinks to refer to files on the machine he was attacking. It's also not as easy as the sshfs method sinekonata described. Sep 3, 2021 at 21:04

You can mount the remote machine on the local one using sshfs:

sshfs remote.machine:/home/name/remotedir/ /home/name/localdir/

Now you can treat the new mount point as if part of the local machine file system and thus copy symlinks normally.

  • I'd like to vote this answer up about a hundred times: In all my years of working with 'nix systems I'd never come across sshfs before but it solves boatloads of problems I've run into and would have simplified so many tasks that caused me pain and grief... Sep 3, 2021 at 20:59

The other answers already explain how to transfer a symbolic link to another machine. However, regardless of how you transfer the symlink, you will not be able to use this symlink to access a file in /etc/ via FTP, SFTP, SCP, SSH or any other way. These services tend to be chrooted to a given directory or otherwise prevented from providing access to files that were not explicitly shared. Of course, there are always ways to hack around this on older systems, but such vulnerabilities tend to be patched up and upgraded as soon as they're found. I'm sorry, I don't think what you want to do is going to be possible.

  • fair enough, atleast I tried I guess :(
    – TheHidden
    Nov 10, 2015 at 17:17
  • If you use the method sinekonata described, the symlinks will work 100% as expected. Sep 3, 2021 at 21:01

To migrate all my nginx vhost symlinks I used an awk script

ll | awk '{ print "ln -s "$11" "$9 }'

I got a list that I needed to run on the new server.

ln -s ../sites-available/example.de 001-example.de.conf
ln -s ../sites-available/example.com 002-example.com.conf
ln -s ../sites-available/example.net 003-example.net.conf

To complete Skeeve answer, for a limited case, I have small oneliner:

ssh ${SOURCE_SRV} "find ${SOURCE_DIR}/config -maxdepth 1 -type l -ls |awk '{print \"ln -sf \" \$13 \"  \" \$11}'" |xargs -i bash -c "{}"

which will extract the symbolic links from the remote server and recreate them locally. Usually, I copy everything via scp, and then I recreate the symbolic links. I do it one directory at a time, but it would (probably) be easy to do it for a full arbo in one command.

You must log in to answer this question.

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