Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have a script that backup up files to the external drive each night. The problem is that Nautilus auto mounts the external drive and when the script runs it fails to mount the external drive.

The mount point that the script creates in /mnt/mirror and the mount point the Nautilus creates is /mnt/mirror_

How do I detect if an external drive is already mounted?

share|improve this question
add comment

1 Answer 1

up vote 4 down vote accepted

Nautilus mounts it to /mnt/mirror_ because /mnt/mirror seems to be already in use.

  1. You could edit /etc/fstab and add a line such that your external disk is mounted to /mnt/mirror every time.

    UUID="12345-67890" /mnt/mirror vfat defaults,auto,users,rw 0 0

    Change vfat if your use another filesystem.
    You find your UUID and filesystem type with the command sudo blkid.
    After plugging in, either cick on the entry in nautilus or mount using the command mount /mnt/mirror.
  2. Else do the following:
    Use the mount command which returns the current mounts of your system.

    Shows all current mounts:
    mount

    Filters out the mounts containing the word mirror:
    mount | grep "mirror"

    To use it in a script you can do the following:
    mounted=$(mount | grep "mirror")
    [ ! "$mounted" = "" ] && { echo -e "mirror mounts:\n$mounted"; }

    There will be no output if no mount is found containing "mirror".

Notes:

  • You can also have your disk mounted to several mount points at the same time, if you are using filetypes other than ntfs. You could mount it to /mnt/mirror regardless of /mnt/mirror_.
    When using ntfs, make sure to first unmount the partition with sudo umount /mnt/mirror_.
  • replace "mirror" with something else you know about the device, for example "sdb".
  • check if any partition of your last plugged in removable disk is mounted:
    last_dev=$(dmesg | grep "removable" | sed -e 's/.*\[//g' -e 's/\].*//' | tail -n1)
    mounted=$(mount | grep $last_dev)
    echo -e "last removable disk mounts\n:$mounted"
share|improve this answer
    
When I try and mount the disk to several mount points a get the following error: "Mount is denied because the NTFS volume is already exclusively opened. The volume may be already mounted, or another software may use it which could be identified for example by the help of the 'fuser' command. " –  Hendre Oct 14 '12 at 12:43
    
You're right, but this seems to be specific to ntfs, multiple mountpoints are possible with at least ext and fat filetypes. When using ntfs unmount first then mount to a mountpoint of your choice. I will update my answer to include this information. –  rosch Oct 14 '12 at 14:55
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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