Safely Deleting a Directory Only If It ISN'T a Mount Point

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











up vote
1
down vote

favorite












Unfortunately I'm being forced to work with a piece of software that handles automatically mounting and unmounting a network volume very poorly; it has an annoying tendency to leave a directory where the mount point was, but can't cope properly with that being the case when mounting the volume again later (it's supposed to tidy up but often doesn't).



Naturally I'm on at the developers to get that fixed, but in the meantime I need to do something to tidy up the mount point(s) myself.



So essentially what I need to do is remove the directory, but only if it isn't currently a mount point (as I don't want to delete the volume's contents by accident).



Now, I can get the device ID of the directory and compare it to the device ID of root easily enough, but there's a possibility of a race-condition if I use such a comparison, i.e- if the volume is mounted between checking device IDs and calling rm -r /mnt/point.



Are there any alternatives? I was intrigued by the possibility of using the find command's -xdev option, but I'm not sure how I would actually provide a point of comparison, as find /mnt/point -xdev won't work as the target and its contents are the same device.



Also, using rmdir on the assumption that the leftover folder will always be empty seems unreliable, as on some systems a mount point may have a file inside; macOS for example leaves an .autodiskmounted file inside. While I could create a list of such cases and handle them, it'd be nice (and hopefully useful to others) to have a more general purpose solution for future reference, if such a thing is possible.










share|improve this question



















  • 2




    Not sure about Mac OS but on Linux, you can run "mount" on it's own to get a list of mounted file systems. You can then use something like awk to parse the directories and compare them to the one in question.
    – Raman Sailopal
    Sep 26 '17 at 10:34










  • What's your OS platform? (Please tag it.) We don't have GNU utilities on Mac systems or embedded Linux-based systems that would otherwise be expected as standard on full Linux-based systems.
    – roaima
    Sep 27 '17 at 0:48















up vote
1
down vote

favorite












Unfortunately I'm being forced to work with a piece of software that handles automatically mounting and unmounting a network volume very poorly; it has an annoying tendency to leave a directory where the mount point was, but can't cope properly with that being the case when mounting the volume again later (it's supposed to tidy up but often doesn't).



Naturally I'm on at the developers to get that fixed, but in the meantime I need to do something to tidy up the mount point(s) myself.



So essentially what I need to do is remove the directory, but only if it isn't currently a mount point (as I don't want to delete the volume's contents by accident).



Now, I can get the device ID of the directory and compare it to the device ID of root easily enough, but there's a possibility of a race-condition if I use such a comparison, i.e- if the volume is mounted between checking device IDs and calling rm -r /mnt/point.



Are there any alternatives? I was intrigued by the possibility of using the find command's -xdev option, but I'm not sure how I would actually provide a point of comparison, as find /mnt/point -xdev won't work as the target and its contents are the same device.



Also, using rmdir on the assumption that the leftover folder will always be empty seems unreliable, as on some systems a mount point may have a file inside; macOS for example leaves an .autodiskmounted file inside. While I could create a list of such cases and handle them, it'd be nice (and hopefully useful to others) to have a more general purpose solution for future reference, if such a thing is possible.










share|improve this question



















  • 2




    Not sure about Mac OS but on Linux, you can run "mount" on it's own to get a list of mounted file systems. You can then use something like awk to parse the directories and compare them to the one in question.
    – Raman Sailopal
    Sep 26 '17 at 10:34










  • What's your OS platform? (Please tag it.) We don't have GNU utilities on Mac systems or embedded Linux-based systems that would otherwise be expected as standard on full Linux-based systems.
    – roaima
    Sep 27 '17 at 0:48













up vote
1
down vote

favorite









up vote
1
down vote

favorite











Unfortunately I'm being forced to work with a piece of software that handles automatically mounting and unmounting a network volume very poorly; it has an annoying tendency to leave a directory where the mount point was, but can't cope properly with that being the case when mounting the volume again later (it's supposed to tidy up but often doesn't).



Naturally I'm on at the developers to get that fixed, but in the meantime I need to do something to tidy up the mount point(s) myself.



So essentially what I need to do is remove the directory, but only if it isn't currently a mount point (as I don't want to delete the volume's contents by accident).



Now, I can get the device ID of the directory and compare it to the device ID of root easily enough, but there's a possibility of a race-condition if I use such a comparison, i.e- if the volume is mounted between checking device IDs and calling rm -r /mnt/point.



Are there any alternatives? I was intrigued by the possibility of using the find command's -xdev option, but I'm not sure how I would actually provide a point of comparison, as find /mnt/point -xdev won't work as the target and its contents are the same device.



Also, using rmdir on the assumption that the leftover folder will always be empty seems unreliable, as on some systems a mount point may have a file inside; macOS for example leaves an .autodiskmounted file inside. While I could create a list of such cases and handle them, it'd be nice (and hopefully useful to others) to have a more general purpose solution for future reference, if such a thing is possible.










share|improve this question















Unfortunately I'm being forced to work with a piece of software that handles automatically mounting and unmounting a network volume very poorly; it has an annoying tendency to leave a directory where the mount point was, but can't cope properly with that being the case when mounting the volume again later (it's supposed to tidy up but often doesn't).



Naturally I'm on at the developers to get that fixed, but in the meantime I need to do something to tidy up the mount point(s) myself.



So essentially what I need to do is remove the directory, but only if it isn't currently a mount point (as I don't want to delete the volume's contents by accident).



Now, I can get the device ID of the directory and compare it to the device ID of root easily enough, but there's a possibility of a race-condition if I use such a comparison, i.e- if the volume is mounted between checking device IDs and calling rm -r /mnt/point.



Are there any alternatives? I was intrigued by the possibility of using the find command's -xdev option, but I'm not sure how I would actually provide a point of comparison, as find /mnt/point -xdev won't work as the target and its contents are the same device.



Also, using rmdir on the assumption that the leftover folder will always be empty seems unreliable, as on some systems a mount point may have a file inside; macOS for example leaves an .autodiskmounted file inside. While I could create a list of such cases and handle them, it'd be nice (and hopefully useful to others) to have a more general purpose solution for future reference, if such a thing is possible.







filesystems mount directory rm unmounting






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 26 '17 at 23:45









Jeff Schaller

32.3k849110




32.3k849110










asked Sep 26 '17 at 10:16









Haravikk

2741315




2741315







  • 2




    Not sure about Mac OS but on Linux, you can run "mount" on it's own to get a list of mounted file systems. You can then use something like awk to parse the directories and compare them to the one in question.
    – Raman Sailopal
    Sep 26 '17 at 10:34










  • What's your OS platform? (Please tag it.) We don't have GNU utilities on Mac systems or embedded Linux-based systems that would otherwise be expected as standard on full Linux-based systems.
    – roaima
    Sep 27 '17 at 0:48













  • 2




    Not sure about Mac OS but on Linux, you can run "mount" on it's own to get a list of mounted file systems. You can then use something like awk to parse the directories and compare them to the one in question.
    – Raman Sailopal
    Sep 26 '17 at 10:34










  • What's your OS platform? (Please tag it.) We don't have GNU utilities on Mac systems or embedded Linux-based systems that would otherwise be expected as standard on full Linux-based systems.
    – roaima
    Sep 27 '17 at 0:48








2




2




Not sure about Mac OS but on Linux, you can run "mount" on it's own to get a list of mounted file systems. You can then use something like awk to parse the directories and compare them to the one in question.
– Raman Sailopal
Sep 26 '17 at 10:34




Not sure about Mac OS but on Linux, you can run "mount" on it's own to get a list of mounted file systems. You can then use something like awk to parse the directories and compare them to the one in question.
– Raman Sailopal
Sep 26 '17 at 10:34












What's your OS platform? (Please tag it.) We don't have GNU utilities on Mac systems or embedded Linux-based systems that would otherwise be expected as standard on full Linux-based systems.
– roaima
Sep 27 '17 at 0:48





What's your OS platform? (Please tag it.) We don't have GNU utilities on Mac systems or embedded Linux-based systems that would otherwise be expected as standard on full Linux-based systems.
– roaima
Sep 27 '17 at 0:48











3 Answers
3






active

oldest

votes

















up vote
3
down vote



accepted










If the directory is a mount point, it will be busy and you shouldn't be able to rename it.



$ sudo mv /mnt /mnt.old
mv: cannot move '/mnt' to '/mnt.old': Device or resource busy


If it's just a regular directory, you should be able to rename it.



$ sudo mv /mnt /mnt.old


If the move succeeds, re-create the mount directory and delete the renamed directory. Optionally, you can validate the renamed directory is part of the filesystem you expect before removal.






share|improve this answer




















  • Looks like we may have a winner! Super compatible and like all good solutions it's so simple I'm now kicking myself for not thinking of it on my own! It looks like the "can't move a mountpoint" condition applies to all unix-y systems I have at the moment (Macs, a NAS, couple of servers). Unless there's an exception to this somewhere it seems like a good general purpose solution.
    – Haravikk
    Sep 27 '17 at 13:01

















up vote
1
down vote













If you are using Linux



You can test if a directory is a mount point using mountpoint.



To avoid mount point appearing/disappearing between test and rm -r, you need to run the script in a separate mount namespace with private subtree (mounts do not propagate from/to new namespace). This can be accomplished with unshare.



unshare -m --propagation private -- "<delete script>"


Everything in same script:



#!/bin/sh

unshare -m --propagation private -- sh -e <<EOF

if ! mountpoint -q "<path>"; then
rm -r "<path>"
fi

EOF





share|improve this answer





























    up vote
    0
    down vote













    You cannot rmdir a directory that's not empty or one that's a mountpoint. So this will satisfy your requirement:



    rm -f mountpoint/.autodiskmounted # MacOS cookie
    rmdir mountpoint 2>/dev/null


    If you're running in a script with exit on error set, you can use rmdir ... || true. (Note that --ignore-fail-on-non-empty is of little use, because we will get an uncaught error when attempting to remove a mountpoint.)



    I should point out that I don't have access to a Mac system so I cannot test whether the .autodiskmounted file is present when the filesystem really is mounted, or whether it's a placeholder for when the filesystem is unmounted.






    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%2f394501%2fsafely-deleting-a-directory-only-if-it-isnt-a-mount-point%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 the directory is a mount point, it will be busy and you shouldn't be able to rename it.



      $ sudo mv /mnt /mnt.old
      mv: cannot move '/mnt' to '/mnt.old': Device or resource busy


      If it's just a regular directory, you should be able to rename it.



      $ sudo mv /mnt /mnt.old


      If the move succeeds, re-create the mount directory and delete the renamed directory. Optionally, you can validate the renamed directory is part of the filesystem you expect before removal.






      share|improve this answer




















      • Looks like we may have a winner! Super compatible and like all good solutions it's so simple I'm now kicking myself for not thinking of it on my own! It looks like the "can't move a mountpoint" condition applies to all unix-y systems I have at the moment (Macs, a NAS, couple of servers). Unless there's an exception to this somewhere it seems like a good general purpose solution.
        – Haravikk
        Sep 27 '17 at 13:01














      up vote
      3
      down vote



      accepted










      If the directory is a mount point, it will be busy and you shouldn't be able to rename it.



      $ sudo mv /mnt /mnt.old
      mv: cannot move '/mnt' to '/mnt.old': Device or resource busy


      If it's just a regular directory, you should be able to rename it.



      $ sudo mv /mnt /mnt.old


      If the move succeeds, re-create the mount directory and delete the renamed directory. Optionally, you can validate the renamed directory is part of the filesystem you expect before removal.






      share|improve this answer




















      • Looks like we may have a winner! Super compatible and like all good solutions it's so simple I'm now kicking myself for not thinking of it on my own! It looks like the "can't move a mountpoint" condition applies to all unix-y systems I have at the moment (Macs, a NAS, couple of servers). Unless there's an exception to this somewhere it seems like a good general purpose solution.
        – Haravikk
        Sep 27 '17 at 13:01












      up vote
      3
      down vote



      accepted







      up vote
      3
      down vote



      accepted






      If the directory is a mount point, it will be busy and you shouldn't be able to rename it.



      $ sudo mv /mnt /mnt.old
      mv: cannot move '/mnt' to '/mnt.old': Device or resource busy


      If it's just a regular directory, you should be able to rename it.



      $ sudo mv /mnt /mnt.old


      If the move succeeds, re-create the mount directory and delete the renamed directory. Optionally, you can validate the renamed directory is part of the filesystem you expect before removal.






      share|improve this answer












      If the directory is a mount point, it will be busy and you shouldn't be able to rename it.



      $ sudo mv /mnt /mnt.old
      mv: cannot move '/mnt' to '/mnt.old': Device or resource busy


      If it's just a regular directory, you should be able to rename it.



      $ sudo mv /mnt /mnt.old


      If the move succeeds, re-create the mount directory and delete the renamed directory. Optionally, you can validate the renamed directory is part of the filesystem you expect before removal.







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Sep 27 '17 at 0:00









      BowlOfRed

      2,335612




      2,335612











      • Looks like we may have a winner! Super compatible and like all good solutions it's so simple I'm now kicking myself for not thinking of it on my own! It looks like the "can't move a mountpoint" condition applies to all unix-y systems I have at the moment (Macs, a NAS, couple of servers). Unless there's an exception to this somewhere it seems like a good general purpose solution.
        – Haravikk
        Sep 27 '17 at 13:01
















      • Looks like we may have a winner! Super compatible and like all good solutions it's so simple I'm now kicking myself for not thinking of it on my own! It looks like the "can't move a mountpoint" condition applies to all unix-y systems I have at the moment (Macs, a NAS, couple of servers). Unless there's an exception to this somewhere it seems like a good general purpose solution.
        – Haravikk
        Sep 27 '17 at 13:01















      Looks like we may have a winner! Super compatible and like all good solutions it's so simple I'm now kicking myself for not thinking of it on my own! It looks like the "can't move a mountpoint" condition applies to all unix-y systems I have at the moment (Macs, a NAS, couple of servers). Unless there's an exception to this somewhere it seems like a good general purpose solution.
      – Haravikk
      Sep 27 '17 at 13:01




      Looks like we may have a winner! Super compatible and like all good solutions it's so simple I'm now kicking myself for not thinking of it on my own! It looks like the "can't move a mountpoint" condition applies to all unix-y systems I have at the moment (Macs, a NAS, couple of servers). Unless there's an exception to this somewhere it seems like a good general purpose solution.
      – Haravikk
      Sep 27 '17 at 13:01












      up vote
      1
      down vote













      If you are using Linux



      You can test if a directory is a mount point using mountpoint.



      To avoid mount point appearing/disappearing between test and rm -r, you need to run the script in a separate mount namespace with private subtree (mounts do not propagate from/to new namespace). This can be accomplished with unshare.



      unshare -m --propagation private -- "<delete script>"


      Everything in same script:



      #!/bin/sh

      unshare -m --propagation private -- sh -e <<EOF

      if ! mountpoint -q "<path>"; then
      rm -r "<path>"
      fi

      EOF





      share|improve this answer


























        up vote
        1
        down vote













        If you are using Linux



        You can test if a directory is a mount point using mountpoint.



        To avoid mount point appearing/disappearing between test and rm -r, you need to run the script in a separate mount namespace with private subtree (mounts do not propagate from/to new namespace). This can be accomplished with unshare.



        unshare -m --propagation private -- "<delete script>"


        Everything in same script:



        #!/bin/sh

        unshare -m --propagation private -- sh -e <<EOF

        if ! mountpoint -q "<path>"; then
        rm -r "<path>"
        fi

        EOF





        share|improve this answer
























          up vote
          1
          down vote










          up vote
          1
          down vote









          If you are using Linux



          You can test if a directory is a mount point using mountpoint.



          To avoid mount point appearing/disappearing between test and rm -r, you need to run the script in a separate mount namespace with private subtree (mounts do not propagate from/to new namespace). This can be accomplished with unshare.



          unshare -m --propagation private -- "<delete script>"


          Everything in same script:



          #!/bin/sh

          unshare -m --propagation private -- sh -e <<EOF

          if ! mountpoint -q "<path>"; then
          rm -r "<path>"
          fi

          EOF





          share|improve this answer














          If you are using Linux



          You can test if a directory is a mount point using mountpoint.



          To avoid mount point appearing/disappearing between test and rm -r, you need to run the script in a separate mount namespace with private subtree (mounts do not propagate from/to new namespace). This can be accomplished with unshare.



          unshare -m --propagation private -- "<delete script>"


          Everything in same script:



          #!/bin/sh

          unshare -m --propagation private -- sh -e <<EOF

          if ! mountpoint -q "<path>"; then
          rm -r "<path>"
          fi

          EOF






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Sep 26 '17 at 12:42

























          answered Sep 26 '17 at 10:23









          sebasth

          6,14321643




          6,14321643




















              up vote
              0
              down vote













              You cannot rmdir a directory that's not empty or one that's a mountpoint. So this will satisfy your requirement:



              rm -f mountpoint/.autodiskmounted # MacOS cookie
              rmdir mountpoint 2>/dev/null


              If you're running in a script with exit on error set, you can use rmdir ... || true. (Note that --ignore-fail-on-non-empty is of little use, because we will get an uncaught error when attempting to remove a mountpoint.)



              I should point out that I don't have access to a Mac system so I cannot test whether the .autodiskmounted file is present when the filesystem really is mounted, or whether it's a placeholder for when the filesystem is unmounted.






              share|improve this answer


























                up vote
                0
                down vote













                You cannot rmdir a directory that's not empty or one that's a mountpoint. So this will satisfy your requirement:



                rm -f mountpoint/.autodiskmounted # MacOS cookie
                rmdir mountpoint 2>/dev/null


                If you're running in a script with exit on error set, you can use rmdir ... || true. (Note that --ignore-fail-on-non-empty is of little use, because we will get an uncaught error when attempting to remove a mountpoint.)



                I should point out that I don't have access to a Mac system so I cannot test whether the .autodiskmounted file is present when the filesystem really is mounted, or whether it's a placeholder for when the filesystem is unmounted.






                share|improve this answer
























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  You cannot rmdir a directory that's not empty or one that's a mountpoint. So this will satisfy your requirement:



                  rm -f mountpoint/.autodiskmounted # MacOS cookie
                  rmdir mountpoint 2>/dev/null


                  If you're running in a script with exit on error set, you can use rmdir ... || true. (Note that --ignore-fail-on-non-empty is of little use, because we will get an uncaught error when attempting to remove a mountpoint.)



                  I should point out that I don't have access to a Mac system so I cannot test whether the .autodiskmounted file is present when the filesystem really is mounted, or whether it's a placeholder for when the filesystem is unmounted.






                  share|improve this answer














                  You cannot rmdir a directory that's not empty or one that's a mountpoint. So this will satisfy your requirement:



                  rm -f mountpoint/.autodiskmounted # MacOS cookie
                  rmdir mountpoint 2>/dev/null


                  If you're running in a script with exit on error set, you can use rmdir ... || true. (Note that --ignore-fail-on-non-empty is of little use, because we will get an uncaught error when attempting to remove a mountpoint.)



                  I should point out that I don't have access to a Mac system so I cannot test whether the .autodiskmounted file is present when the filesystem really is mounted, or whether it's a placeholder for when the filesystem is unmounted.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Sep 26 '17 at 16:32

























                  answered Sep 26 '17 at 15:08









                  roaima

                  40.2k547110




                  40.2k547110



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f394501%2fsafely-deleting-a-directory-only-if-it-isnt-a-mount-point%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?

                      Bahrain

                      Postfix configuration issue with fips on centos 7; mailgun relay