How to see full absolute path of a symlink

LinuxSymlink

Linux Problem Overview


When I'm using ls -la symlinkName or stat symlinkName not all the path is displayed (e.g ../../../one/two/file.txt)

What is the linux command that reveals the full path?

Linux Solutions


Solution 1 - Linux

realpath isn't available on all linux flavors, but readlink should be.

readlink -f symlinkName

The above should do the trick.

Alternatively, if you don't have either of the above installed, you can do the following if you have python 2.6 (or later) installed

python -c 'import os.path; print(os.path.realpath("symlinkName"))'

Solution 2 - Linux

realpath <path to the symlink file> should do the trick.

Solution 3 - Linux

unix flavors -> ll symLinkName

OSX -> readlink symLinkName

Difference is 1st way would display the sym link path in a blinking way and 2nd way would just echo it out on the console.

Solution 4 - Linux

You can use awk with a system call readlink to get the equivalent of an ls output with full symlink paths. For example:

ls | awk '{printf("%s ->", $1); system("readlink -f " $1)}'

Will display e.g.

thin_repair ->/home/user/workspace/boot/usr/bin/pdata_tools
thin_restore ->/home/user/workspace/boot/usr/bin/pdata_tools
thin_rmap ->/home/user/workspace/boot/usr/bin/pdata_tools
thin_trim ->/home/user/workspace/boot/usr/bin/pdata_tools
touch ->/home/user/workspace/boot/usr/bin/busybox
true ->/home/user/workspace/boot/usr/bin/busybox

Solution 5 - Linux

Another way to see information is stat command that will show more information. Command stat ~/.ssh on my machine display

File: ‘/home/sumon/.ssh’ -> ‘/home/sumon/ssh-keys/.ssh.personal’
  Size: 34        	Blocks: 0          IO Block: 4096   symbolic link
Device: 801h/2049d	Inode: 25297409    Links: 1
Access: (0777/lrwxrwxrwx)  Uid: ( 1000/   sumon)   Gid: ( 1000/   sumon)
Access: 2017-09-26 16:41:18.985423932 +0600
Modify: 2017-09-25 15:48:07.880104043 +0600
Change: 2017-09-25 15:48:07.880104043 +0600
 Birth: -

Hope this may help someone.

Solution 6 - Linux

In macOS Catalina try -

readlink -n `which <command>`

Example Input -

readlink -n `which adb`

Example Output -

/usr/local/Caskroom/android-platform-tools/31.0.1,d027ce0f9f214a4bd575a73786b44d8ccf7e7516/platform-tools/adb

Solution 7 - Linux

On my mac I have the following:

For example create symlink to dir: ln -s /Users/test/play/game game

Then if I use the symlink to goto dir: cd game If I issuethe cmd: pwd, the actual directory you're in is: ~

If I decide I want to actually change to the dir: /Users/test/play/game for some reason, I have an alias in my .bash_profile set up:

alias cdp='cd -P $(basename `pwd`)'

after using the above cdp alias, followed by pwd command the current dir is: /Users/test/play/game

Solution 8 - Linux

I will give short review and analysis here, but for those TL;DR here is one-liner for bash, useful to anchor the working directory:

script_home=$( dirname $(realpath "$0") )

Or you can use any other filename instead of $0 to determine it's real location.

There is not only problem of detemination of real path of some file, but especially some script is called via symlink from another location and needs to reference other resources relative to it's real work directory.

Details follow. Lets assume we have real script or file and symbolic link to it:

$ ls -la
-rwxr-xr-x 1 root    root  0 Mar 20 07:05 realscript.sh
lrwxrwxrwx 1 root    root 10 Mar 20 07:05 symlink -> realscript.sh

And the part of GNU coreutils are few very useful commands:

$ realpath symlink
/home/test/realscript.sh

see also on original:

realpath realscript.sh
/home/test/realscript.sh

Also very good combination in scripting is to use dirname on script

$ dirname /home/test/realscript.sh
/home/test

so to wrap it up, you can use in script

echo  $( dirname $(realpath "symlink") )

or to get and store in variable real script home dir and save code to get real path script realscript.sh:

script_home=$( dirname $(realpath "$0") )
echo Original script home: $script_home

Where "$0" is defined as "self" in shell script.

To test everything, we put symlink into /home/test2/, amend some additional things and run/call it from root directory:

$ /home/test2/symlink
/home/test
Original script home: /home/test
Original script is: /home/test/realscript.sh
Called script is: /home/test2/symlink

Please try to write your self the amended outputs :)

Update 2021, there is also command:

readlink - print resolved symbolic links or canonical file names

DESCRIPTION Note realpath(1) is the preferred command to use for canonicalization functionality.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Question15412sView Question on Stackoverflow
Solution 1 - LinuxIan Stapleton CordascoView Answer on Stackoverflow
Solution 2 - LinuxJosephHView Answer on Stackoverflow
Solution 3 - LinuxRupamView Answer on Stackoverflow
Solution 4 - Linuxuser3467349View Answer on Stackoverflow
Solution 5 - LinuxEngr. Hasanuzzaman SumonView Answer on Stackoverflow
Solution 6 - LinuxManyam Nandeesh ReddyView Answer on Stackoverflow
Solution 7 - LinuxTedEdView Answer on Stackoverflow
Solution 8 - LinuxArunas BartisiusView Answer on Stackoverflow