Re: Calculating the target of a symlink

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 19Jun2010 21:35, Patrick O'Callaghan <pocallaghan@xxxxxxxxx> wrote:
| A script I'm writing needs to work out the target of a symbolic link,
| i.e. given:
| 
| $ touch foo
| $ ln -s foo bar
| 
| the function should print bar when given foo as a parameter.

>From your examples below, you mean "print foo when given bar as a
parameter". I'll presume you meant that.

| The manual
| says "ls -L" should do this,

No, it doesn't. It prints information _about_ foo, not "foo".  i.e. it does a
stat(2) instead of an lstat(2) call. From the manual:

  -L, --dereference
        when showing file information for a symbolic link, show information 
        for  the file the link references rather than for the link itself

So plain "ls -ld bar" shows that it is a symlink. "ls -ldL bar" shows
information about "foo" (not _not_ its name - it just uses "bar" to
access through the symlink - the OS reads the string "foo" and follows
it to the file "bar" - the ls command never sees it).

| but it doesn't seem to work:
| 
| $ touch foo
| $ ln -s foo bar
| $ ls -l foo bar
| lrwxrwxrwx 1 poc poc 3 Jun 19 21:32 bar -> foo
| -rw-rw-r-- 1 poc poc 0 Jun 19 21:32 foo
| $ ls -L bar
| bar
| 
| (should give foo)
| 
| Have I misunderstood what "ls -L" does?

Yes.

| Is there a bug?

No.

| And is there a better way of doing this?

man 1 readlink

linkvalue=`readlink bar`        # gets "foo"

The readlink command is a GNU command - not portable to UNIX systems which
don't have the GNU commands grafted on. Of course it is a one line perl
or python script do implement this on a system with no readlink command.

Cheers,
-- 
Cameron Simpson <cs@xxxxxxxxxx> DoD#743
http://www.cskk.ezoshosting.com/cs/

We all lie, cheat and steal, mostly a little, sometimes a lot. Relax.
        - Henry Prange - biker/thermoregulation physiologist
          DoD#0821, <prange@xxxxxxxxxxxxxxxxxxxxxx>
-- 
users mailing list
users@xxxxxxxxxxxxxxxxxxxxxxx
To unsubscribe or change subscription options:
https://admin.fedoraproject.org/mailman/listinfo/users
Guidelines: http://fedoraproject.org/wiki/Mailing_list_guidelines


[Index of Archives]     [Current Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Yosemite News]     [Yosemite Photos]     [KDE Users]     [Fedora Tools]     [Fedora Docs]

  Powered by Linux