Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find out what my symbolic link is pointing to?

Tags:

unix

Making a bash script, and I am trying to figure out a way to find out what my symbolic link points to. Right now, I am doing it with this, but this only works if my symlink is in the current directory. Is there a way to find out what my symlink is pointing to if it is in another directory?

 ls  -l "symlink" | cut -d'>' -f2
like image 697
Strawberry Avatar asked Nov 15 '10 06:11

Strawberry


2 Answers

If you have the readlink(1) utility (part of GNU coreutils), it does what you want. Otherwise you are kinda up a creek, I'm not aware of any straightforward & portable equivalent.

like image 142
zwol Avatar answered Nov 02 '22 20:11

zwol


On a BSD toolchain, I am doing:

stat -f %Y <filename>

For example:

% ln -sf /bsd ~/blah        
% stat -f %Y ~/blah
/bsd

On a GNU toolchain it is not so easy, you can use something like:

$ stat -c %N /usr/bin/firefox
`/usr/bin/firefox' -> `../lib/firefox-3.6.12/firefox.sh'

Then, use awk/cut and sed to extract and remove junk quotes.

Or a messier solution is to use ls -al and either awk/cut to extract the column you need.

like image 42
Edd Barrett Avatar answered Nov 02 '22 19:11

Edd Barrett