Need Randomness? Unleash the Power of Shuf!
Ever needed to shuffle lines in a file, pick a random winner from a list, or generate a random password? The shuf command-line utility is your Swiss Army knife for all things random. This unassuming tool, part of the GNU Core Utilities, provides a simple yet powerful way to generate random permutations of input data. Forget complex scripting; shuf makes random selection and shuffling a breeze.
Overview

shuf (short for “shuffle”) is a command-line tool that generates random permutations of input lines. It’s ingenious in its simplicity: it takes input from a file or standard input, and outputs a randomly reordered version of that input. This may seem trivial, but the applications are vast. It’s particularly useful in scripting where you need to introduce randomness without relying on complex programming languages. Imagine needing to select a random subset of users from a database for A/B testing. Or perhaps you need to randomize the order of questions in a quiz. shuf excels at these tasks, providing a quick and reliable solution. It is part of the GNU Core Utilities package, meaning it is often pre-installed on Linux distributions.
Installation
Since shuf is part of the GNU Core Utilities, it’s highly likely that it’s already installed on your system. If not, you can install it using your distribution’s package manager.
- Debian/Ubuntu:
sudo apt-get update sudo apt-get install coreutils - Fedora/CentOS/RHEL:
sudo dnf install coreutils - macOS (using Homebrew):
brew install coreutils
After installation, verify it by running:
shuf --version
This should output the version number of the shuf utility.
Usage
shuf is extremely versatile, accepting input from files, standard input, or generating a sequence of numbers. Here are some common use cases with practical examples:
Shuffling Lines in a File
This is the most basic use case. Let’s say you have a file named names.txt containing a list of names, one per line:
Alice
Bob
Charlie
David
Eve
To shuffle these names randomly, simply run:
shuf names.txt
The output will be a randomly reordered list of the names:
David
Charlie
Alice
Eve
Bob
Note that the order will be different each time you run the command.
Selecting a Random Sample
You can select a specific number of random lines using the -n option. For example, to select 2 random names from names.txt:
shuf -n 2 names.txt
This will output two random names:
Alice
Charlie
Again, the output will vary each time.
Generating a Random Sequence of Numbers
shuf can also generate a random sequence of numbers using the -i option. This option takes a range of numbers as input. For example, to generate a random permutation of numbers from 1 to 10:
shuf -i 1-10
The output might look like this:
7
3
1
9
5
2
10
4
6
8
Generating a Random Password
Combining shuf with other utilities can be used to create random passwords. First, create a file containing all possible password characters:
echo {a..z}{A..Z}0123456789!@#$%^&*()_+\|~-=`{}[]:">?<,./ >> chars.txt
Now, create a password generator function:
generate_password() {
shuf -n "$1" chars.txt | tr -d '\n'
echo
}
This can be enhanced by reading the characters from a file:
generate_password_from_file() {
length=$1
chars_file=$2
# Check if file exists
if [ ! -f "$chars_file" ]; then
echo "Error: Character file '$chars_file' not found." >&2
return 1
fi
# Count number of lines
num_chars=$(wc -l < "$chars_file")
# Error handling to avoid out of bounds
if [ "$length" -gt "$num_chars" ]; then
echo "Error: Password length ($length) cannot exceed the number of characters in the file ($num_chars)." >&2
return 1
fi
# Generate the password
shuf -n "$length" "$chars_file" | tr -d '\n'
echo
}
Then call it
generate_password_from_file 16 chars.txt
Running generate_password 16 will generate a random 16-character password:
aZ9!b@c#D$e%F^
Using tr -d '\n' removes the newline characters added by shuf, ensuring the output is a single line of characters.
Reading from Standard Input
shuf can also read input from standard input. This is useful when piping the output of another command to shuf. For example, to shuffle a list of files returned by ls:
ls -l | shuf
This will list the files in the current directory in a random order. This is incredibly useful for automating randomized tasks in scripts.
Dealing with Duplicate Lines
By default, shuf shuffles all lines, even if there are duplicates. If you want to ensure that each input line appears only once in the output (like a true random sample without replacement), you’ll need a slightly more complex solution using tools like awk or sort -u (unique) before piping to shuf. For instance:
sort -u names.txt | shuf -n 3
This will first remove duplicate lines (if any exist) from `names.txt` and then output 3 random unique lines.
Controlling the Random Number Generator
For reproducible results (useful for debugging or testing), you can seed the random number generator using the `–random-source=FILE` option. The FILE needs to contain random data.
shuf is an excellent tool for running virtual events, it can be used to randomly assign participants to breakout groups or to select random questions for a Q&A session.
Tips & Best Practices
- Understanding Input Sources: Be clear about where your input is coming from – a file, standard input, or a range of numbers. The correct usage depends on the input source.
- Handling Large Files: For very large files, consider the memory implications.
shufloads the entire input into memory. If memory is a concern, consider alternative approaches that process the file in chunks. However, for most common use cases, this is not a significant issue. - Combining with Other Utilities:
shufshines when combined with other command-line tools. Use pipes to create powerful and flexible workflows. For example, usegrepto filter data before shuffling it. - Quoting Arguments: When using
shufwith variables, be sure to quote the variables properly to prevent word splitting and unexpected behavior. - Removing Duplicates: If you need truly unique randomness consider removing duplicates before piping to `shuf`.
Troubleshooting & Common Issues
- “shuf: invalid option” error: This usually means you are using an older version of coreutils that doesn’t support a particular option. Check your coreutils version using
shuf --versionand consult the documentation for your version. - Unexpected output order: Remember that
shufproduces *random* output. If you expect a specific order, you’re misunderstanding the tool’s purpose. If you need reproducible results, use the `–random-source` option to control the random number generator. - “shuf: memory exhausted” error: This indicates that the input data is too large to fit in memory. Try processing the data in smaller chunks or using a more memory-efficient approach.
- Script is non deterministic: If you want the script to behave deterministically, and provide the same shuffling and random numbers consider using `–random-source=FILE`. It might be a good idea to also provide the seed in the documentation/help for the script.
FAQ
- Q: Can
shufhandle binary files? - A: While
shufprimarily deals with text files (specifically, lines of text), you can technically use it with binary files, but the results may not be meaningful unless the binary data is structured in a line-oriented manner. - Q: How can I ensure that
shufproduces truly random results? - A:
shufrelies on the system’s random number generator. On most systems, this is sufficient for general-purpose randomness. For cryptographic purposes, you might need a more robust random number source. - Q: Is
shufavailable on Windows? - A:
shufis a standard Unix/Linux utility. You can use it on Windows via the Windows Subsystem for Linux (WSL) or by installing a Unix-like environment such as Cygwin or Git for Windows (which includes a Bash environment). However, using a native PowerShell solution may be a better fit for pure windows scripting. - Q: How can I shuffle files by file extension with `shuf`?
- A: You can do that by filtering the output of `ls` with `grep` and piping into `shuf`, i.e. `ls | grep .txt$ | shuf`
Conclusion
shuf is a deceptively simple command-line tool that provides a powerful way to introduce randomness into your scripts and workflows. Whether you need to shuffle lines in a file, select a random sample, or generate a random password, shuf offers a quick and efficient solution. Experiment with the examples provided and discover the many ways this versatile utility can simplify your tasks. Now, go ahead and give shuf a try! Visit the GNU Core Utilities page for more information: https://www.gnu.org/software/coreutils/.