target of a symlink

It took me ages to find this, so I will record it. To find the target of a symbolic link, you can use the ‘readlink’ command.

Or in my case, you can find the real file or directory of the current script like this:

#!/bin/bash
MY_FILE=`readlink -f $0`
MY_DIR=`dirname $MY_FILE`
...

The ‘-f’ option means it will traverse multiple symbolic links if they point to each other, rather than just the first.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*