marco-andarcia marco-andarcia - 3 months ago 19
Linux Question

Read Symlink remotely in Linux

I am trying to get the contents of a symlink remotely, but I am having a hard time understanding why is not working.

I tried following the example on this post. but is not working for me. This is what I am trying to do.

$PATH_LINK=~/some/directory/to/link
SYMB_LINK=`ssh files@files_server.com 'readlink $PATH_LINK'`


but I get $readlink: missing operand

I can get it to work if I write the path in the command.

SYMB_LINK=`ssh files@files.files_server.com 'readlink path/to/file'`


I tried running it without the -f but still gives me the same problem.

Answer

First of all, don't use ~ because that will probably be expanded by your shell using the user settings of the user on your local machine rather than the remote machine.

Secondly, you should not use the $ character on the left-hand side when assigning to a shell variable.

Taking all this advice and combining it together, we get:

LINK_PATH=/home/david/link
LINK_TARGET=$(ssh example.com "readlink $LINK_PATH")
Comments