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

            Mobil Contents History Mobil brands Former Mobil brands Lukoil transaction Mobil UK Mobil Australia Mobil New Zealand Mobil Greece Mobil in Japan Mobil in Canada Mobil Egypt See also References External links Navigation menuwww.mobil.com"Mobil Corporation"the original"Our Houston campus""Business & Finance: Socony-Vacuum Corp.""Popular Mechanics""Lubrite Technologies""Exxon Mobil campus 'clearly happening'""Toledo Blade - Google News Archive Search""The Lion and the Moose - How 2 Executives Pulled off the Biggest Merger Ever""ExxonMobil Press Release""Lubricants""Archived copy"the original"Mobil 1™ and Mobil Super™ motor oil and synthetic motor oil - Mobil™ Motor Oils""Mobil Delvac""Mobil Industrial website""The State of Competition in Gasoline Marketing: The Effects of Refiner Operations at Retail""Mobil Travel Guide to become Forbes Travel Guide""Hotel Rankings: Forbes Merges with Mobil"the original"Jamieson oil industry history""Mobil news""Caltex pumps for control""Watchdog blocks Caltex bid""Exxon Mobil sells service station network""Mobil Oil New Zealand Limited is New Zealand's oldest oil company, with predecessor companies having first established a presence in the country in 1896""ExxonMobil subsidiaries have a business history in New Zealand stretching back more than 120 years. We are involved in petroleum refining and distribution and the marketing of fuels, lubricants and chemical products""Archived copy"the original"Exxon Mobil to Sell Its Japanese Arm for $3.9 Billion""Gas station merger will end Esso and Mobil's long run in Japan""Esso moves to affiliate itself with PC Optimum, no longer Aeroplan, in loyalty point switch""Mobil brand of gas stations to launch in Canada after deal for 213 Loblaws-owned locations""Mobil Nears Completion of Rebranding 200 Loblaw Gas Stations""Learn about ExxonMobil's operations in Egypt""Petrol and Diesel Service Stations in Egypt - Mobil"Official websiteExxon Mobil corporate websiteMobil Industrial official websiteeeeeeeeDA04275022275790-40000 0001 0860 5061n82045453134887257134887257

            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