How to resolve symbolic link

Many common programs on Linux are maintained as chain of symbolic links. For example, the old cc command will usually be a symlink to gcc, which itself will be a symlink to a specific version of gcc, for example gcc-4.8. Tools such as package installers use symlinks to install, uninstall and switch between multiple versions of the same program.

Given a file or directory, it would be educational to know its chain of symlinks and the final terminating file or directory.

  • The readlink command can be used if you just want to resolve the symlink and know what is the final file or directory in the chain. Example: readlink -m foo_symlink

  • The namei command is more interesting to me. It can show the full chain of symlinks in a beautiful hierarchical manner. I like to invoke it as: namei -mv foo_symlink

An example output on my system:

$ namei -mv /usr/bin/cc
f: /usr/bin/cc
Drwxr-xr-x /
drwxr-xr-x usr
drwxr-xr-x bin
lrwxrwxrwx cc -> /etc/alternatives/cc
Drwxr-xr-x   /
drwxr-xr-x   etc
drwxr-xr-x   alternatives
lrwxrwxrwx   cc -> /usr/bin/gcc
Drwxr-xr-x     /
drwxr-xr-x     usr
drwxr-xr-x     bin
lrwxrwxrwx     gcc -> /etc/alternatives/gcc
Drwxr-xr-x       /
drwxr-xr-x       etc
drwxr-xr-x       alternatives
lrwxrwxrwx       gcc -> /usr/bin/ccache
Drwxr-xr-x         /
drwxr-xr-x         usr
drwxr-xr-x         bin
-rwxr-xr-x         ccache

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s