Remove symlinks originating from specific directory

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











up vote
2
down vote

favorite
1












I have an "install.sh" that installs my personal scripts:



find /home/Steven -name '*.sh' -exec ln -s -t /usr/local/bin +


I would like to make an "uninstall.sh" that removes the symbolic links created
by "install.sh". I wrote this:



for z in /usr/local/bin/*
do
if [ -h "$z" ]
then rm "$z"
fi
done


but it removes all symlinks, not just ones where the target is under
"/home/Steven".







share|improve this question




















  • @StevePenny It occurred to me that there are a couple of minor possible ambiguities in the question related to symbolic links. The first is whether or not you're using absolute or relative paths when creating the symbolic links. The second is whether or not the targets of the symbolic links are links themselves. I suspect that neither of these will be issues in your specific case (as described in the question body), but it might be something to keep in mind.
    – igal
    Oct 28 '17 at 17:30










  • @igal you can see my command - so that answers at least one of the questions
    – Steven Penny
    Oct 28 '17 at 17:47










  • @StevePenny That's right. If you're only ever using absolute paths then that's not going to be an issue.
    – igal
    Oct 28 '17 at 17:50














up vote
2
down vote

favorite
1












I have an "install.sh" that installs my personal scripts:



find /home/Steven -name '*.sh' -exec ln -s -t /usr/local/bin +


I would like to make an "uninstall.sh" that removes the symbolic links created
by "install.sh". I wrote this:



for z in /usr/local/bin/*
do
if [ -h "$z" ]
then rm "$z"
fi
done


but it removes all symlinks, not just ones where the target is under
"/home/Steven".







share|improve this question




















  • @StevePenny It occurred to me that there are a couple of minor possible ambiguities in the question related to symbolic links. The first is whether or not you're using absolute or relative paths when creating the symbolic links. The second is whether or not the targets of the symbolic links are links themselves. I suspect that neither of these will be issues in your specific case (as described in the question body), but it might be something to keep in mind.
    – igal
    Oct 28 '17 at 17:30










  • @igal you can see my command - so that answers at least one of the questions
    – Steven Penny
    Oct 28 '17 at 17:47










  • @StevePenny That's right. If you're only ever using absolute paths then that's not going to be an issue.
    – igal
    Oct 28 '17 at 17:50












up vote
2
down vote

favorite
1









up vote
2
down vote

favorite
1






1





I have an "install.sh" that installs my personal scripts:



find /home/Steven -name '*.sh' -exec ln -s -t /usr/local/bin +


I would like to make an "uninstall.sh" that removes the symbolic links created
by "install.sh". I wrote this:



for z in /usr/local/bin/*
do
if [ -h "$z" ]
then rm "$z"
fi
done


but it removes all symlinks, not just ones where the target is under
"/home/Steven".







share|improve this question












I have an "install.sh" that installs my personal scripts:



find /home/Steven -name '*.sh' -exec ln -s -t /usr/local/bin +


I would like to make an "uninstall.sh" that removes the symbolic links created
by "install.sh". I wrote this:



for z in /usr/local/bin/*
do
if [ -h "$z" ]
then rm "$z"
fi
done


but it removes all symlinks, not just ones where the target is under
"/home/Steven".









share|improve this question











share|improve this question




share|improve this question










asked Oct 28 '17 at 16:42









Steven Penny

2,30921635




2,30921635











  • @StevePenny It occurred to me that there are a couple of minor possible ambiguities in the question related to symbolic links. The first is whether or not you're using absolute or relative paths when creating the symbolic links. The second is whether or not the targets of the symbolic links are links themselves. I suspect that neither of these will be issues in your specific case (as described in the question body), but it might be something to keep in mind.
    – igal
    Oct 28 '17 at 17:30










  • @igal you can see my command - so that answers at least one of the questions
    – Steven Penny
    Oct 28 '17 at 17:47










  • @StevePenny That's right. If you're only ever using absolute paths then that's not going to be an issue.
    – igal
    Oct 28 '17 at 17:50
















  • @StevePenny It occurred to me that there are a couple of minor possible ambiguities in the question related to symbolic links. The first is whether or not you're using absolute or relative paths when creating the symbolic links. The second is whether or not the targets of the symbolic links are links themselves. I suspect that neither of these will be issues in your specific case (as described in the question body), but it might be something to keep in mind.
    – igal
    Oct 28 '17 at 17:30










  • @igal you can see my command - so that answers at least one of the questions
    – Steven Penny
    Oct 28 '17 at 17:47










  • @StevePenny That's right. If you're only ever using absolute paths then that's not going to be an issue.
    – igal
    Oct 28 '17 at 17:50















@StevePenny It occurred to me that there are a couple of minor possible ambiguities in the question related to symbolic links. The first is whether or not you're using absolute or relative paths when creating the symbolic links. The second is whether or not the targets of the symbolic links are links themselves. I suspect that neither of these will be issues in your specific case (as described in the question body), but it might be something to keep in mind.
– igal
Oct 28 '17 at 17:30




@StevePenny It occurred to me that there are a couple of minor possible ambiguities in the question related to symbolic links. The first is whether or not you're using absolute or relative paths when creating the symbolic links. The second is whether or not the targets of the symbolic links are links themselves. I suspect that neither of these will be issues in your specific case (as described in the question body), but it might be something to keep in mind.
– igal
Oct 28 '17 at 17:30












@igal you can see my command - so that answers at least one of the questions
– Steven Penny
Oct 28 '17 at 17:47




@igal you can see my command - so that answers at least one of the questions
– Steven Penny
Oct 28 '17 at 17:47












@StevePenny That's right. If you're only ever using absolute paths then that's not going to be an issue.
– igal
Oct 28 '17 at 17:50




@StevePenny That's right. If you're only ever using absolute paths then that's not going to be an issue.
– igal
Oct 28 '17 at 17:50










3 Answers
3






active

oldest

votes

















up vote
3
down vote



accepted










If you have GNU or BSD find, this should do it:



find -lname '/home/Steven/*' -delete





share|improve this answer




















  • @TavianBarnes This seems like a better solution. But this won't work if the symbolic links use relative paths, will it?
    – igal
    Oct 28 '17 at 17:21






  • 2




    @igal Right. That shouldn't be a problem in this specific case, because the links were created with absolute paths as seen in the question. In general it would be hard to use find to answer the question "does this symbolic link point somewhere under this directory?"
    – Tavian Barnes
    Oct 28 '17 at 17:36


















up vote
1
down vote













You could modify your script and use grep and either readlink or realpath to extract the files in the desired subdirectory, e.g.:



for z in /usr/local/bin/*
do
if [ -h "$z" ] && readlink -f "$z" | grep -q '^/home/Steven'
then rm "$z"
fi
done


The readlink -f command returns the full path to the file pointed to by the symbolic link. The grep -q '^/home/Steven' command returns true if the path begins with the substring '/home/Steven' and returns false otherwise.



A word of caution: there is some ambiguity related to symbolic links that could affect the outcome here. The readlink -f command will resolve the link recursively, so the above command would fail if the file in the /home/Steven directory were itself a symbolic link point outside of this directory. If this isn't the desired behavior then you might want to use the realpath command instead.






share|improve this answer





























    up vote
    1
    down vote













    To cover for the case where the links may be relative and to check that their canonical path is within /home/Steven, with zsh:



    rm /usr/local/bin/*(@e'[[ $REPLY:A = /home/Steven/* ]]')


    Where:




    • @ glob qualifier that matches on symlinks:


    • ecode glob qualifier that matches based on the evaluation of the code (where $REPLY contains the path of the file to check)


    • $REPLY:A expands to the absolute path of $REPLY.





    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%2f401093%2fremove-symlinks-originating-from-specific-directory%23new-answer', 'question_page');

      );

      Post as a guest






























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      3
      down vote



      accepted










      If you have GNU or BSD find, this should do it:



      find -lname '/home/Steven/*' -delete





      share|improve this answer




















      • @TavianBarnes This seems like a better solution. But this won't work if the symbolic links use relative paths, will it?
        – igal
        Oct 28 '17 at 17:21






      • 2




        @igal Right. That shouldn't be a problem in this specific case, because the links were created with absolute paths as seen in the question. In general it would be hard to use find to answer the question "does this symbolic link point somewhere under this directory?"
        – Tavian Barnes
        Oct 28 '17 at 17:36















      up vote
      3
      down vote



      accepted










      If you have GNU or BSD find, this should do it:



      find -lname '/home/Steven/*' -delete





      share|improve this answer




















      • @TavianBarnes This seems like a better solution. But this won't work if the symbolic links use relative paths, will it?
        – igal
        Oct 28 '17 at 17:21






      • 2




        @igal Right. That shouldn't be a problem in this specific case, because the links were created with absolute paths as seen in the question. In general it would be hard to use find to answer the question "does this symbolic link point somewhere under this directory?"
        – Tavian Barnes
        Oct 28 '17 at 17:36













      up vote
      3
      down vote



      accepted







      up vote
      3
      down vote



      accepted






      If you have GNU or BSD find, this should do it:



      find -lname '/home/Steven/*' -delete





      share|improve this answer












      If you have GNU or BSD find, this should do it:



      find -lname '/home/Steven/*' -delete






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Oct 28 '17 at 16:52









      Tavian Barnes

      267210




      267210











      • @TavianBarnes This seems like a better solution. But this won't work if the symbolic links use relative paths, will it?
        – igal
        Oct 28 '17 at 17:21






      • 2




        @igal Right. That shouldn't be a problem in this specific case, because the links were created with absolute paths as seen in the question. In general it would be hard to use find to answer the question "does this symbolic link point somewhere under this directory?"
        – Tavian Barnes
        Oct 28 '17 at 17:36

















      • @TavianBarnes This seems like a better solution. But this won't work if the symbolic links use relative paths, will it?
        – igal
        Oct 28 '17 at 17:21






      • 2




        @igal Right. That shouldn't be a problem in this specific case, because the links were created with absolute paths as seen in the question. In general it would be hard to use find to answer the question "does this symbolic link point somewhere under this directory?"
        – Tavian Barnes
        Oct 28 '17 at 17:36
















      @TavianBarnes This seems like a better solution. But this won't work if the symbolic links use relative paths, will it?
      – igal
      Oct 28 '17 at 17:21




      @TavianBarnes This seems like a better solution. But this won't work if the symbolic links use relative paths, will it?
      – igal
      Oct 28 '17 at 17:21




      2




      2




      @igal Right. That shouldn't be a problem in this specific case, because the links were created with absolute paths as seen in the question. In general it would be hard to use find to answer the question "does this symbolic link point somewhere under this directory?"
      – Tavian Barnes
      Oct 28 '17 at 17:36





      @igal Right. That shouldn't be a problem in this specific case, because the links were created with absolute paths as seen in the question. In general it would be hard to use find to answer the question "does this symbolic link point somewhere under this directory?"
      – Tavian Barnes
      Oct 28 '17 at 17:36













      up vote
      1
      down vote













      You could modify your script and use grep and either readlink or realpath to extract the files in the desired subdirectory, e.g.:



      for z in /usr/local/bin/*
      do
      if [ -h "$z" ] && readlink -f "$z" | grep -q '^/home/Steven'
      then rm "$z"
      fi
      done


      The readlink -f command returns the full path to the file pointed to by the symbolic link. The grep -q '^/home/Steven' command returns true if the path begins with the substring '/home/Steven' and returns false otherwise.



      A word of caution: there is some ambiguity related to symbolic links that could affect the outcome here. The readlink -f command will resolve the link recursively, so the above command would fail if the file in the /home/Steven directory were itself a symbolic link point outside of this directory. If this isn't the desired behavior then you might want to use the realpath command instead.






      share|improve this answer


























        up vote
        1
        down vote













        You could modify your script and use grep and either readlink or realpath to extract the files in the desired subdirectory, e.g.:



        for z in /usr/local/bin/*
        do
        if [ -h "$z" ] && readlink -f "$z" | grep -q '^/home/Steven'
        then rm "$z"
        fi
        done


        The readlink -f command returns the full path to the file pointed to by the symbolic link. The grep -q '^/home/Steven' command returns true if the path begins with the substring '/home/Steven' and returns false otherwise.



        A word of caution: there is some ambiguity related to symbolic links that could affect the outcome here. The readlink -f command will resolve the link recursively, so the above command would fail if the file in the /home/Steven directory were itself a symbolic link point outside of this directory. If this isn't the desired behavior then you might want to use the realpath command instead.






        share|improve this answer
























          up vote
          1
          down vote










          up vote
          1
          down vote









          You could modify your script and use grep and either readlink or realpath to extract the files in the desired subdirectory, e.g.:



          for z in /usr/local/bin/*
          do
          if [ -h "$z" ] && readlink -f "$z" | grep -q '^/home/Steven'
          then rm "$z"
          fi
          done


          The readlink -f command returns the full path to the file pointed to by the symbolic link. The grep -q '^/home/Steven' command returns true if the path begins with the substring '/home/Steven' and returns false otherwise.



          A word of caution: there is some ambiguity related to symbolic links that could affect the outcome here. The readlink -f command will resolve the link recursively, so the above command would fail if the file in the /home/Steven directory were itself a symbolic link point outside of this directory. If this isn't the desired behavior then you might want to use the realpath command instead.






          share|improve this answer














          You could modify your script and use grep and either readlink or realpath to extract the files in the desired subdirectory, e.g.:



          for z in /usr/local/bin/*
          do
          if [ -h "$z" ] && readlink -f "$z" | grep -q '^/home/Steven'
          then rm "$z"
          fi
          done


          The readlink -f command returns the full path to the file pointed to by the symbolic link. The grep -q '^/home/Steven' command returns true if the path begins with the substring '/home/Steven' and returns false otherwise.



          A word of caution: there is some ambiguity related to symbolic links that could affect the outcome here. The readlink -f command will resolve the link recursively, so the above command would fail if the file in the /home/Steven directory were itself a symbolic link point outside of this directory. If this isn't the desired behavior then you might want to use the realpath command instead.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Oct 28 '17 at 17:20

























          answered Oct 28 '17 at 16:55









          igal

          4,830930




          4,830930




















              up vote
              1
              down vote













              To cover for the case where the links may be relative and to check that their canonical path is within /home/Steven, with zsh:



              rm /usr/local/bin/*(@e'[[ $REPLY:A = /home/Steven/* ]]')


              Where:




              • @ glob qualifier that matches on symlinks:


              • ecode glob qualifier that matches based on the evaluation of the code (where $REPLY contains the path of the file to check)


              • $REPLY:A expands to the absolute path of $REPLY.





              share|improve this answer
























                up vote
                1
                down vote













                To cover for the case where the links may be relative and to check that their canonical path is within /home/Steven, with zsh:



                rm /usr/local/bin/*(@e'[[ $REPLY:A = /home/Steven/* ]]')


                Where:




                • @ glob qualifier that matches on symlinks:


                • ecode glob qualifier that matches based on the evaluation of the code (where $REPLY contains the path of the file to check)


                • $REPLY:A expands to the absolute path of $REPLY.





                share|improve this answer






















                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  To cover for the case where the links may be relative and to check that their canonical path is within /home/Steven, with zsh:



                  rm /usr/local/bin/*(@e'[[ $REPLY:A = /home/Steven/* ]]')


                  Where:




                  • @ glob qualifier that matches on symlinks:


                  • ecode glob qualifier that matches based on the evaluation of the code (where $REPLY contains the path of the file to check)


                  • $REPLY:A expands to the absolute path of $REPLY.





                  share|improve this answer












                  To cover for the case where the links may be relative and to check that their canonical path is within /home/Steven, with zsh:



                  rm /usr/local/bin/*(@e'[[ $REPLY:A = /home/Steven/* ]]')


                  Where:




                  • @ glob qualifier that matches on symlinks:


                  • ecode glob qualifier that matches based on the evaluation of the code (where $REPLY contains the path of the file to check)


                  • $REPLY:A expands to the absolute path of $REPLY.






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Oct 28 '17 at 21:24









                  Stéphane Chazelas

                  283k53521855




                  283k53521855



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f401093%2fremove-symlinks-originating-from-specific-directory%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Popular posts from this blog

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

                      Displaying single band from multi-band raster using QGIS

                      How many registers does an x86_64 CPU actually have?