Brighid McDonnell Brighid McDonnell - 1 year ago 96
Linux Question

Using bash to tell whether or not a drive with a given UUID is mounted

I am working on a bash script that is designed to tell whether or not a given drive is attached to the system, and if it is, to copy some data (backups that need to be taken off-site) to that drive.

What I'd like to do is identify the drive/partition by UUID, so that I can have a script that frequently scans for the presence of that drive, and when it's plugged in, copies data to it (via rsync, so I don't particularly care if that part runs more than once, because the later runs are I/O-cheap). I want this to be as simple as possible so that I can hand the drive in question to a non-technical person and say "plug it in here, then unplug it later and take it with you."

I can't seem to find any options in

that quite do this, and grepping through
doesn't tell me whether a drive is actually mounted or not.

How can I tell, given a UUID, whether or not a drive with that UUID is mounted and what its mount point is?

Answer Source

/dev/disk/by-uuid/ is a symlink farm for attached volumes. It won't tell you if the volume is mounted or not but will tell you if it's plugged in - so might address your particular use case anyway.

$ ls -l /dev/disk/by-uuid/
total 0
lrwxrwxrwx 1 root root 10 Nov 22 10:08 b928a862-6b3c-45a8-82fe-8f1db2863be3 -> ../../dm-0
lrwxrwxrwx 1 root root 10 Dec  8 14:33 e37fab85-9b00-4d9b-b25e-0970d6eaa3fa -> ../../sdg1

$ readlink /dev/disk/by-uuid/b928a862-6b3c-45a8-82fe-8f1db2863be3 

Does that help?

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download