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?
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?
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:So:
Since
lsblk
can act on specific devices, you can also do: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.If you only want one line with your UUID and mountpoint ($UUID represents your UUID):
The mount point will be empty if it is unmounted. Try
lsblk -h
for more options.Use
awk
to print the result. ifNF
(Number of fields) is more than one it means that it has a mount point:If you want the details as from
mount
replace
your-UUID-here
with your UUIDmore readably:
output example:
You can just make it check that the string is not null and echo "mounted":
but others gave better ways to do that :)
Ubuntu uses
UDisks2
daemon, which polls whole lot of information about file systems, and we can useudisksctl
and filter its output to find a quick and dirty way to find the info we need: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.
My solution
procfs
, so no weird command output formatting issues,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:
Which means I can quickly deduce if it's mounted by grepping the output of
df
:Or by using the code in Muru's answer, which has the added bonus of telling me where the drive is mounted:
You can use the
df
command to see the mounted file systems and their mount point. Here is an example from my machine ... the partitionsdb3
, 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) :You can use
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.If there is a link named "
the_UUID
" in/dev/disk/by-uuid/
the partition is mounted. On my Ubuntu 14.04.5 (YMMV):