It is currently Tue, 17 May 2022 19:17:55 GMT



 
Author Message
 Target of a symlink
Is there any nice way of getting at the name of the target of a symlink?
All I could think of was:

ls -l "`dirname $1`" | grep "`basename $1`" | sed s/.*-\>\ //

which is horrible. I assume "ls -L" helps but I can't figure out how.

Help would be much appreciated...



 Tue, 14 Oct 2003 20:35:40 GMT   
 Target of a symlink

Uri> Is there any nice way of getting at the name of the target of a symlink?
Uri> All I could think of was:

Uri> ls -l "`dirname $1`" | grep "`basename $1`" | sed s/.*-\>\ //

Uri> which is horrible. I assume "ls -L" helps but I can't figure out how.

perl -e 'print readlink shift' your-file-name-here

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<mer...@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!



 Tue, 14 Oct 2003 22:16:56 GMT   
 Target of a symlink
Randal L. Schwartz <mer...@stonehenge.com> wrote:

Is a there a solution in perl which _isn't_ a one-liner!?

Here is a general purpose ksh script to do what you want.

=Brian

#! /bin/echo error: only source
#*TAG:42576 4:Jan 9 1973:0755:resolvepath:
# Author: Brian Hiles <b...@iname.com>
# Copyright: (c) 2000
# Description: resolve and canonicize full pathname of arguments
# Name: resolvepath
# Requires:
# Sccs: @(#)resolvepath.sh 1.8 1997/12 b...@iname.com (Brian Hiles)
# See-also: File-PathConvert-0.4.tar (perl)
# Usage: resolvepath [-hlp] path...
# Version: 1.08

#01
function resolvepath # [-hlp] path...
{       set -o noglob
        # Ksh Bug: OPTIND cannot be declared integer!
        typeset -i rc
        typeset IFS OPTARG arg dir fn headers= symlink= oarg opt usepath=
        while getopts :HhLlPp opt
        do      case $opt in
                (h)     headers=ON ;;
                (+h|H)  headers=OFF ;;
                (l)     symlink=ON ;;
                (+l|L)  symlink= ;;
                (p)     usepath=ON ;;
                (+p|P)  usepath= ;;
                ([:?])  print -ru2 "usage: $0 [-hlp] path...
-h      - prepend the output with argument header
-l      - show logical path with symlinks resolved [physical path]
-p      - apply path lookup, if applicable"
                        return 2 ;;
                esac
        done
        shift OPTIND-1
        if [[ $headers = OFF ]]
        then    headers=
        else    (($#>1)) && headers=ON
        fi
        for arg
        do      oarg=$arg
                if [[ $usepath = ON && ! -d $arg ]]
                then    # Ksh Bug: "whence" cannot handle args with spaces
                        arg=$(whence -p "$arg") ||
                        {       print -ru2 'resolvepath: whence: error:' \
                                "\"$oarg\" not found"
                                rc=rc+1 continue
                        }
                fi
                [[ -a $arg ]] ||
                {       print -ru2 "resolvepath: error: \"$arg\" not found"
                        rc=rc+1 continue
                }
                [[ $arg != */* ]] && arg="./$arg"
                if [[ -d $arg ]]
                then    dir=$arg fn=
                else    dir=${arg%/*} fn=${arg##*/}
                fi
${DIAG:+print -ru2 "[ $0: dirpart=\"$dir\", filepart=\"$fn\" ]"}
                # Ksh Bug: "cd -P dir" works, but "cd -P -- dir" does not!
                [[ $dir = -* ]] && dir="./$dir"       # work-around for above bug
                \cd ${symlink:+-P} "$dir" || rc=rc+1 continue
                print -r -- "${headers:+$oarg:     }${PWD%/}/$fn"     # <= TAB
                \cd - >&-
        done
        return $rc

#02 EMBEDDED MAN-PAGE FOR "src2man"
: '
#++
NAME
        resolvepath - resolve and canonicize the full pathname of argument

SYNOPSIS
        resolvepath [-hlp] path...

OPTIONS
        -h      - Prepend the output with argument header.
        -l      - Show logical path with symlinks resolved. [physical path]
        -p      - Apply path lookup, if applicable.

DESCRIPTION
        ...

        If and only if the path is a directory, the output is guaranteed
        to be terminated with a slash ("/").

RETURN CODE
        Returns 0 if successful, 2 for options parsing errors, otherwise
        the number of arguments in error.

EXAMPLE
        $ resolvepath .
        /home/brian/side/lib/

        $ resolvepath cat
        resolvepath: error: "cat" not found
        $ resolvepath -p cat
        /bin/cat
        $ resolvepath -lp cat
        /usr/bin/cat

        $ resolvepath /
        /
        $ resolvepath //
        /
        $ resolvepath /..
        /
        $ resolvepath /.//..///.////../////.//////..///////.////////..
        /

ENVIRONMENT
        PATH

SEE ALSO
        predictshell(3S), stat(3S)

AUTHOR
        Brian Hiles <b...@iname.com>

CAVEATS
        The algorithm that is used requires the directory component of
        the resolved argument to be executable; i.e. you must have
        permission to chdir to it.

BUGS
        Filenames with embedded spaces will be failed to be recognized;
        this is a bug in the ksh builtin "whence", not resolvepath(3S).

#--
'



 Wed, 15 Oct 2003 01:38:38 GMT   
 Target of a symlink
In article <9cbp8m$lv...@pegasus.csx.cam.ac.uk>,
        "Uri Zarfaty" <ud...@cam.ac.uk> writes:

"ls -L" seems to give you size and date of the referenced file, but not the
name. How about the following?

  ls -l "$1" | cut -f 2 -d ">" | cut -c 2-

or

  ls -l "$1" | cut -f 14 -d " "

--

Frederick



 Wed, 15 Oct 2003 04:50:32 GMT   
 Target of a symlink

ls -l <linkname> | awk '{print $11}'

--
Regards,

Daniel E. Lincoln - Ford Motor Company
R&VT Digital Build & Evaluation
http://ae0581.pd8.ford.com/vcpm



 Wed, 15 Oct 2003 04:29:20 GMT   
 Target of a symlink

brian> Randal L. Schwartz <mer...@stonehenge.com> wrote:

brian> Is a there a solution in perl which _isn't_ a one-liner!?

Depends on the length of the line permitted.   If infinite, no. :)
Unlike Python, we *can* write one-liners. :)

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<mer...@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!



 Thu, 16 Oct 2003 00:35:17 GMT   
 
   [ 6 post ] 

Similar Threads

1. Get symlink target with shell commands

2. Finding a symlink's target

3. iptables: rule with RETURN target just after a rule with ACCEPT target

4. iptables: rule with RETURN target after a rule with the ACCEPT target

5. Target 0: Target not connected

6. "Symlink component flag not implemented": problem with long symlinks?

7. RH 9 Upgrading: /usr/tmp symlink Error

8. moving all files (and symlinks) to new partition

9. Could symlinks prevent /mnt/cdrom access

10. Multiple System.map symlinks/files ???


 
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software