Skip to content

shuf Command Cheat Sheet


Basic Usage

Generate a random permutation of the lines in a file

shuf filename

Generate a random permutation of the lines from standard input

echo -e "line1\nline2\nline3" | shuf

Specify Number of Output Lines

Generate a random permutation of three lines in a file

shuf -n 3 filename

Generate a single random line from standard input

echo -e "line1\nline2\nline3" | shuf -n 1

Generating Random Numbers

Generate a random number between 1 and 10

shuf -i 1-10 -n 1

Generate three random numbers between 1 and 100

shuf -i 1-100 -n 3

Repeat Output Lines

Repeat output lines

shuf -r -n 10 filename

Generate 10 random numbers between 1 and 5 with repetition

shuf -r -i 1-5 -n 10

Zero-Padded Numbers

Generate a random, zero-padded number between 1 and 100

shuf -z -i 1-100 -n 1

Generate three random, zero-padded numbers between 1 and 1000

shuf -z -i 1-1000 -n 3

Using shuf With Other Commands

Shuffle the lines of the /etc/passwd file

cat /etc/passwd | shuf

List files in random order

ls | shuf

Pick a random file from a directory

ls | shuf -n 1

Display random log lines

cat /var/log/syslog | shuf -n 10

Shuffle CSV data but keep header line

(head -n 1 file.csv && tail -n +2 file.csv | shuf) > shuffled_file.csv