How to find all symbolic links pointing to any file/directory inside a given directory

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











up vote
10
down vote

favorite
3












On this question or on this one (for example) you will get solutions on how to look for symlinks pointing to a given directory (let's call it /dir1), while I am interested to symbolic links possibly pointing to any file/folder inside /dir1.



I want to delete such directory but I am not sure that I am safe to do so, as on an other directory (let's call it /dir2), I may have symlinks pointing to inner parts of /dir1.



Further, I may have created these symlinks using absolute or relative paths.
My only help is that I know the symlinks I want to check are on a mounted filesystem, on /dir2.










share|improve this question



























    up vote
    10
    down vote

    favorite
    3












    On this question or on this one (for example) you will get solutions on how to look for symlinks pointing to a given directory (let's call it /dir1), while I am interested to symbolic links possibly pointing to any file/folder inside /dir1.



    I want to delete such directory but I am not sure that I am safe to do so, as on an other directory (let's call it /dir2), I may have symlinks pointing to inner parts of /dir1.



    Further, I may have created these symlinks using absolute or relative paths.
    My only help is that I know the symlinks I want to check are on a mounted filesystem, on /dir2.










    share|improve this question

























      up vote
      10
      down vote

      favorite
      3









      up vote
      10
      down vote

      favorite
      3






      3





      On this question or on this one (for example) you will get solutions on how to look for symlinks pointing to a given directory (let's call it /dir1), while I am interested to symbolic links possibly pointing to any file/folder inside /dir1.



      I want to delete such directory but I am not sure that I am safe to do so, as on an other directory (let's call it /dir2), I may have symlinks pointing to inner parts of /dir1.



      Further, I may have created these symlinks using absolute or relative paths.
      My only help is that I know the symlinks I want to check are on a mounted filesystem, on /dir2.










      share|improve this question















      On this question or on this one (for example) you will get solutions on how to look for symlinks pointing to a given directory (let's call it /dir1), while I am interested to symbolic links possibly pointing to any file/folder inside /dir1.



      I want to delete such directory but I am not sure that I am safe to do so, as on an other directory (let's call it /dir2), I may have symlinks pointing to inner parts of /dir1.



      Further, I may have created these symlinks using absolute or relative paths.
      My only help is that I know the symlinks I want to check are on a mounted filesystem, on /dir2.







      symlink






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited May 23 '17 at 11:33









      Community

      1




      1










      asked Aug 6 '16 at 9:36









      Antonello

      2801313




      2801313




















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          11
          down vote



          accepted










          You can find all the symbolic links using:



          find / -type l 


          you might want to run this as root in order to get to every place on the disc.



          You can expand these using readlink -f to get the full path of the link and you should be able to grep the output against the target directory that you are considering for deletion:



          find / -type l -exec readlink -f + | grep -F /dir2


          Using find / -type l -printf '%ln' doesn't work as you get relative links like ../tmp/xyz which might be pointing to your target dir, but are not matched because they are not fully expanded.






          share|improve this answer


















          • 1




            In case of subtree it can be useful to follow symlinks: find -L /subtree -xtype l -exec readlink -f +
            – ruvim
            Nov 30 '17 at 11:52

















          up vote
          0
          down vote













          In my case, the accepted answer wasn't useful (because it didn't output the link source). Here is what worked for me.



          I worked around it using two -exec clauses:



          find /home/ -type l -exec readlink -nf ';' -exec echo " -> " ';' | grep "/dir2"





          share|improve this answer










          New contributor




          Dorian Marchal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.
























            up vote
            0
            down vote













            With zsh:



            printf '%sn' /dir2/**/*(D@e'([[ $REPLY:P = /dir1(/*|) ]])')





            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: 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%2f301717%2fhow-to-find-all-symbolic-links-pointing-to-any-file-directory-inside-a-given-dir%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              11
              down vote



              accepted










              You can find all the symbolic links using:



              find / -type l 


              you might want to run this as root in order to get to every place on the disc.



              You can expand these using readlink -f to get the full path of the link and you should be able to grep the output against the target directory that you are considering for deletion:



              find / -type l -exec readlink -f + | grep -F /dir2


              Using find / -type l -printf '%ln' doesn't work as you get relative links like ../tmp/xyz which might be pointing to your target dir, but are not matched because they are not fully expanded.






              share|improve this answer


















              • 1




                In case of subtree it can be useful to follow symlinks: find -L /subtree -xtype l -exec readlink -f +
                – ruvim
                Nov 30 '17 at 11:52














              up vote
              11
              down vote



              accepted










              You can find all the symbolic links using:



              find / -type l 


              you might want to run this as root in order to get to every place on the disc.



              You can expand these using readlink -f to get the full path of the link and you should be able to grep the output against the target directory that you are considering for deletion:



              find / -type l -exec readlink -f + | grep -F /dir2


              Using find / -type l -printf '%ln' doesn't work as you get relative links like ../tmp/xyz which might be pointing to your target dir, but are not matched because they are not fully expanded.






              share|improve this answer


















              • 1




                In case of subtree it can be useful to follow symlinks: find -L /subtree -xtype l -exec readlink -f +
                – ruvim
                Nov 30 '17 at 11:52












              up vote
              11
              down vote



              accepted







              up vote
              11
              down vote



              accepted






              You can find all the symbolic links using:



              find / -type l 


              you might want to run this as root in order to get to every place on the disc.



              You can expand these using readlink -f to get the full path of the link and you should be able to grep the output against the target directory that you are considering for deletion:



              find / -type l -exec readlink -f + | grep -F /dir2


              Using find / -type l -printf '%ln' doesn't work as you get relative links like ../tmp/xyz which might be pointing to your target dir, but are not matched because they are not fully expanded.






              share|improve this answer














              You can find all the symbolic links using:



              find / -type l 


              you might want to run this as root in order to get to every place on the disc.



              You can expand these using readlink -f to get the full path of the link and you should be able to grep the output against the target directory that you are considering for deletion:



              find / -type l -exec readlink -f + | grep -F /dir2


              Using find / -type l -printf '%ln' doesn't work as you get relative links like ../tmp/xyz which might be pointing to your target dir, but are not matched because they are not fully expanded.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Aug 6 '16 at 10:00

























              answered Aug 6 '16 at 9:53









              Anthon

              59.8k17102163




              59.8k17102163







              • 1




                In case of subtree it can be useful to follow symlinks: find -L /subtree -xtype l -exec readlink -f +
                – ruvim
                Nov 30 '17 at 11:52












              • 1




                In case of subtree it can be useful to follow symlinks: find -L /subtree -xtype l -exec readlink -f +
                – ruvim
                Nov 30 '17 at 11:52







              1




              1




              In case of subtree it can be useful to follow symlinks: find -L /subtree -xtype l -exec readlink -f +
              – ruvim
              Nov 30 '17 at 11:52




              In case of subtree it can be useful to follow symlinks: find -L /subtree -xtype l -exec readlink -f +
              – ruvim
              Nov 30 '17 at 11:52












              up vote
              0
              down vote













              In my case, the accepted answer wasn't useful (because it didn't output the link source). Here is what worked for me.



              I worked around it using two -exec clauses:



              find /home/ -type l -exec readlink -nf ';' -exec echo " -> " ';' | grep "/dir2"





              share|improve this answer










              New contributor




              Dorian Marchal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.





















                up vote
                0
                down vote













                In my case, the accepted answer wasn't useful (because it didn't output the link source). Here is what worked for me.



                I worked around it using two -exec clauses:



                find /home/ -type l -exec readlink -nf ';' -exec echo " -> " ';' | grep "/dir2"





                share|improve this answer










                New contributor




                Dorian Marchal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.



















                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  In my case, the accepted answer wasn't useful (because it didn't output the link source). Here is what worked for me.



                  I worked around it using two -exec clauses:



                  find /home/ -type l -exec readlink -nf ';' -exec echo " -> " ';' | grep "/dir2"





                  share|improve this answer










                  New contributor




                  Dorian Marchal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  In my case, the accepted answer wasn't useful (because it didn't output the link source). Here is what worked for me.



                  I worked around it using two -exec clauses:



                  find /home/ -type l -exec readlink -nf ';' -exec echo " -> " ';' | grep "/dir2"






                  share|improve this answer










                  New contributor




                  Dorian Marchal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  share|improve this answer



                  share|improve this answer








                  edited Nov 20 at 15:30









                  ctrl-alt-delor

                  10.2k41955




                  10.2k41955






                  New contributor




                  Dorian Marchal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  answered Nov 20 at 14:41









                  Dorian Marchal

                  1




                  1




                  New contributor




                  Dorian Marchal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.





                  New contributor





                  Dorian Marchal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.






                  Dorian Marchal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.




















                      up vote
                      0
                      down vote













                      With zsh:



                      printf '%sn' /dir2/**/*(D@e'([[ $REPLY:P = /dir1(/*|) ]])')





                      share|improve this answer
























                        up vote
                        0
                        down vote













                        With zsh:



                        printf '%sn' /dir2/**/*(D@e'([[ $REPLY:P = /dir1(/*|) ]])')





                        share|improve this answer






















                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          With zsh:



                          printf '%sn' /dir2/**/*(D@e'([[ $REPLY:P = /dir1(/*|) ]])')





                          share|improve this answer












                          With zsh:



                          printf '%sn' /dir2/**/*(D@e'([[ $REPLY:P = /dir1(/*|) ]])')






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 20 at 15:32









                          Stéphane Chazelas

                          294k54555898




                          294k54555898



























                               

                              draft saved


                              draft discarded















































                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f301717%2fhow-to-find-all-symbolic-links-pointing-to-any-file-directory-inside-a-given-dir%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

                              How to check contact read email or not when send email to Individual?

                              Bahrain

                              Postfix configuration issue with fips on centos 7; mailgun relay