How to check whether a partition is mounted by UUID?
lsblk
might help. It can print just the UUID and mount point, so, given the UUID, just see if the mount point is not empty:
uuid=foo
lsblk -o UUID,MOUNTPOINT | awk -v u="$uuid" '$1 == u {print $2}'
So:
uuid=foo
mountpoint=$(lsblk -o UUID,MOUNTPOINT | awk -v u="$uuid" '$1 == u {print $2}')
if [[ -n $mountpoint ]]
then
echo mounted
else
echo not mounted
fi
Since lbslk
can act on specific devices, you can also do:
mountpoint=$(lsblk -o MOUNTPOINT "/dev/disk/by-uuid/$uuid" | awk 'NR==2')
With the first method, there won't be an error if that UUID isn't from a currently connected disk. With the second method, lsblk
will error out if /dev/disk/by-uuid/$uuid
doesn't exist.
lsblk -o UUID,SIZE,MOUNTPOINT
If you only want one line with your UUID and mountpoint ($UUID represents your UUID):
lsblk -o UUID,MOUNTPOINT|grep "$UUID"
The mount point will be empty if it is unmounted. Try lsblk -h
for more options.
Use awk
to print the result. if NF
(Number of fields) is more than one it means that it has a mount point:
lsblk -o UUID,MOUNTPOINT|grep "$UUID"|awk '{if (NF>1) print $1" is mounted"; else print $1" is unmounted";}'
If you want the details as from mount
for uuid in /dev/disk/by-uuid/*; do if [[ "$uuid" =~ .*your-UUID-here.* ]]; then echo $(mount | grep "$(readlink -e "$uuid")") ; fi; done
replace your-UUID-here
with your UUID
more readably:
for uuid in /dev/disk/by-uuid/*; do
if [[ "$uuid" =~ .*your-UUID-here.* ]]
then echo $(mount | grep "$(readlink -e "$uuid")")
fi
done
output example:
/dev/mmcblk1p2 on / type ext4 (rw,relatime,errors=remount-ro,data=ordered)
You can just make it check that the string is not null and echo "mounted":
for uuid in /dev/disk/by-uuid/*; do if [[ "$uuid" =~ .*your-UUID-here.* ]]; then if [[ $(mount | grep "$(readlink -e "$uuid")") ]]; then echo "mounted"; fi; fi; done
but others gave better ways to do that :)