Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

question about soft links

0 views
Skip to first unread message

dn....@gmail.com

unread,
Sep 3, 2008, 2:34:44 AM9/3/08
to

I have a directory /home/webroot/reports. It is a soft link to a
directory. The 'real' directory is /home/scripts/reports. I can run
'ls -l /home/webroot/reports' to find out what it links to.

But how can I find out which directory is linked to /home/scripts/
reports ?


Secondly, if I run:
cd /home/webroot/reports
cd ..
===> I end up in /home/webroot

Can I do 'cd /home/webroot/reports' and then do something which will
set PWD to the 'real' directory; viz. '/home/scripts/reports'. Can it
be done?

Thanks in advance.

Andrew McDermott

unread,
Sep 3, 2008, 3:31:17 AM9/3/08
to
on Wednesday 03 September 2008 07:34 dn....@gmail.com wrote:

>
> I have a directory /home/webroot/reports. It is a soft link to a
> directory. The 'real' directory is /home/scripts/reports. I can run
> 'ls -l /home/webroot/reports' to find out what it links to.
>
> But how can I find out which directory is linked to /home/scripts/
> reports ?

I don't think you can directly. You could try:

find / -type l -print | xargs file

This will report all the symbolic links on your system (depending on your
system you may need to use file -L). You can then use grep for your
original directory. If your system consistently uses spaces in directory
names you may need to use -print0 in the find command and the -0 option to
xargs:

find / -type l -print0 | xargs -0 file

>
>
> Secondly, if I run:
> cd /home/webroot/reports
> cd ..
> ===> I end up in /home/webroot
>
> Can I do 'cd /home/webroot/reports' and then do something which will
> set PWD to the 'real' directory; viz. '/home/scripts/reports'. Can it
> be done?

Modern shells such as bash and ksh have a PWD envirnment variable which is
set every time you change directory. When you do "cd .." you effectively
do "cd `dirname $PWD`"

cd `/bin/pwd` # forces the pwd command which then starts by looking for
# your real working directory
cd `pwd -P` # forces the builtin pwd to use the real working directory

I hope this helps.

Andrew

Geoff Clare

unread,
Sep 4, 2008, 8:42:14 AM9/4/08
to
Andrew McDermott wrote:

> on Wednesday 03 September 2008 07:34 dn....@gmail.com wrote:
>

[ /home/webroot/reports is a symlink to /home/scripts/reports ]


>
>> Secondly, if I run:
>> cd /home/webroot/reports
>> cd ..
>> ===> I end up in /home/webroot
>>
>> Can I do 'cd /home/webroot/reports' and then do something which will
>> set PWD to the 'real' directory; viz. '/home/scripts/reports'. Can it
>> be done?
>
> Modern shells such as bash and ksh have a PWD envirnment variable which is
> set every time you change directory. When you do "cd .." you effectively
> do "cd `dirname $PWD`"
>
> cd `/bin/pwd` # forces the pwd command which then starts by looking for
> # your real working directory
> cd `pwd -P` # forces the builtin pwd to use the real working directory

Slightly simpler is just:

cd -P .

This is a no-op except for the side-effect of -P that it sets
PWD to be the physical path for the current directory.

Of course the OP could also just do:

cd /home/webroot/reports
cd -P ..

rather than:

cd /home/webroot/reports
cd -P .
cd ..

--
Geoff Clare <net...@gclare.org.uk>

0 new messages