206

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?

Chris Stryczynski
  • 30,145
  • 48
  • 175
  • 286
15412s
  • 3,298
  • 5
  • 28
  • 38
  • 5
    One should be careful here: when ls or stat display no absolute path - the link has no absolute path! This is esp. important when the device is mounted e.g. in a different context (e.g. if you have something on a cd or usb-stick or try to recover some broken hdd). All the mentioned solutions (realpath, readlink,....) show only the absolute path in the mounted context. – flolo Apr 15 '13 at 14:43
  • `readlink symlinkName` without `-f` shows the relative path, if it was defined with relative path. – midnite Jun 27 '22 at 06:37

10 Answers10

331

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"))'
Ian Stapleton Cordasco
  • 26,944
  • 4
  • 67
  • 72
43

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

JosephH
  • 8,465
  • 4
  • 34
  • 62
  • 2
    `man realpath` shows at the bottom that it it part of "GNU coreutils", which I'm pretty sure always comes on Ubuntu, so even though @IanStapletonCordasco says "`realpath` isn't available on all linux flavors", it should at least be available on all Ubuntu and Ubuntu derivative flavors I believe. – Gabriel Staples Jun 16 '20 at 15:55
13

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.

Rupam
  • 163
  • 1
  • 5
  • 1
    The question was asking specifically for the full/absolute path, not relative path (../../file.txt). Both of these answers will give the relative path. – wisbucky Apr 30 '19 at 23:29
7

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.

Arunas Bartisius
  • 1,879
  • 22
  • 23
5

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
user3467349
  • 3,043
  • 4
  • 34
  • 61
  • ls can do it without readlink; just use `ls -l`. This also doesn't work at all, tries to do it on everything in the directory (non-links) and if your ls is configured to use colours awk completely breaks. just use `find . -maxdepth 1 -type l -ls | awk '{print $11 "\t" $12 "\t" $13}'` – Hashbrown May 07 '18 at 02:06
5

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.

Engr. Hasanuzzaman Sumon
  • 2,103
  • 3
  • 29
  • 38
3

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
0

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

TedEd
  • 621
  • 6
  • 9
0

find -maxdepth 1 -type l -printf "%l\n"

Konstantin Glukhov
  • 1,898
  • 3
  • 18
  • 25
0

To get actual Symlink pointing path in Mac > Terminal:

ls -lrt <Symlink path/symlink>
Anusha Kottiyal
  • 3,855
  • 3
  • 28
  • 45