Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to scp a file to a server. The file is a symbolic link, and actually what I want to do is copying the source file. Anyway I don't want to track source file's path manually, because it can be replaced. If I can get source file's absolute path, I can sep with it. How can I get the path?

share|improve this question
Most scp versions follow symlinks by default, don't they? You should check yours and maybe save yourself some work. – jw013 Oct 6 '11 at 3:26
@jw103 Thanks for letting me know it. I didn't know. :) – Eonil Oct 6 '11 at 4:42
Note: scp -r forces following symlinks. – Eonil Oct 6 '11 at 4:43
up vote 57 down vote accepted

Try this line:

readlink -f `which command`

If command is in your $PATH variable , otherwise you need to specify the path you know.

share|improve this answer
-f will return a path to a non-existent final target, so long as the intermediate link targets exist... Use -e to avoid this, ie. -e will return null if the final target does not exist. – Peter.O Oct 6 '11 at 4:31
-f errored with readlink: illegal option -- f in OSX. Removing it worked fine. – Heath Borders Nov 13 '13 at 16:44

Under Linux, readlink reads the contents of a symlink, and readlink -f follows symlinks to symlinks to symlinks, etc., until it finds something that isn't a symlink.

This isn't necessary for scp though: scp always follows symlinks (it always copies file content, ignoring metadata except that -p preserves file times and modes when possible).

If you find yourself disappointed by what metadata scp can and can't preserve, I suggest using rsync. With no option, rsync copies file contents ignoring metadata. The commonly used option -a preserves all garden-variety metadata (times, symbolic links, permissions and ownership), and there are options to preserve exotic metadata like ACLs and hard links.

share|improve this answer

Stat will give you this information:

$ stat current
  File: `current' -> `/home/user/releases/build/'
share|improve this answer

You can either do

readlink -f `which command`

or you can get something similar in Linux with

stat `which command` | grep File

If you are adding this to a script, inspect the error. If you don't want to see the error message in the case when the sym link is not there, go with something like

readlink -f "`which command`" 
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.