How do I test for link to a link? The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) 2019 Community Moderator Election Resultsdifference 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?

Semisimplicity of the category of coherent sheaves?

Did the new image of black hole confirm the general theory of relativity?

How can I define good in a religion that claims no moral authority?

How to pronounce 1ターン?

Format single node in tikzcd

Take groceries in checked luggage

What's the point in a preamp?

ELI5: Why do they say that Israel would have been the fourth country to land a spacecraft on the Moon and why do they call it low cost?

Didn't get enough time to take a Coding Test - what to do now?

Windows 10: How to Lock (not sleep) laptop on lid close?

How to delete random line from file using Unix command?

Did God make two great lights or did He make the great light two?

Was credit for the black hole image misattributed?

How do I add random spotting to the same face in cycles?

Why can't wing-mounted spoilers be used to steepen approaches?

Cooking pasta in a water boiler

He got a vote 80% that of Emmanuel Macron’s

How to test the equality of two Pearson correlation coefficients computed from the same sample?

Can the DM override racial traits?

Can smartphones with the same camera sensor have different image quality?

What can I do if neighbor is blocking my solar panels intentionally?

Road tyres vs "Street" tyres for charity ride on MTB Tandem

Sort a list of pairs representing an acyclic, partial automorphism

I could not break this equation. Please help me



How do I test for link to a link?



The 2019 Stack Overflow Developer Survey Results Are In
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
2019 Community Moderator Election Resultsdifference 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
    Apr 10 at 9:06












  • 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
    Apr 10 at 10:59

















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
    Apr 10 at 9:06












  • 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
    Apr 10 at 10:59













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 Apr 10 at 7:07









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
    Apr 10 at 9:06












  • 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
    Apr 10 at 10:59












  • 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
    Apr 10 at 9:06












  • 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
    Apr 10 at 10:59







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
Apr 10 at 9:06






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
Apr 10 at 9:06














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
Apr 10 at 10:59





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
Apr 10 at 10:59










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
    Apr 10 at 7:52


















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 ()


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
      Apr 10 at 7:52















    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
      Apr 10 at 7:52













    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 Apr 10 at 7:27









    SjoerdSjoerd

    34328




    34328












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

      – Arjen
      Apr 10 at 7:52

















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

      – Arjen
      Apr 10 at 7:52
















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

    – Arjen
    Apr 10 at 7:52





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

    – Arjen
    Apr 10 at 7:52













    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 ()


    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 ()


      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 ()


        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 ()


        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 2 days 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







            Popular posts from this blog

            getting Checkpoint VPN SSL Network Extender working in the command lineHow to connect to CheckPoint VPN on Ubuntu 18.04LTS?Will the Linux ( red-hat ) Open VPNC Client connect to checkpoint or nortel VPN gateways?VPN client for linux machine + support checkpoint gatewayVPN SSL Network Extender in FirefoxLinux Checkpoint SNX tool configuration issuesCheck Point - Connect under Linux - snx + OTPSNX VPN Ububuntu 18.XXUsing Checkpoint VPN SSL Network Extender CLI with certificateVPN with network manager (nm-applet) is not workingWill the Linux ( red-hat ) Open VPNC Client connect to checkpoint or nortel VPN gateways?VPN client for linux machine + support checkpoint gatewayImport VPN config files to NetworkManager from command lineTrouble connecting to VPN using network-manager, while command line worksStart a VPN connection with PPTP protocol on command linestarting a docker service daemon breaks the vpn networkCan't connect to vpn with Network-managerVPN SSL Network Extender in FirefoxUsing Checkpoint VPN SSL Network Extender CLI with certificate

            Cannot Extend partition with GParted The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) 2019 Community Moderator Election ResultsCan't increase partition size with GParted?GParted doesn't recognize the unallocated space after my current partitionWhat is the best way to add unallocated space located before to Ubuntu 12.04 partition with GParted live?I can't figure out how to extend my Arch home partition into free spaceGparted Linux Mint 18.1 issueTrying to extend but swap partition is showing as Unknown in Gparted, shows proper from fdiskRearrange partitions in gparted to extend a partitionUnable to extend partition even though unallocated space is next to it using GPartedAllocate free space to root partitiongparted: how to merge unallocated space with a partition

            Marilyn Monroe Ny fiainany manokana | Jereo koa | Meny fitetezanafanitarana azy.