Linux command
shuf 命令
文件
复制后可按需替换文件名、目录或参数。
常用示例
Shuffle lines of a file
shuf [file]
Pick N random lines
shuf -n [5] [file]
Generate random numbers in range
shuf -i [1-100]
Pick N random numbers from range
shuf -i [1-100] -n [10]
Shuffle command arguments
shuf -e [item1] [item2] [item3]
Output with custom delimiter
shuf -e -z [items] | xargs -0
Repeat output
shuf -r -n [10] -e [a] [b] [c]
Use specific random seed
shuf --random-source=[/dev/urandom] [file]
说明
shuf randomly permutes input lines. It reads input, shuffles the order, and outputs all lines in random sequence. Without options, shuf outputs all input lines in random order. The -n option limits output to the first N lines after shuffling - effectively random sampling without replacement. Input range mode (-i) generates sequential numbers and shuffles them. Combined with -n, this selects random numbers from a range. Useful for generating lottery numbers, random IDs, or sampling. Echo mode (-e) shuffles command-line arguments instead of file lines. This enables shuffling small lists without creating temporary files. Repeat mode (-r) enables sampling with replacement - the same line can appear multiple times in output. This is useful for bootstrap sampling or simulation. The random source option ensures reproducible shuffling when given a deterministic source, useful for testing.
参数
- -n _NUM_, --head-count _NUM_
- Output at most NUM lines.
- -i _LO-HI_, --input-range _LO-HI_
- Generate numbers from LO to HI.
- -e, --echo
- Treat arguments as input lines.
- -r, --repeat
- Output can repeat (with replacement).
- -z, --zero-terminated
- Use NUL as line delimiter.
- -o _FILE_, --output _FILE_
- Write to file instead of stdout.
- --random-source _FILE_
- Get random bytes from file.
FAQ
What is the shuf command used for?
shuf randomly permutes input lines. It reads input, shuffles the order, and outputs all lines in random sequence. Without options, shuf outputs all input lines in random order. The -n option limits output to the first N lines after shuffling - effectively random sampling without replacement. Input range mode (-i) generates sequential numbers and shuffles them. Combined with -n, this selects random numbers from a range. Useful for generating lottery numbers, random IDs, or sampling. Echo mode (-e) shuffles command-line arguments instead of file lines. This enables shuffling small lists without creating temporary files. Repeat mode (-r) enables sampling with replacement - the same line can appear multiple times in output. This is useful for bootstrap sampling or simulation. The random source option ensures reproducible shuffling when given a deterministic source, useful for testing.
How do I run a basic shuf example?
Run `shuf [file]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.
What does -n _NUM_, --head-count _NUM_ do in shuf?
Output at most NUM lines.