Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
2  
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

3 Answers 3

up vote 23 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
5  
-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

Your Answer

 
discard

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.