Command-line Basics: Generating UUIDs


Universally unique identifiers (UUIDs) are 128-bit numbers that are accepted as being unique on the local system they are created on as well as among the UUIDs created on other systems in the past as well as the future. Because of their uniqueness, they can be created on both the client and server and come in really handy in situations where an auto incremented primary key can fall short.

Because of their uniqueness, UUIDs are well suited for generating test data. Need a random string? A UUID is fine. What about an email? is great. Need a bunch of random string? UUIDs will be unique, making them easy to track down as they move through a system.

Getting started

To generate universally unique identifiers from the command-line you will need the uuidgen utility.

Fortunately, the command is pretty standard issue on Unix-like operating systems like Linux and macOS.

If you don’t happen to have the command available (try running it to see) please consult with your system’s package manager and see if it’s available.

Also worth noting, the macOS version of uuidgen does function a bit differently than that of the Linux version in that is returns UUIDs in all capital letters.

For the sake of example, this shouldn’t matter much.

Generating a UUID

The easiest way to generate a UUID is to run uuidgen without any arguments. Out of the box, uuidgen will generate a random UUID assuming you have a high-quality random number generator available:

$ uuidgen

You can also generate time-based and hash-based UUIDs but generally speaking, the random values are probably sufficient.

Generating a bunch of UUIDs

Generating a single UUID is pretty easy. To generate a bunch of them, we will need to leverage a small bit of shell scripting.

Let’s say we wanted to generate 10 UUIDs, we could write a short loop:

$ for i in {1..10}; do uuidgen; done

You can swap the 10 out for whatever number you’d like (the system’s math co-processor permitting).

If you wanted to generate a list of comma-separated values (CSV) with 2 UUIDs per line, you simply echo out multiple UUIDs during each iteration of our for loop:

$ for i in {1..10}; do echo `uuidgen`,`uuidgen`; done

Based on the unique nature of UUIDs, we don’t have to worry about any duplicates in our generated data!

Remember how I mentioned generating email addresses? A small tweak to our echo statement, and we can generate a list of email-looking data:

$ for i in {1..10}; do echo `uuidgen`@`uuidgen`.com; done

These are all well and good, but they aren’t real email addresses that we can easily check.

If we were to tweak the output one more time, and swap the second uuidgen for a disposable email address domain, like we will not only have a list of email-looking data, but it will be a list email addresses we could actually monitor!

$ for i in {1..10}; do echo `uuidgen`; done

And for good measure, if you wanted to save the output of any of the previous examples to a file, you can append > /path/to/some/file to pipe the output:

$ for i in {1..10}; do echo `uuidgen`; done > /tmp/emails.txt

$ cat /tmp/emails.txt


Universally unique identifiers are like a better version of a random number.

Their uniqueness makes them quite powerful and combined with some light shell scripting on the command-line, we’re able to generate a substantial volume of data. All without needing to load up our favorite language’s package repository.

Next time you’re in need of a UUID, save yourself the search for “online UUID generator” and take care of your system’s command-line interface!

Want to learn more about your system’s particular implementation of uuidgen? Simply type man uuidgen in your favorite terminal and go beyond the basics covered here.

  Tweet It

🕵 Search Results

🔎 Searching...

Sponsored by #native_company# — Learn More
#native_title# #native_desc#