Where symbolic link points to

Hi!

How can I find where a symbolic link points to from a shell script?

I have the follwing problem:

Let's suppose the follwing files:

#ls -l /tmp

-rw-r--r--1 rootother 18 Aug 28 18:18 file

drwxr-xr-x2 rootother178 Aug 28 18:33 mydir

# ls -l /tmp/mydir

lrwxrwxrwx1 rootother 7 Aug 28 18:33 link -> ../file

I would need a command which could return me the file path and name where my link is pointing in order to use it in a shell script.

I found in the ls man page that:

-LIf an argument is a symbolic link, lists the file or

directory the link references rather than the link

itself.

but if I run

# ls -lL /tmp/mydir/link

I got:

-rw-r--r--1 rootother 18 Aug 28 18:18 /tmp/mydir/link

which is very strange because the permissons are the original file's permissions but the name is the link's name.

Thanks.

[935 byte] By [pityq] at [2007-11-26 9:46:11]
# 1
> Hi! > How can I find where a symbolic link points to from a> shell script?I think you'd use 'ls -l' and parse the output.With perl or C, you'd use readlink().-- Darren
Darren_Dunham at 2007-7-7 0:50:51 > top of Java-index,Solaris Operating System,Solaris Essentials - General Technical Questions...
# 2
ls -lL follows the link, so the behaviour you see is correct. If you run ls -lL on a symbolic link, you will get the file information which belongs to the file it points to. 7/M.
mAbrante at 2007-7-7 0:50:51 > top of Java-index,Solaris Operating System,Solaris Essentials - General Technical Questions...
# 3
Thanks for your answer.Yes, I could to parse the result of "ls -l" but my problem is that ther I can find the relative path (in my case ../file) instead of absoluth path ( /tmp/file ) and I need the absolute path.
pityq at 2007-7-7 0:50:51 > top of Java-index,Solaris Operating System,Solaris Essentials - General Technical Questions...
# 4

> Yes, I could to parse the result of "ls -l" but my

> problem is that ther I can find the relative path (in

> my case ../file) instead of absoluth path ( /tmp/file

> ) and I need the absolute path.

The absolute path depends on how it's mounted. So you have to figure out where you are, add the relative path to that, then figure out what the absolute path is. There's no system function to do this (most things don't care about ever calculating an absolute path).

Can I ask why you want to calculate the absolute path? Why can't you just operate on the link itself?

Perl has some modules to do this conversion symbolically (in other words, you feed it the paths, and it gives you the answers. It doesn't traverse the filesystem, but it may call cwd() to find the current directory).

See File::Spec and the rel2abs() function.

--

Darren

Darren_Dunham at 2007-7-7 0:50:51 > top of Java-index,Solaris Operating System,Solaris Essentials - General Technical Questions...
# 5

I'm using the Solaris Security Toolkit and making a customized script for it.

I want to make some change in a file and before this I create a backup of it using one of the above mentioned SST's functions. This backup operation will create a record in a manifest file based on which I will be able to make an undo operation. (it will copy the backed up file to its original location).

The problem is that if I'm referring to the file with it's relative path name then this will be stored to the manifest file and during the undo operation I can't change directories.

pityq at 2007-7-7 0:50:51 > top of Java-index,Solaris Operating System,Solaris Essentials - General Technical Questions...