shuf - Linux
Overview
The shuf
command in Linux is used for generating random permutations of input lines. It can shuffle lines from a specified file or standard input and outputs them. This is useful in various scenarios like random sampling, creating test cases, or simply randomizing the order of lines in a file.
Syntax
The basic syntax for the shuf
command is:
shuf [OPTION]... [FILE]
- [FILE]: The name of the file whose lines will be shuffled. If no file is specified, or if the filename provided is
-
,shuf
reads from standard input.
Variations
shuf -e [OPTION]... [ARG]...
shuf -i LO-HI [OPTION]...
- -e: Treat each command-line argument as an input line.
- -i: Generate output consisting of random numbers from range LO to HI inclusive.
Options/Flags
- -n, –head-count=COUNT: Output at most
COUNT
lines. By default, all input lines are output. - -o, –output=FILE: Write result to FILE instead of standard output.
- -r, –repeat: Repeat output values, i.e., allow lines to be repeated in the output.
- -z, –zero-terminated: Line delimiter is NUL, not newline.
Examples
1. Shuffle lines of a file and display on standard output:
shuf myfile.txt
2. Generate 5 random numbers from 1 to 100:
shuf -i 1-100 -n 5
3. Shuffle lines of a file and save them to another file:
shuf -o shuffled.txt myfile.txt
4. Shuffle and repeat lines:
shuf -r -n 20 myfile.txt
Common Issues
- File Not Found: Ensure the file path is correct. The command will error if the specified file does not exist.
- Permission Denied:
shuf
requires read permissions on the files it processes. Ensure appropriate permissions are set.
Integration
shuf
can be integrated with other commands via pipes to perform complex tasks:
cat myfile.txt | shuf | grep 'pattern'
This command chain will shuffle the lines of myfile.txt
, then pass the shuffled content through grep
to filter lines matching a pattern.
Another integration could be with loops for scripting:
for i in {1..10}; do shuf -n 1 file.txt; done
This loop shuffles file.txt
and prints one line each loop iteration, useful for drawing random samples.
Related Commands
- sort: Sort lines of text files.
- awk: Pattern scanning and processing language.
- grep: Searches for and prints out text matching a pattern.
For more details and further examples, consult the shuf
man page (man shuf
) or the GNU Coreutils online documentation.