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?
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?
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"))'
realpath <path to the symlink file>
should do the trick.
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.
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.
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
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.
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
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
To get actual Symlink pointing path in Mac > Terminal
:
ls -lrt <Symlink path/symlink>