Skip to content
shellmap

Shuffle lines in a file

Randomize the order of lines in a file — useful for sampling, A/B-bucketing, and test fixtures.

How to shuffle lines in a file in each shell

Bashunix
shuf file.txt
Zshunix
shuf file.txt
Fishunix
shuf file.txt
PowerShellwindows
Get-Content file.txt | Sort-Object {Get-Random}

The script-block sort key `{Get-Random}` returns a fresh random integer PER ELEMENT, which `Sort-Object` then uses for ordering — effectively a Fisher-Yates shuffle. NOT cryptographic. For repeatable shuffles seed via `Get-Random -SetSeed N` once before sorting (pwsh 5.1+).

cmd.exewindows
powershell -NoProfile -Command "Get-Content file.txt | Sort-Object {Get-Random}"

Equivalents listed for Bash, Zsh, Fish, PowerShell, cmd.exe.

Gotchas & notes

  • macOS BSD has NO `shuf`. The portable substitute is `sort -R` (BSD `sort` 8.0+) — but `sort -R` groups identical lines together (random-order BUCKETS, not random per line). Don't use `sort -R` if your file has duplicates and you want true random order. `brew install coreutils` provides `gshuf` matching GNU behavior exactly.
  • Sample N random lines: `shuf -n 100 file.txt` (GNU/Linux), `gshuf -n 100 file.txt` (macOS), `Get-Content file.txt | Get-Random -Count 100` (pwsh — reads whole file then samples WITHOUT replacement). For huge files where loading all lines is infeasible: reservoir sampling via awk — `awk -v n=100 'BEGIN{srand()} {a[NR]=$0} END{for(i=1;i<=n;i++){k=int(rand()*NR)+1; print a[k]}}'` (with-replacement; rewrite for without).
  • `shuf` reads the WHOLE file into RAM. A 4 GB log file may OOM the shell. For multi-gigabyte shuffles use `terashuf` (external, disk-based merge-sort) or partition the file with `split -l N`, shuffle each partition, then merge in random order. For pwsh: `Get-Content -ReadCount 1000` batches, but full shuffle still requires all lines in memory.
  • Reproducibility: GNU `shuf --random-source=/path/to/seedfile` makes the shuffle deterministic given the seed file (`echo "any-string" > /tmp/seed`). pwsh: `Get-Random -SetSeed 42` once, then chain `Sort-Object {Get-Random}` — but `-SetSeed` resets the SHARED RNG state for the process, affecting all subsequent `Get-Random` calls. Don't use `-SetSeed` in long-running scripts.

Related commands

Related tasks