If I have the UUID of a drive partition, how would I go about finding out whether it is mounted or not, using the command line?
9 Answers
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 lsblk
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
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";}'
-
3yes, this ! exactly this ! also can be improved with grep to filter out specific UUID, maybe even parse with awk Nov 14, 2016 at 14:46
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 :)
Ubuntu uses UDisks2
daemon, which polls whole lot of information about file systems, and we can use udisksctl
and filter its output to find a quick and dirty way to find the info we need:
udisksctl dump | python -c 'import sys;lines = [l.strip() for l in sys.stdin if " UUID" in l or " MountPoints:" in l];print "\n".join(lines)'
What you see above, basically will print UUIDs of filesystems and their mountpoints. By visual inspection you now can figure out, which UUID is mounted, and which is not.
$ udisksctl dump | python -c 'import sys;lines = [l.strip() for l in sys.stdin
> if " UUID" in l or " MountPoints:" in l];print "\n".join(lines)'
MountPoints: /
UUID: 000b5321-01
MountPoints:
UUID: bbf23a81-808e-11e4-a445-201a06d7b0e9
MountPoints:
UUID: bbf23a87-808e-11e4-a445-201a06d7b0e9
MountPoints:
UUID: bbf23a89-808e-11e4-a445-201a06d7b0e9
MountPoints:
UUID: bbf23a8f-808e-11e4-a445-201a06d7b0e9
MountPoints:
UUID: 02fe8de2-a408-11e4-8eeb-f0761c8c6df7
MountPoints: /mnt/HDD
UUID: 53827413-0b7e-4ae6-ae09-1daea529d6a1
My solution
- uses only a shell and
procfs
, so no weird command output formatting issues, - doesn't (re-)scan device UUIDs but uses the kernel cache,
- therefore doesn't require super-user privileges,
- doesn't rely on canonicalised path names but uses (device) file identity.
my_uuid=...
while IFS=' ' read -r dev mnt remainder; do
case "$dev" in
/dev/*)
if [ "$dev" -ef "/dev/disk/by-uuid/$my_uuid" ]; then
echo "$my_uuid ($dev) mounted at $mnt"
break
fi;;
esac
done < /proc/mounts
Building on the excellent answers I received for this question, I realised that it's much easier to work with device names. To get the device name from UUID:
disk_uuid=foo
disk_devname=$(blkid -U "$disk_uuid")
Which means I can quickly deduce if it's mounted by grepping the output of df
:
df | grep -q "$disk_devname" && echo "Disk mounted" || echo "Disk not mounted"
Or by using the code in Muru's answer, which has the added bonus of telling me where the drive is mounted:
mountpoint=$(lsblk -no MOUNTPOINT "$disk_devname")
if [[ -n "$mountpoint" ]]; then
echo "Disk is mounted at $mountpoint"
else
echo "Disk not mounted"
fi
You can use the df
command to see the mounted file systems and their mount point. Here is an example from my machine ... the partition sdb3
, on which I have another system installed, is not mounted, so not listed (partitions are re-labeled, that's why names are shown instead of UUIDs) :
df
Filesystem 1K-blocks Used Available Use% Mounted on
udev 8100756 0 8100756 0% /dev
tmpfs 1625296 9952 1615344 1% /run
/dev/sdb4 41022688 7074348 31834804 19% /
tmpfs 8126464 19456 8107008 1% /dev/shm
tmpfs 5120 4 5116 1% /run/lock
tmpfs 8126464 0 8126464 0% /sys/fs/cgroup
/dev/loop0 76800 76800 0 100% /snap/ubuntu-core/423
/dev/sdb1 4186108 12160 4173948 1% /boot/efi
zsc-lxd/containers/uc-1604-1 58620160 516736 58103424 1% /var/lib/lxd/containers/uc-1604-1.zfs
zsc-lxd/containers/uc-1610-1 58637184 533760 58103424 1% /var/lib/lxd/containers/uc-1610-1.zfs
tmpfs 1625292 124 1625168 1% /run/user/1000
/dev/sdb5 61796348 10766892 47867344 19% /media/cl/virtual
/dev/sda4 206293688 13419740 182371804 7% /media/cl/system
/dev/sda3 206293688 32116600 163674944 17% /media/cl/rescue
/dev/sda2 206293688 1867604 193923940 1% /media/cl/media
/dev/sda1 206293688 2539712 193251832 2% /media/cl/data
You can use
findmnt --source UUID=uuid
If the output is empty, no partition with this uuid
is mounted, otherwise devicename, mountpoint, filesystem and mount options will be displayed.
Of course, you'll have to replace uuid
with the actual UUID you want to test.
-
I have 5 partitions mounted using
UUID
via/etc/fstab
andfindmnt --source UUID=uuid
(tried withsudo
too) did not find any at all. Thanks.– JagsJun 25, 2021 at 20:23 -
Oh ok. Yes with an actual
UUID
I did get/ /dev/sdh1 ext4 rw,relatime,errors=remount-ro
as the output. Thanks.– JagsJun 25, 2021 at 20:32 -
This might usurp the current answer when I can get on my machine to test. I’ve got a strange feeling I did come across findmnt at some point after asking. Jun 26, 2021 at 21:35
If there is a link named "the_UUID
" in /dev/disk/by-uuid/
the partition is mounted. On my Ubuntu 14.04.5 (YMMV):
$ ll /dev/disk/by-uuid
total 0
lrwxrwxrwx 1 root root 9 Nov 14 04:30 0123-4567 -> ../../sde
lrwxrwxrwx 1 root root 10 Nov 11 00:25 06ee65d5-26f7-41f3-91ab-3497021a6213 -> ../../dm-0
lrwxrwxrwx 1 root root 10 Nov 11 00:25 12817b99-9d2b-4357-a4ca-c11eab672a20 -> ../../sdb6
lrwxrwxrwx 1 root root 10 Nov 11 00:25 362254e8-2b99-442d-8ad9-4a348bc08032 -> ../../sda1
lrwxrwxrwx 1 root root 10 Nov 11 00:25 83a64b80-5a37-4659-b797-221b88ef41f8 -> ../../sdb5
lrwxrwxrwx 1 root root 10 Nov 11 00:25 ff359af0-d996-4949-b27e-f24ce453c48c -> ../../sdc1
-
2Unfortunately my drive which is connected shows up in this list even if not mounted. Nov 14, 2016 at 14:45
-
Look for "interesting" messages in
dmesg
and/var/log/kern.log*
. It sounds like the system is not happy with the filesystem on the partition, and can't mount it. One possible cause is Windows hibernation. Nov 14, 2016 at 14:50 -
I think I may have been confusing, the drive mounts and unmounts cleanly. It's just that while it's not mounted, but is connected and switched on, it shows up in the
/dev/disks/by-uuid/
directory. Nov 14, 2016 at 14:53 -
3Nope,
/dev/disk/by-uuid
and its counterparts holds all UUIDs/IDs/labels of connected devices known to udev regardless of their mount state. Nov 14, 2016 at 17:12
tune2fs
.tune2fs -U random /dev/sdxx
. But this won't work for all types of partitions.