Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

How to resolve symbolic link

📅 2016-Mar-08 ⬩ ✍️ Ashwin Nanjappa ⬩ 📚 Archive

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.

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