How do I test for link to a link? The 2019 Stack Overflow Developer Survey Results Are Indifference between non-builtin 'test' and '['unix test when to use eq vs = vs == in test commands?sym link for android-studio/bin/studio.shWhat does `[ EXPRESSION ], [ ] and [OPTION` mean in `man test`?-n Vs !(exclamation mark) behaves differently with test command( test -n $st ) != ( test -z $st ) right?Trace route of a symbolic linkHow to test list of proxy servers?Bash test: what does “=~” do?How to correctly test file's extension in if statement?

What is this business jet?

Why doesn't UInt have a toDouble()?

Can there be female White Walkers?

How to support a colleague who finds meetings extremely tiring?

Why are there uneven bright areas in this photo of black hole?

Why didn't the Event Horizon Telescope team mention Sagittarius A*?

What is the most efficient way to store a numeric range?

Is it ethical to upload a automatically generated paper to a non peer-reviewed site as part of a larger research?

Is bread bad for ducks?

Geography at the pixel level

Old scifi movie from the 50s or 60s with men in solid red uniforms who interrogate a spy from the past

RequirePermission not working

Pokemon Turn Based battle (Python)

Are spiders unable to hurt humans, especially very small spiders?

What is this sharp, curved notch on my knife for?

The difference between dialogue marks

Output the Arecibo Message

Deal with toxic manager when you can't quit

Did the UK government pay "millions and millions of dollars" to try to snag Julian Assange?

Is there a way to generate a uniformly distributed point on a sphere from a fixed amount of random real numbers?

Worn-tile Scrabble

For what reasons would an animal species NOT cross a *horizontal* land bridge?

Loose spokes after only a few rides

Will it cause any balance problems to have PCs level up and gain the benefits of a long rest mid-fight?



How do I test for link to a link?



The 2019 Stack Overflow Developer Survey Results Are Indifference between non-builtin 'test' and '['unix test when to use eq vs = vs == in test commands?sym link for android-studio/bin/studio.shWhat does `[ EXPRESSION ], [ ] and [OPTION` mean in `man test`?-n Vs !(exclamation mark) behaves differently with test command( test -n $st ) != ( test -z $st ) right?Trace route of a symbolic linkHow to test list of proxy servers?Bash test: what does “=~” do?How to correctly test file's extension in if statement?



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








5















I want to test whether a file is a link to another link. I tried readlink but it doesn't work the way I need it:



ralph@bash4.4.12,1:~/subdir1 $ ll
lrwxrwxrwx 1 pi pi 13 Apr 10 14:34 hellolink -> subdir2/hello
lrwxrwxrwx 1 pi pi 9 Apr 10 14:34 hellolink2 -> hellolink
drwxr-xr-x 2 pi pi 4096 Apr 10 14:33 subdir2


Using readlink I now get either the canonicalized form of the ultimate target or the naked filename of the next link (hellolink):



ralph@bash4.4.12,1:~/subdir1 $ readlink -f hellolink2
/home/ralph/subdir1/subdir2/hello
ralph@bash4.4.12,1:~/subdir1 $ readlink hellolink2
hellolink


But what I need is the full path to the file that hellolink2 points at:



/home/ralph/subdir1/hellolink


Right now I'm doing something like this:



if [ -h "$(dirname hellolink2)/$(readlink hellolink2)" ] ; then 
echo hellolink2 is a link
fi


That looks like a lot of overhead when I do it many times in a loop, using find to feed it the filenames.



Is there an easier way?










share|improve this question

















  • 1





    Your test won't work in the case where the target of the symlink is an absolute path, and you probably can get rid of the extra dirname command subst by (conditionally) using some "$var%/*" form. If you really want to make it more light-weight, you'll probably have to use another language, like C, perl, python, etc ;-)

    – mosvy
    yesterday












  • Thanks @mosvy, the construct if [ -h "$FILENAME%/*"/"$(readlink "$FILENAME")" ] ; then ... does the job without the use of dirname. But the problem with readlink and the absolute path persists. Isn't there a command that does the job out of the box? Delivering the canonicalized form of the next linked file? It doesn't appear to be too much to ask.

    – Arjen
    yesterday

















5















I want to test whether a file is a link to another link. I tried readlink but it doesn't work the way I need it:



ralph@bash4.4.12,1:~/subdir1 $ ll
lrwxrwxrwx 1 pi pi 13 Apr 10 14:34 hellolink -> subdir2/hello
lrwxrwxrwx 1 pi pi 9 Apr 10 14:34 hellolink2 -> hellolink
drwxr-xr-x 2 pi pi 4096 Apr 10 14:33 subdir2


Using readlink I now get either the canonicalized form of the ultimate target or the naked filename of the next link (hellolink):



ralph@bash4.4.12,1:~/subdir1 $ readlink -f hellolink2
/home/ralph/subdir1/subdir2/hello
ralph@bash4.4.12,1:~/subdir1 $ readlink hellolink2
hellolink


But what I need is the full path to the file that hellolink2 points at:



/home/ralph/subdir1/hellolink


Right now I'm doing something like this:



if [ -h "$(dirname hellolink2)/$(readlink hellolink2)" ] ; then 
echo hellolink2 is a link
fi


That looks like a lot of overhead when I do it many times in a loop, using find to feed it the filenames.



Is there an easier way?










share|improve this question

















  • 1





    Your test won't work in the case where the target of the symlink is an absolute path, and you probably can get rid of the extra dirname command subst by (conditionally) using some "$var%/*" form. If you really want to make it more light-weight, you'll probably have to use another language, like C, perl, python, etc ;-)

    – mosvy
    yesterday












  • Thanks @mosvy, the construct if [ -h "$FILENAME%/*"/"$(readlink "$FILENAME")" ] ; then ... does the job without the use of dirname. But the problem with readlink and the absolute path persists. Isn't there a command that does the job out of the box? Delivering the canonicalized form of the next linked file? It doesn't appear to be too much to ask.

    – Arjen
    yesterday













5












5








5








I want to test whether a file is a link to another link. I tried readlink but it doesn't work the way I need it:



ralph@bash4.4.12,1:~/subdir1 $ ll
lrwxrwxrwx 1 pi pi 13 Apr 10 14:34 hellolink -> subdir2/hello
lrwxrwxrwx 1 pi pi 9 Apr 10 14:34 hellolink2 -> hellolink
drwxr-xr-x 2 pi pi 4096 Apr 10 14:33 subdir2


Using readlink I now get either the canonicalized form of the ultimate target or the naked filename of the next link (hellolink):



ralph@bash4.4.12,1:~/subdir1 $ readlink -f hellolink2
/home/ralph/subdir1/subdir2/hello
ralph@bash4.4.12,1:~/subdir1 $ readlink hellolink2
hellolink


But what I need is the full path to the file that hellolink2 points at:



/home/ralph/subdir1/hellolink


Right now I'm doing something like this:



if [ -h "$(dirname hellolink2)/$(readlink hellolink2)" ] ; then 
echo hellolink2 is a link
fi


That looks like a lot of overhead when I do it many times in a loop, using find to feed it the filenames.



Is there an easier way?










share|improve this question














I want to test whether a file is a link to another link. I tried readlink but it doesn't work the way I need it:



ralph@bash4.4.12,1:~/subdir1 $ ll
lrwxrwxrwx 1 pi pi 13 Apr 10 14:34 hellolink -> subdir2/hello
lrwxrwxrwx 1 pi pi 9 Apr 10 14:34 hellolink2 -> hellolink
drwxr-xr-x 2 pi pi 4096 Apr 10 14:33 subdir2


Using readlink I now get either the canonicalized form of the ultimate target or the naked filename of the next link (hellolink):



ralph@bash4.4.12,1:~/subdir1 $ readlink -f hellolink2
/home/ralph/subdir1/subdir2/hello
ralph@bash4.4.12,1:~/subdir1 $ readlink hellolink2
hellolink


But what I need is the full path to the file that hellolink2 points at:



/home/ralph/subdir1/hellolink


Right now I'm doing something like this:



if [ -h "$(dirname hellolink2)/$(readlink hellolink2)" ] ; then 
echo hellolink2 is a link
fi


That looks like a lot of overhead when I do it many times in a loop, using find to feed it the filenames.



Is there an easier way?







test readlink






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked yesterday









ArjenArjen

1037




1037







  • 1





    Your test won't work in the case where the target of the symlink is an absolute path, and you probably can get rid of the extra dirname command subst by (conditionally) using some "$var%/*" form. If you really want to make it more light-weight, you'll probably have to use another language, like C, perl, python, etc ;-)

    – mosvy
    yesterday












  • Thanks @mosvy, the construct if [ -h "$FILENAME%/*"/"$(readlink "$FILENAME")" ] ; then ... does the job without the use of dirname. But the problem with readlink and the absolute path persists. Isn't there a command that does the job out of the box? Delivering the canonicalized form of the next linked file? It doesn't appear to be too much to ask.

    – Arjen
    yesterday












  • 1





    Your test won't work in the case where the target of the symlink is an absolute path, and you probably can get rid of the extra dirname command subst by (conditionally) using some "$var%/*" form. If you really want to make it more light-weight, you'll probably have to use another language, like C, perl, python, etc ;-)

    – mosvy
    yesterday












  • Thanks @mosvy, the construct if [ -h "$FILENAME%/*"/"$(readlink "$FILENAME")" ] ; then ... does the job without the use of dirname. But the problem with readlink and the absolute path persists. Isn't there a command that does the job out of the box? Delivering the canonicalized form of the next linked file? It doesn't appear to be too much to ask.

    – Arjen
    yesterday







1




1





Your test won't work in the case where the target of the symlink is an absolute path, and you probably can get rid of the extra dirname command subst by (conditionally) using some "$var%/*" form. If you really want to make it more light-weight, you'll probably have to use another language, like C, perl, python, etc ;-)

– mosvy
yesterday






Your test won't work in the case where the target of the symlink is an absolute path, and you probably can get rid of the extra dirname command subst by (conditionally) using some "$var%/*" form. If you really want to make it more light-weight, you'll probably have to use another language, like C, perl, python, etc ;-)

– mosvy
yesterday














Thanks @mosvy, the construct if [ -h "$FILENAME%/*"/"$(readlink "$FILENAME")" ] ; then ... does the job without the use of dirname. But the problem with readlink and the absolute path persists. Isn't there a command that does the job out of the box? Delivering the canonicalized form of the next linked file? It doesn't appear to be too much to ask.

– Arjen
yesterday





Thanks @mosvy, the construct if [ -h "$FILENAME%/*"/"$(readlink "$FILENAME")" ] ; then ... does the job without the use of dirname. But the problem with readlink and the absolute path persists. Isn't there a command that does the job out of the box? Delivering the canonicalized form of the next linked file? It doesn't appear to be too much to ask.

– Arjen
yesterday










2 Answers
2






active

oldest

votes


















1














Use test -L (without readlink) to see if a file is a symbolic link.



if [ -L hellolink2 ]


Use realpath to get the absolute path of a symlink to a directory.



$ realpath hellolink2
/home/ralph/subdir1/hellolink





share|improve this answer























  • realpath for me gives the same result as readlink -f: ralph:~/subdir1 $ realpath hellolink2 /home/ralph/subdir1/subdir2/hello

    – Arjen
    yesterday


















0














For what it's worth... following the suggestions in the comments above I rewrote the code that gets me the file that a link points to, even if that is another link, plus a few lines to test it:



#!/bin/bash

function nextlinked ()

if [ -h "$1" ]; then # we have a link
linked="$(readlink "$1")"
[ "$linked:0:1" == / ] && echo "$linked"

header=""
add=" "
count=0

find / -print0 | while read -rd '' FILENAME ; do
(( count++ ))
if [ -h "$FILENAME" ] ; then # is this a link?
filename="$FILENAME"
printf "%6d " $count ; ls -ld "$filename" 2>&1
filename="$(nextlinked "$filename" )"
while [ "$filename" ] ; do
header=$header$add
printf "%6d %s" $count "$header" ; ls -ld "$filename" 2>&1
filename="$(nextlinked "$filename")"
done
header=""
fi
done


The question still stands: Is there an existing Linux command that does the job of the function nextlinked?






share|improve this answer























    Your Answer








    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f511600%2fhow-do-i-test-for-link-to-a-link%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Use test -L (without readlink) to see if a file is a symbolic link.



    if [ -L hellolink2 ]


    Use realpath to get the absolute path of a symlink to a directory.



    $ realpath hellolink2
    /home/ralph/subdir1/hellolink





    share|improve this answer























    • realpath for me gives the same result as readlink -f: ralph:~/subdir1 $ realpath hellolink2 /home/ralph/subdir1/subdir2/hello

      – Arjen
      yesterday















    1














    Use test -L (without readlink) to see if a file is a symbolic link.



    if [ -L hellolink2 ]


    Use realpath to get the absolute path of a symlink to a directory.



    $ realpath hellolink2
    /home/ralph/subdir1/hellolink





    share|improve this answer























    • realpath for me gives the same result as readlink -f: ralph:~/subdir1 $ realpath hellolink2 /home/ralph/subdir1/subdir2/hello

      – Arjen
      yesterday













    1












    1








    1







    Use test -L (without readlink) to see if a file is a symbolic link.



    if [ -L hellolink2 ]


    Use realpath to get the absolute path of a symlink to a directory.



    $ realpath hellolink2
    /home/ralph/subdir1/hellolink





    share|improve this answer













    Use test -L (without readlink) to see if a file is a symbolic link.



    if [ -L hellolink2 ]


    Use realpath to get the absolute path of a symlink to a directory.



    $ realpath hellolink2
    /home/ralph/subdir1/hellolink






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered yesterday









    SjoerdSjoerd

    34328




    34328












    • realpath for me gives the same result as readlink -f: ralph:~/subdir1 $ realpath hellolink2 /home/ralph/subdir1/subdir2/hello

      – Arjen
      yesterday

















    • realpath for me gives the same result as readlink -f: ralph:~/subdir1 $ realpath hellolink2 /home/ralph/subdir1/subdir2/hello

      – Arjen
      yesterday
















    realpath for me gives the same result as readlink -f: ralph:~/subdir1 $ realpath hellolink2 /home/ralph/subdir1/subdir2/hello

    – Arjen
    yesterday





    realpath for me gives the same result as readlink -f: ralph:~/subdir1 $ realpath hellolink2 /home/ralph/subdir1/subdir2/hello

    – Arjen
    yesterday













    0














    For what it's worth... following the suggestions in the comments above I rewrote the code that gets me the file that a link points to, even if that is another link, plus a few lines to test it:



    #!/bin/bash

    function nextlinked ()

    if [ -h "$1" ]; then # we have a link
    linked="$(readlink "$1")"
    [ "$linked:0:1" == / ] && echo "$linked"

    header=""
    add=" "
    count=0

    find / -print0 | while read -rd '' FILENAME ; do
    (( count++ ))
    if [ -h "$FILENAME" ] ; then # is this a link?
    filename="$FILENAME"
    printf "%6d " $count ; ls -ld "$filename" 2>&1
    filename="$(nextlinked "$filename" )"
    while [ "$filename" ] ; do
    header=$header$add
    printf "%6d %s" $count "$header" ; ls -ld "$filename" 2>&1
    filename="$(nextlinked "$filename")"
    done
    header=""
    fi
    done


    The question still stands: Is there an existing Linux command that does the job of the function nextlinked?






    share|improve this answer



























      0














      For what it's worth... following the suggestions in the comments above I rewrote the code that gets me the file that a link points to, even if that is another link, plus a few lines to test it:



      #!/bin/bash

      function nextlinked ()

      if [ -h "$1" ]; then # we have a link
      linked="$(readlink "$1")"
      [ "$linked:0:1" == / ] && echo "$linked"

      header=""
      add=" "
      count=0

      find / -print0 | while read -rd '' FILENAME ; do
      (( count++ ))
      if [ -h "$FILENAME" ] ; then # is this a link?
      filename="$FILENAME"
      printf "%6d " $count ; ls -ld "$filename" 2>&1
      filename="$(nextlinked "$filename" )"
      while [ "$filename" ] ; do
      header=$header$add
      printf "%6d %s" $count "$header" ; ls -ld "$filename" 2>&1
      filename="$(nextlinked "$filename")"
      done
      header=""
      fi
      done


      The question still stands: Is there an existing Linux command that does the job of the function nextlinked?






      share|improve this answer

























        0












        0








        0







        For what it's worth... following the suggestions in the comments above I rewrote the code that gets me the file that a link points to, even if that is another link, plus a few lines to test it:



        #!/bin/bash

        function nextlinked ()

        if [ -h "$1" ]; then # we have a link
        linked="$(readlink "$1")"
        [ "$linked:0:1" == / ] && echo "$linked"

        header=""
        add=" "
        count=0

        find / -print0 | while read -rd '' FILENAME ; do
        (( count++ ))
        if [ -h "$FILENAME" ] ; then # is this a link?
        filename="$FILENAME"
        printf "%6d " $count ; ls -ld "$filename" 2>&1
        filename="$(nextlinked "$filename" )"
        while [ "$filename" ] ; do
        header=$header$add
        printf "%6d %s" $count "$header" ; ls -ld "$filename" 2>&1
        filename="$(nextlinked "$filename")"
        done
        header=""
        fi
        done


        The question still stands: Is there an existing Linux command that does the job of the function nextlinked?






        share|improve this answer













        For what it's worth... following the suggestions in the comments above I rewrote the code that gets me the file that a link points to, even if that is another link, plus a few lines to test it:



        #!/bin/bash

        function nextlinked ()

        if [ -h "$1" ]; then # we have a link
        linked="$(readlink "$1")"
        [ "$linked:0:1" == / ] && echo "$linked"

        header=""
        add=" "
        count=0

        find / -print0 | while read -rd '' FILENAME ; do
        (( count++ ))
        if [ -h "$FILENAME" ] ; then # is this a link?
        filename="$FILENAME"
        printf "%6d " $count ; ls -ld "$filename" 2>&1
        filename="$(nextlinked "$filename" )"
        while [ "$filename" ] ; do
        header=$header$add
        printf "%6d %s" $count "$header" ; ls -ld "$filename" 2>&1
        filename="$(nextlinked "$filename")"
        done
        header=""
        fi
        done


        The question still stands: Is there an existing Linux command that does the job of the function nextlinked?







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 11 hours ago









        ArjenArjen

        1037




        1037



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Unix & Linux Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f511600%2fhow-do-i-test-for-link-to-a-link%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            -readlink, test

            Popular posts from this blog

            Frič See also Navigation menuinternal link

            Identify plant with long narrow paired leaves and reddish stems Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?What is this plant with long sharp leaves? Is it a weed?What is this 3ft high, stalky plant, with mid sized narrow leaves?What is this young shrub with opposite ovate, crenate leaves and reddish stems?What is this plant with large broad serrated leaves?Identify this upright branching weed with long leaves and reddish stemsPlease help me identify this bulbous plant with long, broad leaves and white flowersWhat is this small annual with narrow gray/green leaves and rust colored daisy-type flowers?What is this chilli plant?Does anyone know what type of chilli plant this is?Help identify this plant

            fontconfig warning: “/etc/fonts/fonts.conf”, line 100: unknown “element blank” The 2019 Stack Overflow Developer Survey Results Are In“tar: unrecognized option --warning” during 'apt-get install'How to fix Fontconfig errorHow do I figure out which font file is chosen for a system generic font alias?Why are some apt-get-installed fonts being ignored by fc-list, xfontsel, etc?Reload settings in /etc/fonts/conf.dTaking 30 seconds longer to boot after upgrade from jessie to stretchHow to match multiple font names with a single <match> element?Adding a custom font to fontconfigRemoving fonts from fontconfig <match> resultsBroken fonts after upgrading Firefox ESR to latest Firefox