Need Random Data? Harness the Power of “shuf”!
In the realm of command-line tools, sometimes the simplest utilities provide the most unexpected power. shuf is one such gem. Included within the GNU Core Utilities, shuf allows you to generate random permutations of input data. Whether you need to shuffle lines in a file, create a random sample, or generate unique test data, shuf is the tool you didn’t know you needed.
Overview of shuf

shuf, short for “shuffle,” is a command-line utility designed for generating random permutations. Its primary function is to take input, which can be lines from a file, a range of numbers, or arguments provided directly on the command line, and output those items in a random order. What makes shuf truly ingenious is its simplicity and versatility. It seamlessly integrates with other command-line tools, making it a powerful component in complex data processing pipelines. Imagine needing to select a random subset of users for A/B testing, generating a random password, or simply shuffling a playlist – shuf makes all these tasks straightforward and efficient.
Installation of shuf

Since shuf is part of GNU Core Utilities, it’s highly likely that it’s already installed on your Linux or macOS system. If not, installation is usually very simple.
Linux
On most Debian/Ubuntu-based systems, you can install GNU Core Utilities (if it’s not already present) using the following command:
sudo apt-get update
sudo apt-get install coreutils
On Fedora/CentOS/RHEL-based systems:
sudo yum install coreutils
Or using DNF:
sudo dnf install coreutils
macOS
On macOS, if you don’t have it already, you’ll typically install GNU Core Utilities using Homebrew:
brew install coreutils
After installation on macOS, the GNU versions of these tools are often prefixed with `g`. Thus, you would use `gshuf` instead of `shuf`. To avoid this, you can add `/opt/homebrew/opt/coreutils/libexec/gnubin` to your `PATH` environment variable in your `.zshrc` or `.bashrc` file.
export PATH="/opt/homebrew/opt/coreutils/libexec/gnubin:$PATH"
Remember to source your shell configuration file after making changes:
source ~/.zshrc # or source ~/.bashrc
After installation, verify that shuf is available by running:
shuf --version
or if on macOS and you didn’t update the `PATH`:
gshuf --version
Usage: Examples with shuf

The beauty of shuf lies in its straightforward syntax and powerful options. Here are some practical examples to illustrate its usage:
Shuffling Lines in a File
This is perhaps the most common use case. To shuffle the lines in a file named `data.txt` and print the randomized output to the console, use:
shuf data.txt
To save the shuffled output to a new file:
shuf data.txt > shuffled_data.txt
Selecting a Random Sample
To select a specific number of random lines from a file, use the `-n` option:
shuf -n 5 data.txt
This will output 5 randomly selected lines from `data.txt`. This is incredibly useful for creating training datasets or sampling large log files.
Generating a Random Range of Numbers
shuf can generate a random permutation of a range of numbers using the `-i` option. For example, to generate a random order of numbers from 1 to 10:
shuf -i 1-10
This is valuable for generating random test cases or creating randomized experimental conditions.
Generating Unique Random Strings
Combine shuf with other tools to generate unique random strings. For example, to create a random string of 8 characters using characters from the alphabet:
tr -dc A-Za-z0-9 < /dev/urandom | head -c 8 | shuf | tr -d '\n'
This command uses tr to filter random characters from `/dev/urandom`, head to limit the output to 8 characters, shuf to shuffle the characters (though shuffling a string this short doesn’t have much effect), and tr -d '\n' to remove the newline character.
Using shuf with command-line arguments
You can also pass direct arguments to shuf:
shuf -e apple banana cherry date fig
This command will randomly output one of the provided fruits.
Repeating shuf output
The `-r` option allows shuf to output a random sample *with replacement*, meaning the same line can appear multiple times in the output.
shuf -n 5 -r data.txt
This will select 5 lines randomly from `data.txt`, potentially with duplicates.
Specifying a Seed
For repeatable random sequences, you can specify a seed using the `–random-source` option. While this doesn’t directly take a numerical seed, you can direct it to a file with a random number inside (created using `openssl rand -base64 8` or similar).
echo "12345" > seed_file.txt
shuf --random-source=seed_file.txt -i 1-10
Note that this approach is somewhat unconventional, and other tools like Python or `awk` might offer more direct seed control for pseudo-random number generation.
Tips & Best Practices for shuf
- Combine with other utilities:
shufshines when used in conjunction with tools likegrep,awk,sed, andxargsto create powerful data processing pipelines. - Handle large files efficiently: When working with extremely large files, consider using
shufin conjunction with tools likesplitto process the data in smaller chunks. - Be mindful of memory usage:
shufloads the entire input into memory before shuffling. For very large files, this can be a concern. If memory is an issue, explore alternative approaches like using a scripting language to implement a streaming shuffle algorithm. - Use `-n` for sampling: If you only need a subset of the data, the `-n` option is your friend. It significantly reduces the amount of data that
shufneeds to process, improving performance. - Understand the implications of `-r`: The `-r` (repeat) option changes the behavior from generating a permutation (where each item appears exactly once) to generating a random sample (where items can appear multiple times). Choose the appropriate option based on your needs.
Troubleshooting & Common Issues with shuf
- “shuf: command not found”: This indicates that
shufis not installed or not in your system’s PATH. Follow the installation instructions above. - “shuf: input file too large”: This error occurs when the input file exceeds the available memory. Try sampling with `-n` or processing the file in smaller chunks.
- Unexpected Output: Double-check your input data and command-line options. Ensure you are using the correct syntax and that your input file is formatted as expected (e.g., one item per line).
- macOS “gshuf” issues: If you’re on macOS and encountering issues with `gshuf`, verify that you’ve correctly updated your PATH environment variable as described in the Installation section.
FAQ About shuf
- Q: What’s the difference between `shuf data.txt` and `sort -R data.txt`?
- A: Both commands achieve a similar goal (randomizing the order of lines), but
shufis specifically designed for shuffling and is generally more efficient.sort -Ruses a hash function to generate a random order, which can be less uniformly random and potentially less performant, especially with very large files.shufalso has better options for sampling. - Q: Can I use `shuf` to generate a random password?
- A: Yes, you can, but it requires combining it with other tools. The example provided above demonstrates how to generate a random string of characters, which can be adapted to create a password of a desired length and character set.
- Q: Is `shuf` available on Windows?
- A:
shufis part of the GNU Core Utilities, which are primarily designed for Unix-like operating systems. However, you can accessshufon Windows through environments like Cygwin, Git for Windows (which includes a Bash environment), or the Windows Subsystem for Linux (WSL). - Q: How does `-n` option work if the requested sample size is larger than the input?
- A: If you request more lines using the `-n` option than are available in the input file, `shuf` will output all the lines from the file, but only that many lines. It will not repeat lines unless the `-r` flag is also given.
Conclusion
shuf is a remarkably useful and versatile command-line tool for generating random permutations. Its simplicity belies its power, making it an indispensable part of any command-line enthusiast’s toolkit. Whether you’re working with data analysis, scripting, or system administration, shuf can help you introduce randomness and generate unique results. So, dive in, experiment, and discover the myriad ways shuf can enhance your command-line workflows! For more information and advanced usage, visit the official GNU Core Utilities documentation.