Generate UUIDs on the Command Line

Christoph Herzog
  • uuid
  • uuids
  • identifiers
  • linux
  • os-x
  • python
  • bash
  • uuidgen
  • develop
  • random-tip

UUIDs are becoming more popular in distributed systems, and can increasingly be found in databases and applications.

Sometimes you just need a quick way to generate a UUID, be it for writing a unit test, manually testing a function or creating some stub data.

Here are some methods for creating random (version 4) UUIDs straight from the command line:

#uuidgen (Linux and OS X)

# Single UUID:
uuidgen -r
# 100 UUIDs:
seq 100 | xargs -I -- uuidgen -r 

The uuidgen command is available by default on OS X, and should be on most Linux distros.
On Debian systems you need the package uuid-runtime, on Arch libutil-linux.

#Linux uuid proc file (Linux only)

The Linux kernel has the special proc file /proc/sys/kernel/random/uuid, which will yield a new UUID each time it is read.

# Single UUID:
cat /proc/sys/kernel/random/uuid
# 100 UUIDs:
seq 100 | xargs -I -- cat /proc/sys/kernel/random/uuid

#Python (portable)

Fortunately, Python has the uuid module in it's standard library, so this method will work on any system with a Python installation.

# Single UUID:
python -c "import uuid; print(uuid.uuid4());"
# 100 UUIDs:
python -c "import uuid; print('\n'.join([str(uuid.uuid4()) for _ in range(0,100)]));"

#uuidgenerator.net

As a measure of last resort, if you have CURL or wget available, you can use the website uuidgenerator.net, which provides an API for generating UUIDs.

# Single UUID:
curl https://www.uuidgenerator.net/api/version4
# 100 UUIDs:
curl https://www.uuidgenerator.net/api/version4/100
theduke.at | © Christoph Herzog (theduke), 2016 | Vienna, Austria