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
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With