How to get full path of original file of a soft symbolic link?
Try this line:
readlink -f `which command`
If command
is in your $PATH
variable , otherwise you need to specify the path you know.
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.
I use command ls
, stat
, readlink
.
Take file /etc/localtime
as an example
[flying@lempstacker ~]$ ls /etc/localtime
/etc/localtime
[flying@lempstacker ~]$ ls -l /etc/localtime
lrwxrwxrwx. 1 root root 35 Aug 2 22:41 /etc/localtime -> ../usr/share/zoneinfo/Asia/Shanghai
Operation
Using stat
[flying@lempstacker ~]$ stat /etc/localtime
File: ‘/etc/localtime’ -> ‘../usr/share/zoneinfo/Asia/Shanghai’
Size: 35 Blocks: 0 IO Block: 4096 symbolic link
Device: fd01h/64769d Inode: 272202388 Links: 1
Access: (0777/lrwxrwxrwx) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2016-11-23 09:00:59.999887800 +0800
Modify: 2016-08-02 22:41:26.090389904 +0800
Change: 2016-08-02 22:41:26.090389904 +0800
Birth: -
[flying@lempstacker ~]$ stat -c "%N" /etc/localtime
‘/etc/localtime’ -> ‘../usr/share/zoneinfo/Asia/Shanghai’
Using readlink
[flying@lempstacker ~]$ readlink /etc/localtime
../usr/share/zoneinfo/Asia/Shanghai
[flying@lempstacker ~]$ readlink -f /etc/localtime
/usr/share/zoneinfo/Asia/Shanghai
It seems like that command readlink -f
is better.
Explanation
-f, --canonicalize: canonicalize by following every symlink in every component of the given name recursively; all but the last component must exist —— From
man readlink