shuf Command on Linux: Randomize Lines and Pick Samples

shuf command linux - custom-shuf-featured.png

shuf on Linux randomizes line order — ideal for drawing a random sample from IDs, shuffling quiz questions, or anonymizing order in exports. It is simpler than sort -R hacks and built for pipelines.

Related: sort,
uniq,
wc.

Shuffle a file

Each line stays intact; only order changes.

bashshuffle
shuf names.txt
shuf
shuffle

Pick N random lines

-n outputs at most N lines without replacement.

bashsample
shuf -n 5 customers.csv
shuf -n
sample

Shuf from arguments

-e shuffles a short list from the command line.

bashinline
shuf -e Mon Tue Wed Thu Fri
shuf -e
args

Reproducible tests

For deterministic QA, use a seeded language tool; shuf is for live randomness.

TipCombine grep pattern file | shuf -n 20 for a random subset matching a filter.
pipeline
sample

Quick Reference

  • Full shuffle: shuf file
  • Sample: shuf -n 5 file
  • List: shuf -e a b c

Related tutorials

Diagrams are original illustrations by Gnome IT Solutions. Tutorial text © Gnome IT Solutions.