ln: create symlink using another symlink

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












Let's suppose I have one file and one directory:



[user@localhost Tests]$ ls -l
total 4
drwxrwxr-x. 2 user user 4096 Oct 8 09:53 dir
-rw-rw-r--. 1 user user 0 Oct 8 09:53 file


I created symlink to file called symlink1, and symlink to dir called dirslink1:



[user@localhost Tests]$ ls -l
drwxrwxr-x. 2 user user 4096 Oct 8 09:53 dir
lrwxrwxrwx. 1 user user 3 Oct 8 10:03 dirslink1 -> dir
-rw-rw-r--. 5 user user 0 Oct 8 09:53 file
lrwxrwxrwx. 1 user user 4 Oct 8 09:53 symlink1 -> file


Now I created symlinks to symlink1 using ln -s and ln -sL:



[user@localhost Tests]$ ln -s symlink1 symlink2
[user@localhost Tests]$ ln -sL symlink1 symlink3
[user@localhost Tests]$ ln -s dirslink1 dirslink2
[user@localhost Tests]$ ln -sL dirslink1 dirslink3


Now, as fat as I understand symlink3 should point to file and dirslink3 should point to dir. But when I check it, none of the symlink[23] and dirslink [23] points to the original file/directory:



[user@localhost Tests]$ ls -l
drwxrwxr-x. 2 user user 4096 Oct 8 09:53 dir
lrwxrwxrwx. 1 user user 3 Oct 8 10:03 dirslink1 -> dir
lrwxrwxrwx. 1 user user 9 Oct 8 10:03 dirslink2 -> dirslink1
lrwxrwxrwx. 1 user user 9 Oct 8 10:03 dirslink3 -> dirslink1
-rw-rw-r--. 5 user user 0 Oct 8 09:53 file
lrwxrwxrwx. 1 user user 4 Oct 8 09:53 symlink1 -> file
lrwxrwxrwx. 1 user user 8 Oct 8 09:54 symlink2 -> symlink1
lrwxrwxrwx. 1 user user 8 Oct 8 09:54 symlink3 -> symlink1


The question is: Is it possible/How to create symlink to original file using another symlink?










share|improve this question

























    up vote
    1
    down vote

    favorite












    Let's suppose I have one file and one directory:



    [user@localhost Tests]$ ls -l
    total 4
    drwxrwxr-x. 2 user user 4096 Oct 8 09:53 dir
    -rw-rw-r--. 1 user user 0 Oct 8 09:53 file


    I created symlink to file called symlink1, and symlink to dir called dirslink1:



    [user@localhost Tests]$ ls -l
    drwxrwxr-x. 2 user user 4096 Oct 8 09:53 dir
    lrwxrwxrwx. 1 user user 3 Oct 8 10:03 dirslink1 -> dir
    -rw-rw-r--. 5 user user 0 Oct 8 09:53 file
    lrwxrwxrwx. 1 user user 4 Oct 8 09:53 symlink1 -> file


    Now I created symlinks to symlink1 using ln -s and ln -sL:



    [user@localhost Tests]$ ln -s symlink1 symlink2
    [user@localhost Tests]$ ln -sL symlink1 symlink3
    [user@localhost Tests]$ ln -s dirslink1 dirslink2
    [user@localhost Tests]$ ln -sL dirslink1 dirslink3


    Now, as fat as I understand symlink3 should point to file and dirslink3 should point to dir. But when I check it, none of the symlink[23] and dirslink [23] points to the original file/directory:



    [user@localhost Tests]$ ls -l
    drwxrwxr-x. 2 user user 4096 Oct 8 09:53 dir
    lrwxrwxrwx. 1 user user 3 Oct 8 10:03 dirslink1 -> dir
    lrwxrwxrwx. 1 user user 9 Oct 8 10:03 dirslink2 -> dirslink1
    lrwxrwxrwx. 1 user user 9 Oct 8 10:03 dirslink3 -> dirslink1
    -rw-rw-r--. 5 user user 0 Oct 8 09:53 file
    lrwxrwxrwx. 1 user user 4 Oct 8 09:53 symlink1 -> file
    lrwxrwxrwx. 1 user user 8 Oct 8 09:54 symlink2 -> symlink1
    lrwxrwxrwx. 1 user user 8 Oct 8 09:54 symlink3 -> symlink1


    The question is: Is it possible/How to create symlink to original file using another symlink?










    share|improve this question























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      Let's suppose I have one file and one directory:



      [user@localhost Tests]$ ls -l
      total 4
      drwxrwxr-x. 2 user user 4096 Oct 8 09:53 dir
      -rw-rw-r--. 1 user user 0 Oct 8 09:53 file


      I created symlink to file called symlink1, and symlink to dir called dirslink1:



      [user@localhost Tests]$ ls -l
      drwxrwxr-x. 2 user user 4096 Oct 8 09:53 dir
      lrwxrwxrwx. 1 user user 3 Oct 8 10:03 dirslink1 -> dir
      -rw-rw-r--. 5 user user 0 Oct 8 09:53 file
      lrwxrwxrwx. 1 user user 4 Oct 8 09:53 symlink1 -> file


      Now I created symlinks to symlink1 using ln -s and ln -sL:



      [user@localhost Tests]$ ln -s symlink1 symlink2
      [user@localhost Tests]$ ln -sL symlink1 symlink3
      [user@localhost Tests]$ ln -s dirslink1 dirslink2
      [user@localhost Tests]$ ln -sL dirslink1 dirslink3


      Now, as fat as I understand symlink3 should point to file and dirslink3 should point to dir. But when I check it, none of the symlink[23] and dirslink [23] points to the original file/directory:



      [user@localhost Tests]$ ls -l
      drwxrwxr-x. 2 user user 4096 Oct 8 09:53 dir
      lrwxrwxrwx. 1 user user 3 Oct 8 10:03 dirslink1 -> dir
      lrwxrwxrwx. 1 user user 9 Oct 8 10:03 dirslink2 -> dirslink1
      lrwxrwxrwx. 1 user user 9 Oct 8 10:03 dirslink3 -> dirslink1
      -rw-rw-r--. 5 user user 0 Oct 8 09:53 file
      lrwxrwxrwx. 1 user user 4 Oct 8 09:53 symlink1 -> file
      lrwxrwxrwx. 1 user user 8 Oct 8 09:54 symlink2 -> symlink1
      lrwxrwxrwx. 1 user user 8 Oct 8 09:54 symlink3 -> symlink1


      The question is: Is it possible/How to create symlink to original file using another symlink?










      share|improve this question













      Let's suppose I have one file and one directory:



      [user@localhost Tests]$ ls -l
      total 4
      drwxrwxr-x. 2 user user 4096 Oct 8 09:53 dir
      -rw-rw-r--. 1 user user 0 Oct 8 09:53 file


      I created symlink to file called symlink1, and symlink to dir called dirslink1:



      [user@localhost Tests]$ ls -l
      drwxrwxr-x. 2 user user 4096 Oct 8 09:53 dir
      lrwxrwxrwx. 1 user user 3 Oct 8 10:03 dirslink1 -> dir
      -rw-rw-r--. 5 user user 0 Oct 8 09:53 file
      lrwxrwxrwx. 1 user user 4 Oct 8 09:53 symlink1 -> file


      Now I created symlinks to symlink1 using ln -s and ln -sL:



      [user@localhost Tests]$ ln -s symlink1 symlink2
      [user@localhost Tests]$ ln -sL symlink1 symlink3
      [user@localhost Tests]$ ln -s dirslink1 dirslink2
      [user@localhost Tests]$ ln -sL dirslink1 dirslink3


      Now, as fat as I understand symlink3 should point to file and dirslink3 should point to dir. But when I check it, none of the symlink[23] and dirslink [23] points to the original file/directory:



      [user@localhost Tests]$ ls -l
      drwxrwxr-x. 2 user user 4096 Oct 8 09:53 dir
      lrwxrwxrwx. 1 user user 3 Oct 8 10:03 dirslink1 -> dir
      lrwxrwxrwx. 1 user user 9 Oct 8 10:03 dirslink2 -> dirslink1
      lrwxrwxrwx. 1 user user 9 Oct 8 10:03 dirslink3 -> dirslink1
      -rw-rw-r--. 5 user user 0 Oct 8 09:53 file
      lrwxrwxrwx. 1 user user 4 Oct 8 09:53 symlink1 -> file
      lrwxrwxrwx. 1 user user 8 Oct 8 09:54 symlink2 -> symlink1
      lrwxrwxrwx. 1 user user 8 Oct 8 09:54 symlink3 -> symlink1


      The question is: Is it possible/How to create symlink to original file using another symlink?







      bash ln






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 45 mins ago









      mrc02_kr

      759319




      759319




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          -L only works with hard links; as specified in POSIX:




          If the -s option is specified, the -L and -P options shall be silently ignored.




          If you have readlink you can use that:



          ln -s -- "$(readlink symlink1)" symlink4


          If your readlink supports the -f option, you can use that to fully canonicalise the target (i.e. resolve all symlinks in the target’s path, if the target symlink includes other symlinks).






          share|improve this answer






















          • Great, this is actually what I'm looking for
            – mrc02_kr
            27 mins ago










          • Thanks @Stéphane, one day it will sink in :-/.
            – Stephen Kitt
            14 mins ago

















          up vote
          2
          down vote













          You can use cp -P to make a copy of a symlink:



          cp -P symlink2 symlink3


          Beware that if the target of the symlink is relative, the above would probably result in a broken link if the copy is not in the same directory as the original.



          With zsh,



          ln -s -- symlink2(:P) symlink3


          Would create symlink3 as a symlink to the full canonical (symlink-free) path symlink2, like ln -s -- "$(readlink -f symlink2)" symlink3 but more reliable in that it would still work correctly if that path ended in newline characters.






          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',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            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%2f473928%2fln-create-symlink-using-another-symlink%23new-answer', 'question_page');

            );

            Post as a guest






























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            2
            down vote



            accepted










            -L only works with hard links; as specified in POSIX:




            If the -s option is specified, the -L and -P options shall be silently ignored.




            If you have readlink you can use that:



            ln -s -- "$(readlink symlink1)" symlink4


            If your readlink supports the -f option, you can use that to fully canonicalise the target (i.e. resolve all symlinks in the target’s path, if the target symlink includes other symlinks).






            share|improve this answer






















            • Great, this is actually what I'm looking for
              – mrc02_kr
              27 mins ago










            • Thanks @Stéphane, one day it will sink in :-/.
              – Stephen Kitt
              14 mins ago














            up vote
            2
            down vote



            accepted










            -L only works with hard links; as specified in POSIX:




            If the -s option is specified, the -L and -P options shall be silently ignored.




            If you have readlink you can use that:



            ln -s -- "$(readlink symlink1)" symlink4


            If your readlink supports the -f option, you can use that to fully canonicalise the target (i.e. resolve all symlinks in the target’s path, if the target symlink includes other symlinks).






            share|improve this answer






















            • Great, this is actually what I'm looking for
              – mrc02_kr
              27 mins ago










            • Thanks @Stéphane, one day it will sink in :-/.
              – Stephen Kitt
              14 mins ago












            up vote
            2
            down vote



            accepted







            up vote
            2
            down vote



            accepted






            -L only works with hard links; as specified in POSIX:




            If the -s option is specified, the -L and -P options shall be silently ignored.




            If you have readlink you can use that:



            ln -s -- "$(readlink symlink1)" symlink4


            If your readlink supports the -f option, you can use that to fully canonicalise the target (i.e. resolve all symlinks in the target’s path, if the target symlink includes other symlinks).






            share|improve this answer














            -L only works with hard links; as specified in POSIX:




            If the -s option is specified, the -L and -P options shall be silently ignored.




            If you have readlink you can use that:



            ln -s -- "$(readlink symlink1)" symlink4


            If your readlink supports the -f option, you can use that to fully canonicalise the target (i.e. resolve all symlinks in the target’s path, if the target symlink includes other symlinks).







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 25 mins ago









            Stéphane Chazelas

            287k53531868




            287k53531868










            answered 35 mins ago









            Stephen Kitt

            149k23331397




            149k23331397











            • Great, this is actually what I'm looking for
              – mrc02_kr
              27 mins ago










            • Thanks @Stéphane, one day it will sink in :-/.
              – Stephen Kitt
              14 mins ago
















            • Great, this is actually what I'm looking for
              – mrc02_kr
              27 mins ago










            • Thanks @Stéphane, one day it will sink in :-/.
              – Stephen Kitt
              14 mins ago















            Great, this is actually what I'm looking for
            – mrc02_kr
            27 mins ago




            Great, this is actually what I'm looking for
            – mrc02_kr
            27 mins ago












            Thanks @Stéphane, one day it will sink in :-/.
            – Stephen Kitt
            14 mins ago




            Thanks @Stéphane, one day it will sink in :-/.
            – Stephen Kitt
            14 mins ago












            up vote
            2
            down vote













            You can use cp -P to make a copy of a symlink:



            cp -P symlink2 symlink3


            Beware that if the target of the symlink is relative, the above would probably result in a broken link if the copy is not in the same directory as the original.



            With zsh,



            ln -s -- symlink2(:P) symlink3


            Would create symlink3 as a symlink to the full canonical (symlink-free) path symlink2, like ln -s -- "$(readlink -f symlink2)" symlink3 but more reliable in that it would still work correctly if that path ended in newline characters.






            share|improve this answer


























              up vote
              2
              down vote













              You can use cp -P to make a copy of a symlink:



              cp -P symlink2 symlink3


              Beware that if the target of the symlink is relative, the above would probably result in a broken link if the copy is not in the same directory as the original.



              With zsh,



              ln -s -- symlink2(:P) symlink3


              Would create symlink3 as a symlink to the full canonical (symlink-free) path symlink2, like ln -s -- "$(readlink -f symlink2)" symlink3 but more reliable in that it would still work correctly if that path ended in newline characters.






              share|improve this answer
























                up vote
                2
                down vote










                up vote
                2
                down vote









                You can use cp -P to make a copy of a symlink:



                cp -P symlink2 symlink3


                Beware that if the target of the symlink is relative, the above would probably result in a broken link if the copy is not in the same directory as the original.



                With zsh,



                ln -s -- symlink2(:P) symlink3


                Would create symlink3 as a symlink to the full canonical (symlink-free) path symlink2, like ln -s -- "$(readlink -f symlink2)" symlink3 but more reliable in that it would still work correctly if that path ended in newline characters.






                share|improve this answer














                You can use cp -P to make a copy of a symlink:



                cp -P symlink2 symlink3


                Beware that if the target of the symlink is relative, the above would probably result in a broken link if the copy is not in the same directory as the original.



                With zsh,



                ln -s -- symlink2(:P) symlink3


                Would create symlink3 as a symlink to the full canonical (symlink-free) path symlink2, like ln -s -- "$(readlink -f symlink2)" symlink3 but more reliable in that it would still work correctly if that path ended in newline characters.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited 21 mins ago

























                answered 30 mins ago









                Stéphane Chazelas

                287k53531868




                287k53531868



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f473928%2fln-create-symlink-using-another-symlink%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Comments

                    Popular posts from this blog

                    What does second last employer means? [closed]

                    List of Gilmore Girls characters

                    Confectionery