Command-line Basics: Generating UUIDs

joshtronic

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? UUID@UUID.com 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.

Recommended Node.js video course

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
a522f494-92ce-44e9-b1a3-f891baed8d60

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
834efdb6-6044-4b44-8fcb-560710936f37
e8fa8d54-641a-4d7b-9422-91474d713c62
dff59ac0-4d80-4b96-85c4-14f3a118e7fe
511fea83-9f5f-4606-85ec-3d769da4bf63
3bc82ef7-1138-4f97-945a-08626a42a648
a33abc11-264e-4bbb-82e8-b87226bb4383
2a38839e-3b0d-47f0-9e60-d6b19c0978ad
74dca5e8-c702-4e70-ad16-0a16a64d55fa
cd13d088-21cf-4286-ae61-0643d321dd9e
9aec3d5a-a339-4f24-b5a3-8419ac8542f2

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
63b1146f-9e7c-4e1f-82eb-3fe378e203df,ed9d6201-e5b2-4410-9ab1-35c8ca037994
8d3981b6-f112-4f21-ac4b-44791e279b2a,eb63310e-d436-44fa-80c6-65721a300a2b
0eddfe24-1c2e-43a1-b2c2-9d3af6bad837,62ef1782-76a2-4b3c-ac69-1c2d02f65789
29f18766-fc9d-46a4-a1d0-e112738edb30,b6bd303d-1148-4f46-bec7-d7e4cb6e4f03
865bcf30-6a8b-49d6-8b27-8dc51620adf7,972b0959-4270-4683-b19b-360b2605f2d0
0d82d54b-566a-45d1-b3a8-5da1a88bceb3,1c67a802-9647-46b1-bde4-3053699b27f9
778b5415-3e1f-4bc5-a349-499459ac4ab7,7e1a2081-c742-4882-9154-e5d2a4af630c
e6cc95bd-3ee1-43cb-bea1-51783de5fc57,5088d3a3-ab67-4684-8761-e48bb14596ec
a7453bc0-b5e5-41a3-9ed4-cf4d8e0908a2,957ef50f-7889-4335-9f40-17878e3d20fe
3689362d-588a-409e-bd2c-d6fdaa361574,9ffe7c8d-9afb-4b24-a5b7-b29a06f6fac7

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
7dd44050-9df4-43aa-b3b4-3b47eff8fc31@3052e93c-95d1-40f5-b468-3d4e06dd208b.com
cca71187-f666-46ff-81c6-eb3b72ff6972@30f4c9a8-712e-4f4c-ad3a-4b55ef85eee0.com
6ff086ad-493d-4b3a-8ed1-970239d7125b@8302d772-4deb-43d1-8901-0a3b4f747b55.com
f9813daa-6a8e-4543-8708-d42cefdda20a@d586854c-7df9-4046-89f8-51f960973afb.com
a7e9e43b-d2b1-4415-b73d-ff72b713e45f@a7c56c2c-df25-44bc-872d-a893f750b54d.com
0d1d13fe-777d-44d8-b1b2-302ca1e48aa1@7c2d8e6a-fa8b-4fa3-a0ef-8360aa42e730.com
f85d0772-22d2-43d0-8d71-4e6714c2bb20@fb4f74fe-f9f9-4e86-b31d-f148344a97e0.com
f46eb868-0a99-4291-98f2-19d95f1e9fbb@37ef072d-c515-4145-8b8a-edf32ec18bd2.com
eaa4a63e-2646-427a-a892-f8027c2791ed@33daf102-2b5b-4070-88c5-261fe5d96cfa.com
d75f6720-b249-4395-bcc7-9ffe2b67cabb@457b04b4-3c15-4b77-aae2-9afd6803bcfe.com

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 mailinator.com 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`@mailinator.com; done
4ba50929-520b-49f7-996d-e369be5d6232@mailinator.com
16deaeae-64bd-45f0-9f73-b32d41ca1bfb@mailinator.com
743701e8-0dc5-4851-8fc4-24d155755bdc@mailinator.com
adff0015-c535-431a-970f-98ffd1fc21eb@mailinator.com
6516fcb3-e54f-4800-a6cc-11d50d756f28@mailinator.com
8a9c5252-bd0c-4c3b-a7c9-4b60ebcc4294@mailinator.com
eed94fd6-b075-493c-8d8e-3acae90d5629@mailinator.com
f4ab80d2-85ca-4722-a260-0f84c37051fd@mailinator.com
53ead1d0-cc70-410f-a91a-4a79b339fba2@mailinator.com
b208e103-d7f1-4f6d-838d-530d6339dce7@mailinator.com

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`@mailinator.com; done > /tmp/emails.txt

$ cat /tmp/emails.txt
826119d2-f590-4fa3-ba7e-0717869d40b1@mailinator.com
795fec1a-76fe-4fed-8a06-ed517c1a5e7d@mailinator.com
14a502ad-0aa9-40e5-a46f-5806264b5316@mailinator.com
c6c2a588-7cce-4675-a490-0101d7bcc614@mailinator.com
7346c15b-0c92-44c4-a854-5de18c0c202d@mailinator.com
c67a535a-e28d-43b1-b553-c203bc22a821@mailinator.com
76d22d18-0f09-405d-9903-eb44ec93b605@mailinator.com
2b631756-21e6-4d95-873b-3245797f9028@mailinator.com
aab686e8-540e-43e9-9e24-ca04fbf4d414@mailinator.com
a577e9c9-0ad1-4934-b5f1-17b68938fff8@mailinator.com

Conclusion

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#
#native_cta#