shuf

Randomly permute lines, numbers, or input data.

Category: Text Processing Randomization Sampling coreutils

What it does

shuf outputs lines or numbers in random order. It is useful for random sampling, load testing, or selecting random entries from files.

How it works (mechanical)

10 Practical Examples

# 1) Shuffle lines in a file
shuf names.txt
# 2) Pick one random line
shuf -n 1 names.txt
# 3) Pick 5 random lines
shuf -n 5 names.txt
# 4) Shuffle numbers 1–10
shuf -i 1-10
# 5) Shuffle numbers with limit
shuf -i 1-100 -n 10
# 6) Randomize server list before deployment
cat servers.txt | shuf
# 7) Random test user selection
shuf -n 1 /var/tmp/inu0users
# 8) Combine with head
shuf file.txt | head -n 3
# 9) Deterministic shuffle (seed via random-source)
shuf --random-source=seedfile file.txt
# 10) Randomize pipeline input
grep ERROR logs.txt | shuf

Notes & Gotchas

Historical Context

Part of GNU coreutils, shuf reflects Unix’s composability philosophy: small tool, precise function, powerful when piped.

Related Commands