开发者

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

开发者 https://www.devze.com 2023-01-25 06:41 出处:网络
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

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 t开发者_开发知识库here 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消