ICS/Lotus (mostly), Linux, Travel, Skiing, Mixology, and Random Musing of Interest

Linux School -- Displaying UUID Values and Manipulating the Output with Cut

Bill Malchisky  July 8 2013 01:02:00 AM
This post introduces two power tools: blkid and cut, with the latter manipulating the output of the former. So, consider this an entry for administrators and developers, or anyone writing a shell script.

Real World Use Case

Before the introduction of the Universally Unique IDentifier (UUID) in 2005 {For an advanced comprehension, read RFC 4122}, one's /etc/fstab file contained mount point objects like "/dev/sda1" and the respective mount point sub-directory. That process worked for decades, but did introduce a couple of issues when installing additional partitioned drives. Based upon their configuration and the BIOS' boot order, a file system on the new drive could be recognized as /dev/sda1, for example. So unless you adjusted the /etc/fstab accordingly, you may discover unexpected results or even be unable to boot. UUIDs solve this and allow for virtually unlimited flexibility with your systems.

There are countless community entries and related comments about people seeing their mounted file systems disappearing in their Ubuntu desktop--going back several years. This typically occurs on dual-boot configurations where the user seeks to map, say, a pointer back to the unused operating system on the same or a different HD on the same machine. The suggested solution is to ensure that the local /etc/fstab utilizes UUIDs instead of device paths to the mount point object. As such, I thought it would be a good idea to show you a great native tool on UNIX/Linux systems to display the UUID values for your mounted file systems.


1. See your /etc/fstab file for UUID mounting examples, on any newer Linux system
2. You may also see UUIDs referred to as Globally Unique IDentifers (GUIDs) for lucidity

Displaying UUIDs

For testing purposes, I created a simple Ubuntu 12.04 LTS desktop VM with standard partitions to make the point. Your system may have more file systems, thus making for a longer return value set. As blkid requires root access, you need to either run an $su - command first, or use with sudo as below.

bill@ubuntu:~$ sudo blkid

[sudo] password for bill:
/dev/sda1: UUID="9c9ccc38-b453-4299-a577-1e212515f0de" TYPE="ext4"
/dev/sda5: UUID="677d3304-ab67-4709-b095-4db94db5b6c5" TYPE="swap"

If you like a tabled format, try this command variant:

bill@ubuntu:~$ sudo blkid -o list

device     fs_type label    mount point    UUID


/dev/sda1  ext4             /              9c9ccc38-b453-4299-a577-1e212515f0de

/dev/sda5  swap             {swap}         677d3304-ab67-4709-b095-4db94db5b6c5

Programmatically Parsing the Output -- Various Forms

For developers, output forms such as:

bill@ubuntu:~$ sudo blkid -o device



bill@ubuntu:~$ sudo blkid -o value





provide a consistent reliable structure, allowing for easy programmatic parsing, if your application is authorized to run blkid, of course.

Utilizing a combination of grep and cut, you can literally extract key values with surgical precision. Let's build a quick a process to get just the UUID for /dev/sda1.
1. First, grab only the sda1 entry
bill@ubuntu:~$ sudo blkid | grep 'sda1'

/dev/sda1: UUID="9c9ccc38-b453-4299-a577-1e212515f0de" TYPE="ext4"

2. Now we need just the UUID sub-string
bill@ubuntu:~$ sudo blkid | grep 'sda1' | grep -o -E 'UUID="[a-zA-Z|0-9|\-]*'


3. Finally, we can add cut and tell the tool to extract from the seventh character to the end of the line as indicated here
bill@ubuntu:~$ sudo blkid | grep 'sda1' | grep -o -E 'UUID="[a-zA-Z|0-9|\-]*' | cut -c 7-


Cut is really a powerful tool
and I encourage you to read the short man page on it to spawn scores of creative use cases. Think of it as an all-in-one @rightback, @leftback, @right, @left, @middle, @middleback and a few other variants from the IBM Notes Formula language.

Suboptimal Practices

Although it is not quite a worst practice, if you desire to consume more time, you can simply peruse the device disk directory and correlated UUID links, then expand each symbolic link on your own. With such a nice tool in blkid, I see little value in performing a multi-step process that only becomes more complicated the more file systems your computer mounts.

bill@ubuntu:~$ ls -F /dev/disk/by-uuid/

677d3304-ab67-4709-b095-4db94db5b6c5@  9c9ccc38-b453-4299-a577-1e212515f0de@

bill@ubuntu:~$ ls -l /dev/disk/by-uuid/677d3304-ab67-4709-b095-4db94db5b6c5
lrwxrwxrwx 1 root root 10 Jul  7 00:32 /dev/disk/by-uuid/677d3304-ab67-4709-b095-4db94db5b6c5 -> ../../sda5

I utilized the '-F' or classify parameter with the ls command to illustrate better the existence of symbolic links in the directory, in-case the blue font did not translate when posted.

You could also use #dumpe2fs, but you must provide each device separately and then parse each command's results with grep. It can be done of course, but is a lot of work over blkid, for more than a couple file systems. Though dumpe2fs is a great tool, it is best used elsewhere.

I hope you enjoy using blkid and cut.
No Comments Found

Powered by IBM Lotus Domino 8 | Lotus User Group | Get Firefox! | This blog is listed on Planet Lotus   IBM Certified

© 2010 William Malchisky.