I'm trying to write script to back my data up. I don't know much about it, so I might take a wrong approach, but what I'm trying to do is use rsync -avh several times for each directory I want to back up. However, I set destination as hard-coded value - ~/sbackup/. Now I want my script to check before running if the folder is mounted, and if not - try mount it. Therefore, script logic would look like this:

1)See if my USB disc is mounted under ~/sbackup/. (The disc - it would be good if there was a way to prevent other USB device from mounting there).

2)If it isn't try to mount it there. If you can't, write an error message and exit.

3) If it is mounted, run rest of the script.

Now I can write error messages and such, but I'm not sure about 1st step and 1st part of 2nd step.

  • How is the disk mounted? How have you told your system to mount it on ~/sbackup?
    – terdon
    Dec 5, 2015 at 14:16
  • @terdon Well, I usually mount my disc using mount, and supply path manually, but I'm open to suggestions. Dec 5, 2015 at 14:45
  • Manually? What's your OS? Most modern Linux distros should mount automatically via udev. If you mount it normally though, your question is even simpler. Hang on, I'll post an answer.
    – terdon
    Dec 5, 2015 at 14:46
  • @terdon It's Gentoo. Dec 5, 2015 at 15:08
  • Pretty much the same question here: Detect if a (NTFS) drive is mounted or not. If not then mount it Dec 5, 2015 at 18:07

2 Answers 2


if you have a mountpoint command:

mountpoint -q /local/mount/point || mount ...

When run with no arguments, mount returns a list of mounted partitions and some information about them, including their mountpoints. Therefore, you can just grep for the mountpoint in the output of mount and act accordingly:

if ! mount | grep -q sbackup; then
    mount /dev/sdb1 /home/matthew/sbackup || echo "Couldn't mount" && exit

Change /dev/sdb to whatever your USB's partition is and matthew to your username. Run the script with sudo to be able to mount.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.