Operating System - HP-UX
1833776 Members
2057 Online
110063 Solutions
New Discussion

Re: Determine soft link directory

 
Amrit_1
Advisor

Determine soft link directory

Location of D is /temp/
I have a soft link pointing from /A/B/C-> D.

I execute the command cd /A/B/C to login to directory D.

If I execute the dirname /A/B/C I get the o/p as /A/B. But the requirement that I need is the actually location of D.

Is there any simple way of doing this?
8 REPLIES 8
T G Manikandan
Honored Contributor

Re: Determine soft link directory

#ln -s /temp/d /a/b/c

# cd /a/b/c
This will now point to temp/d

Here c is link which points to /temp/d
Sanjay Kumar Suri
Honored Contributor

Re: Determine soft link directory

dirname will only extract portions of path names.

So to get what you need I afraid you have to give $dirname /temp/D only.

sks
A rigid mind is very sure, but often wrong. A flexible mind is generally unsure, but often right.
Amrit_1
Advisor

Re: Determine soft link directory

I shall explain the problem a bit more in detail:

In the script I have a default location: /A/B/C. The user has the option of creating a directory /temp/D. If this done from the default there is a softlink pointing to the non-default.

Now the script recognizes only the default and not the non default. I need to get the softlink path so that I populate the parent directory of the softlink i.e. /temp directory.

One option is do a ll -ls -> grep "directory" -> awk NF to get the exact path of the soft link and then do dirname path

But the above is cumbersome and is round about method. Is there any faster method of accessing.
H.Merijn Brand (procura
Honored Contributor

Re: Determine soft link directory

lt09:/tmp 105 > ln -s /A/B/D D
lt09:/tmp 106 > perl -le'print readlink for @ARGV' D
/A/B/D
lt09:/tmp 107 >

This does *NOT* guarantee that the target folder does exist. It is still a symbolic link

lt09:/tmp 107 > ln -s /usr/tmp E
lt09:/tmp 108 > perl -MCwd -le'print Cwd::realpath $_ for @ARGV' D E

/var/tmp
lt09:/tmp 109 >

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
G. Vrijhoeven
Honored Contributor

Re: Determine soft link directory

Hi Amit,

You can use pwd for dir that includes softlink dir and /bin/pwd for real location.

HTH,

Gideon

Amrit_1
Advisor

Re: Determine soft link directory

I want the implementation in unix scripting and not in perl. thanks for that info.

I tried doing a pwd on /A/B/C. The output is /A/B/C and not what I wanted i.e. the location D.

Please let me know in case I have misunderstood the explaination.
Jean-Louis Phelix
Honored Contributor

Re: Determine soft link directory

Hi,

Previous tip should work :

$ ls -l SdDD
lrwxr-xr-x 1 phelix CST 12 Feb 22 2003 SdDD -> /homeHA/SdDD
$ A=$(cd SdDD ; pwd)
$ B=$(dirname $(cd SdDD ; pwd))
$ echo $A $B
/homeHA/phelix/SdDD /homeHA/phelix
$ A=$(cd SdDD ; /usr/bin/pwd)
$ B=$(dirname $(cd SdDD ; /usr/bin/pwd))
$ echo $A $B
/homeHA/SdDD /homeHA

The only problem is if the target sym link doesn't exist ...

Regards.
It works for me (© Bill McNAMARA ...)
Elmar P. Kolkman
Honored Contributor

Re: Determine soft link directory

Don't use the default pwd. It might use the shell buitltin, which doesn't work like you want. Use /bin/pwd to make sure you get the real working dir.
Every problem has at least one solution. Only some solutions are harder to find.