Bash: how to get real path of a symlink?

get absolute path of symbolic link
get symlink target
bash resolve symlinks in path
how to open symbolic link file in linux
symlink get full path
how to access symbolic link in linux
get symlink actual path
linux follow symlink

Is it possible, executing a file symlinked in /usr/local/bin folder, to get the absolute path of original script? Well, .. I know where original file is, and I know it because I am linkging it. But, ... I want this script working, even if I move original source code (and symlink).

echo "my path is ..."

readlink is not a standard command, but it's common on Linux and BSD, including OS X, and it's the most straightforward answer to your question. BSD and GNU readlink implementations are different, so read the documentation for the one you have.

If readlink is not available, or you need to write a cross-platform script that isn't bound to a specific implementation:

If the symlink is also a directory, then

cd -P "$symlinkdir"

will get you into the dereferenced directory, so

echo "I am in $(cd -P "$symlinkdir" && pwd)"

will echo the fully dereferenced directory. That said, cd -P dereferences the entire path, so if you have more than one symlink in the same path you can have unexpected results.

If the symlink is to a file, not a directory, you may not need to dereference the link. Most commands follow symlinks harmlessly. If you simply want to check if a file is a link, use test -L.

How to get full path of original file of a soft symbolic link?, Under Linux, readlink reads the contents of a symlink, and readlink -f follows symlinks to symlinks to symlinks, etc., until it I would use realpath <symlink> . To get physical path use realpath command. The realpath command uses the realpath () function to resolve all symbolic links, extra / characters and references to /./ and /../ in path. This is useful for shell scripting and security related applications.

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
lrwxrwxrwx 1 root    root 10 Mar 20 07:05 symlink ->

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

$ realpath symlink

see also on original:


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

$ dirname /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

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
Original script home: /home/test
Original script is: /home/test/
Called script is: /home/test2/symlink

Please try to write your self the amended outputs :)

How to get the fully resolved path of a symbolic link in Terminal , pwd -P only works if you're inside the symlink directory: 14:07:13 an absolute path: $ python >>> import os >>> os.path.realpath("/usr/local/bin/python3"). Hello guys after my research I found this solution for how to get real path of a Symlink. If you have a created symlink and want to check where is the real pointer of this file or folder. If someone have better way to write it please share.

if you are working on Linux readlink -f $LINK and on mac you can also use that greadlink -f $LINK

Get real path (absolute path) from symbolic link (aka. softlink), On Linux/Unix/Mac systems one can create symbolic links (aka. softlinks) to a file or a directory using the ln -s TARGET or ln -s TARGET LINK. Again, if foo is a symlink, realpath -s foo will get an absolute path without resolving symlinks, including the one given as argument. Without the -s option, realpath does pretty much the same as readlink , except for simply reading the value of a link, as well as several other things.

To get the symlink path even if it is broken:

$ find filename -printf %l\\n

Tested on Ubuntu 18.04

Remove the ending \\n if you only need the net string:

Tested on Ubuntu 18.04

Linux readlink and realpath Command Tutorial for Beginners (with , As already mentioned in the beginning, both realpath and readlink commands display the resolved path for symlinks in the output. Following is� According to the standards, pwd -P should return the path with symlinks resolved. C function char *getcwd (char *buf, size_t size) from unistd.h should have the same behaviour.

How to configure bash to always resolve full path for symlinks , If set, do not resolve symbolic links when performing commands such as cd which So this should get you the behaviour you want: Third, Bash isn't " pretending" anything; there is no illusion, you really are in that directory. from my home directory /home/jvf/, entering the myproject symlink gives me a pwd /home/jfv/myproject/. Now, I would like to enter the parent directory of the directory I've symlinked to, but the cd.. command will only bring me back to my home directory /home/jfv/.

realpath(1) - Linux manual page - Michael Kerrisk, REALPATH(1) User Commands REALPATH(1) components before symlinks - P, --physical resolve symlinks as encountered (default) -q, --quiet suppress most � abspath returns an absolute path but does not resolve symlinks: python -c "import os,sys; print(os.path.abspath(sys.argv[1]))" path/to/file. realpath returns an absolute path and in doing so resolves symlinks, generating a canonical path: python -c "import os,sys; print(os.path.realpath(sys.argv[1]))" path/to/file

Find out symbolic link target via command line, Because if you want to follow a chain of symbolic links to the actual file, then you bash_realpath() { # print the resolved path # @params # 1: the path to resolve � You can use grep with ls command to list all the symbolic links present in the current directory. This will list all the links present in the current directory. ls -la /var/www/ | grep "\->"

  • readlink is a function I can call in a bash script? I am trying but is not working =(
  • readlink is a binary which is part of coreutils
  • readlink -f filename gives you a full path to link target.
  • @PavelPatrin Only for GNU readlink, not for BSD.
  • coreutils isn't installed by default on every system that includes bash. stat is more consistently available, but it too has different usage depending on your operating system.
  • My "#!/bin/bash" script, doing the command: echo "I am in $(cd -P "$symlinkdir" && pwd)", print "/usr/local/bin/ttmux: line 4: cd: : No such file or directory"
  • pwd has also the -P option. If you already are in the directory you can do : pwd -P. Also if you are not in : (cd /path/w/symlinks/ && pwd -P)
  • after pwd -P , is there a way to do cd .. (without using copy and paste) ?
  • Not my downvote, but this code has a rather glaring problem: it fails very badly if you pass it a path that is not a symlink. It also doesn’t work at all on non-Linux systems (such as macOS), since POSIX find does not have a -printf operator.
  • Konrad, thanks you for your comment. I didn't understand what was wrong with my answer, which -printf was not POSIX. Yet this has been useful in my use case of moving symlink targets recursively which must work with broken symlinks such as those pointing to non mounted devices. I'm not sure if I should add a warning or delete the whole answer.