Generating UUIDs from the command line
On this page
You don’t need a website (or this one) to generate a UUID. Every modern OS ships with a tool. Here’s the cheatsheet for every common shell, plus how to bulk-generate, verify, and extract timestamps.
macOS / Linux
uuidgen (preinstalled on macOS, in util-linux on most distros)
uuidgen
# 0E6F1B8C-2C33-4F1F-9C0B-2A3D4E5F6A7B (uppercase on macOS)
For lowercase:
uuidgen | tr 'A-Z' 'a-z'
# 0e6f1b8c-2c33-4f1f-9c0b-2a3d4e5f6a7b
Apple’s uuidgen does v4 only. GNU uuidgen (Linux) supports more:
uuidgen --random # v4 (default)
uuidgen --time # v1
uuidgen --md5 --namespace @url --name "https://example.com" # v3
uuidgen --sha1 --namespace @url --name "https://example.com" # v5
/proc/sys/kernel/random/uuid (Linux only)
cat /proc/sys/kernel/random/uuid
Always lowercase, always v4, no dependency on util-linux.
openssl (preinstalled almost everywhere)
openssl rand -hex 16 gives 32 random hex characters but not a valid UUID, because the version and variant bits aren’t set. Use uuidgen, /proc/sys/kernel/random/uuid or the Python one-liner instead.
python3 -c
python3 -c 'import uuid; print(uuid.uuid4())'
python3 -c 'import uuid; print(uuid.uuid7())' # Python 3.14+
python3 -c 'import uuid; print(uuid.uuid5(uuid.NAMESPACE_URL, "https://example.com"))'
Python 3.14’s uuid7() is the easiest way to get a v7 UUID at the shell.
node -e
node -e 'console.log(crypto.randomUUID())'
Works in Node 14.17+. The crypto module is global — no require needed.
Use a UUID in a bash script
Capture it in a variable with command substitution:
id=$(uuidgen | tr 'A-Z' 'a-z')
echo "Created job $id"
For a script that has to run on both macOS and Linux, fall back to the kernel file when uuidgen is missing:
new_uuid() {
if command -v uuidgen >/dev/null; then
uuidgen | tr 'A-Z' 'a-z'
else
cat /proc/sys/kernel/random/uuid
fi
}
id=$(new_uuid)
Windows
PowerShell
[guid]::NewGuid()
# Guid
# ----
# 0e6f1b8c-2c33-4f1f-9c0b-2a3d4e5f6a7b
To get just the string:
[guid]::NewGuid().ToString()
For uppercase / formatted:
[guid]::NewGuid().ToString("N") # no hyphens
[guid]::NewGuid().ToString("B") # {braces}
CMD
CMD has no built-in UUID. Use PowerShell:
powershell -Command "[guid]::NewGuid().ToString()"
Or use uuidgen.exe, see below.
uuidgen.exe (Windows SDK)
Windows has its own uuidgen.exe, but it isn’t on a standard install. It comes with the Windows SDK and Visual Studio’s C++ tools. Open a Developer Command Prompt and run:
uuidgen :: one lowercase UUID
uuidgen -n5 :: five UUIDs
If you don’t have the SDK, PowerShell’s [guid]::NewGuid() does the same job with nothing to install.
Windows Subsystem for Linux
Same commands as Linux above. WSL ships with uuidgen in Ubuntu and most other distros.
Bulk generation
Generate N UUIDs
# 100 UUIDs, one per line
for i in $(seq 1 100); do uuidgen; done | tr 'A-Z' 'a-z'
# Faster on Linux — read from /proc directly
for i in $(seq 1 100); do cat /proc/sys/kernel/random/uuid; done
# Python — fastest for large batches
python3 -c 'import uuid; [print(uuid.uuid4()) for _ in range(10000)]'
# Node
node -e 'for(let i=0;i<10000;i++) console.log(crypto.randomUUID())'
For tens of thousands of UUIDs, the Python and Node one-liners are 10–100× faster than looping uuidgen in shell.
Generate as CSV
echo "uuid" # header
python3 -c 'import uuid; [print(uuid.uuid4()) for _ in range(10000)]'
Or one line:
{ echo uuid; for i in $(seq 1 10000); do cat /proc/sys/kernel/random/uuid; done; } > uuids.csv
Generate v7 in bulk (cross-platform)
python3 -c '
import uuid
for _ in range(10000):
print(uuid.uuid7())
'
Requires Python 3.14 or later. For older Python, install uuid-utils:
pip install uuid-utils
python3 -c 'import uuid_utils; [print(uuid_utils.uuid7()) for _ in range(10000)]'
Validation from the shell
# Test a value
echo "0e6f1b8c-2c33-4f1f-9c0b-2a3d4e5f6a7b" | grep -qE '^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$' && echo valid || echo invalid
# As a function
is_uuid() {
echo "$1" | grep -qE '^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$'
}
is_uuid "0e6f1b8c-..." && echo "✓"
Extracting a v7 timestamp from the shell
v7_to_time() {
local id="$1"
local hex="${id//-/}" # strip hyphens
local ts_hex="${hex:0:12}" # first 12 hex = 48 bits = ms
local ms=$((16#$ts_hex))
date -u -d "@$((ms / 1000))" "+%Y-%m-%d %H:%M:%S UTC" 2>/dev/null \
|| date -u -r "$((ms / 1000))" "+%Y-%m-%d %H:%M:%S UTC"
}
v7_to_time "01928a47-3b30-7c5e-9d1a-f0b8c4a7e923"
# 2024-10-09 21:32:30 UTC
The first date form is GNU (Linux); the second (-r) is BSD (macOS). The function tries one then the other.
Aliases worth setting
In ~/.zshrc or ~/.bashrc:
alias uuid="uuidgen | tr 'A-Z' 'a-z' | tee >(pbcopy)" # macOS — also copies to clipboard
alias uuid="cat /proc/sys/kernel/random/uuid" # Linux
Then uuid produces a fresh UUID and (on macOS) copies it to the clipboard. Saves a remarkable amount of context-switching.
Common pitfalls
- macOS
uuidgenis uppercase. Pipe throughtr 'A-Z' 'a-z'if you need lowercase for cross-platform consistency. echoadds a newline. When piping a UUID into another tool that doesn’t tolerate trailing whitespace, useprintf '%s' "$uuid".- Don’t use
$RANDOMin shell. It’s 15 bits, not cryptographically random, and trivially collidable. Use/dev/urandomor one of the methods above.
FAQ
How do I generate a UUID in bash?
Run uuidgen, which ships with macOS and comes with util-linux on most Linux desktops and servers, though minimal containers often leave it out. To save it, use id=$(uuidgen). On Linux, cat /proc/sys/kernel/random/uuid works even without uuidgen.
Why is uuidgen output uppercase on a Mac?
Apple’s uuidgen prints capital letters, while Linux versions print lowercase. Both are the same UUID. Pipe through tr 'A-Z' 'a-z' if you want lowercase everywhere.
How do I get uuidgen on Windows?
uuidgen.exe comes with the Windows SDK and runs from a Developer Command Prompt. Without the SDK, use PowerShell: [guid]::NewGuid().ToString(). In WSL, the Linux uuidgen works as usual.
Is a UUID from the command line safe to use as an ID?
Yes. uuidgen, /proc/sys/kernel/random/uuid, PowerShell, Python and Node all produce random v4 UUIDs from the system’s secure random source. Avoid building one from $RANDOM.
Cheat sheet
| Need | Command |
|---|---|
| One v4 (macOS / Linux) | uuidgen |
| Lowercase | uuidgen | tr 'A-Z' 'a-z' |
| One v4 (Linux only, no util-linux) | cat /proc/sys/kernel/random/uuid |
| One v4 (Windows) | [guid]::NewGuid().ToString() |
| One v7 | python3 -c 'import uuid; print(uuid.uuid7())' |
| Bulk N | for i in $(seq 1 N); do uuidgen; done |
| Validate | grep -qE '^[0-9a-f]{8}-[0-9a-f]{4}-...' |
Try the tools
- UUID generator — instant in-browser, all versions, copy with one click
- Bulk UUID generator — generate up to 10,000 with CSV/JSON download
- UUID validator — paste, get version + timestamp