One of the very powerful features of Linux file systems is symbolic links, more commonly known as soft links or simply symlinks. Symlinks make it much easier to get to a commonly used file or directory. Let’s assume we have a deep directory tree and want to make it easy to access myfile
within it:
$ mkdir -p deep/directory/tree $ ln -s deep/directory/tree shortcut $ touch shortcut/myfile
Now we can refer to that file as either shortcut/myfile
or deep/directory/tree/myfile
from the current directory.
Symlinks can point to other symlinks. For example, on the Debian system I’m typing this on, /usr/bin/vim
is a symlink:
$ file /usr/bin/vim /usr/bin/vim: symbolic link to /etc/alternatives/vim
But the place it points to is also a symlink:
$ file /etc/alternatives/vim /etc/alternatives/vim: symbolic link to /usr/bin/vim.gtk
That’s not an uncommon configuration to handle the update-alternatives(1)
mechanism.
Finding the True Path
There’s a really simple what of finding out where the real file is: realpath
. Here it is in action:
$ realpath shortcut/myfile /tmp/deep/directory/tree/myfile $ realpath /usr/bin/vim /usr/bin/vim.gtk
When we traverse symlinked directories to get to our working directory, it can even be confusing as to where we are, because pwd
will show the path with symlinks:
$ cd shortcut /tmp/shortcut $ pwd /tmp/shortcut
However, realpath
can show where we really are:
$ realpath . /tmp/deep/directory/tree
Was This Linux Tip Useful?
Let us know in the comments below.
Very useful indeed. I always thought PWD was the real path.