How to resolve symbolic links in a shell script

Given an outright or loved one course (in a Unix - like system), I would love to establish the complete course of the target after settling any kind of intermediate symlinks. Incentive factors for additionally settling ~ username symbols at the very same time.

If the target is a directory site, it could be feasible to chdir () right into the directory site and afterwards call getcwd (), yet I actually intend to do this from a shell manuscript as opposed to creating a C assistant. However, coverings tend to attempt to hide the presence of symlinks from the customer (this is bash on OS X):

$ ls -ld foo bar
drwxr-xr-x   2 greg  greg  68 Aug 11 22:36 bar
lrwxr-xr-x   1 greg  greg   3 Aug 11 22:36 foo -> bar
$ cd foo
$ pwd

What I desire is a function willpower () such that when implemented from the tmp directory site in the above instance, willpower (" foo") = = "/ Users/greg/tmp/ bar".

2019-05-18 12:12:22
Source Share
Answers: 3
readlink -f "$path"

Editor is note: The over collaborate with GNU readlink and also FreeBSD/PC - BSD/OpenBSD readlink, yet not on OS X since 10.11. ¢ GNU readlink supplies added, relevant alternatives, such as -m for settling a symlink whether the best target exists.

Keep in mind given that GNU coreutils 8.15 (2012 - 01 - 06), there is a realpath program readily available that is much less obtuse and also extra adaptable than the above. It is additionally suitable with the FreeBSD util of the very same name. It additionally consists of capability to create a loved one course in between 2 documents.

realpath $path

[Admin enhancement listed below from comment by halloleo bdsh danorton]

For Mac OS X (via at the very least 10.11. x), usage readlink without the -f alternative:

readlink $path

Editor is note: This will certainly not settle symlinks recursively and also hence will not report the best target ; as an example, offered symlink a that indicates b, which subsequently indicate c, this will just report b (and also will not make certain that it is result as an outright course ). ¢ Use the adhering to perl command on OS X to load the void of the missing out on readlink -f capability: ¢perl -MCwd -le 'print Cwd::abs_path(shift)' "$path"

2022-06-07 23:36:51

Common shell manuscripts usually need to locate their "residence" directory site also if they are conjured up as a symlink. The manuscript hence need to locate their "actual" placement from simply $0.

cat `mvn`

on my system publishes a manuscript having the adhering to, which need to be an excellent mean what you require.

if [ -z "$M2_HOME" ] ; then
  ## resolve links - $0 may be a link to maven's home

  # need this for relative symlinks
  while [ -h "$PRG" ] ; do
    ls=`ls -ld "$PRG"`
    link=`expr "$ls" : '.*-> \(.*\)$'`
    if expr "$link" : '/.*' > /dev/null; then
      PRG="`dirname "$PRG"`/$link"


  M2_HOME=`dirname "$PRG"`/..

  # make it fully qualified
  M2_HOME=`cd "$M2_HOME" && pwd`
2019-05-19 09:54:49

According to the criteria, pwd -P needs to return the course with symlinks settled.

C function char *getcwd(char *buf, size_t size) from unistd.h need to have the very same practices.

getcwd. pwd

2019-05-19 08:54:14