Text and processing
The shuf command
Shuffles the lines of a file or standard input into random order, or picks random lines. Useful for draws, data samples and unordered lists.
Most useful options
-n NOutputs only N randomly chosen lines-eTreats each argument as a line instead of reading a file-i A-BShuffles the integers from A to B, with no file needed-rAllows repeated lines: with -n, the same one can come out more than once-o ARCHIVOWrites the result to a file instead of standard output--random-source ARCHIVOTakes the seed from a fixed file, so the order repeats. Useful for testingCopy-ready examples
$ shuf -i 1-49 -n 6 | sort -nsix distinct numbers between 1 and 49, like a lottery draw. The result changes on every run.Example output
4 17 23 31 38 44
$ shuf -e rojo verde azulshuffles loose arguments. Each run gives a different order.Example output
azul verde rojo
$ shuf -n 3 nombres.txtpicks three random lines from a file with one name per line.Example output
Carmen Iván Lucía
$ shuf -r -n 5 caras.txtfive coin tosses. With -r, heads and tails can repeat.Example output
cruz cara cara cruz cruz
You can try them in the browser terminal: it does not touch your computer.
Common errors
| Message | Cause | Fix |
|---|---|---|
| shuf: nonexist: No such file or directory | The file does not exist or the path is misspelled. | Check the name with ls or feed the content through standard input: shuf < file. |
| shuf: invalid line count: 'abc' | The value given to -n is not a whole number. | Write digits only, for example -n 5. |
| shuf: invalid line count: '-1' | -n does not accept negative numbers. | Use a positive number. |
| shuf: invalid input range: '5-1' | With -i, the first number must be less than or equal to the second. | Reverse the range: shuf -i 1-5. |
Typical mistakes
- Every run gives a different order. To reproduce a draw, use --random-source with a fixed file.
- Without -r, each input line appears only once. Duplicates in the file still appear as many times as they were.
- shuf shuffles whole lines: leading and trailing spaces are part of the line, and a Windows CRLF stays at the end.
Differences from similar commands
FAQ
How do I pick a random winner from a list?
Save the names in a file, one per line, and run shuf -n 1 list.txt.
How do I generate a random integer between 1 and 100?
With shuf -i 1-100 -n 1. It is more readable than building one from $RANDOM.
How do I always get the same order?
With --random-source pointing to a file with fixed data. Each run then produces the same permutation.
Learn to actually use it
The basic Linux course takes you from zero to console fluency with 24 self-grading lessons.
Start the free course