Skip to main content
7 of 8
added 13 characters in body
sebasth
  • 15.8k
  • 6
  • 53
  • 71

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 name space). 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
sebasth
  • 15.8k
  • 6
  • 53
  • 71