Need Random Data? Harness the Power of Shuf!
In the world of data manipulation and scripting, the ability to generate random samples or shuffle data is often invaluable. Whether you’re creating test data, simulating scenarios, or simply need to randomize a list, the shuf
command-line utility provides a simple yet powerful solution. This article will guide you through the ins and outs of shuf
, demonstrating its versatility and how it can streamline your workflows.
Overview

shuf
, part of the GNU Core Utilities, is a small but ingenious command-line tool designed for creating random permutations of its input. Think of it as a digital card shuffler. It reads lines from a file or generates a sequence of numbers and then outputs them in a random order. What makes shuf
particularly clever is its simplicity and efficiency. It doesn’t require complex algorithms or extensive configuration; it just shuffles data. This makes it ideal for scripting, data analysis, and various other tasks where randomness is needed. Unlike more complex scripting solutions, shuf
specializes in one specific task and excels at it, offering a fast and reliable way to randomize data. This single-purpose design philosophy aligns well with the Unix tradition of small, focused tools that can be combined to accomplish larger tasks.
Installation

Since shuf
is part of the GNU Core Utilities, it’s typically pre-installed on most Linux distributions and macOS. However, if you find that it’s missing or need to install it on another system, here are the steps:
Linux (Debian/Ubuntu):
sudo apt update
sudo apt install coreutils
Linux (Fedora/CentOS/RHEL):
sudo dnf install coreutils
macOS (using Homebrew):
brew install coreutils
After installation on macOS, the command might be available as gshuf
to avoid conflicts with existing system utilities. You can create an alias if you prefer to use the shuf
command.
alias shuf=gshuf
Add this alias to your .bashrc
or .zshrc
file to make it permanent.
Once installed, verify by checking the version:
shuf --version
Usage

The shuf
command offers several options for controlling its behavior. Here are some common use cases with examples:
1. Shuffling Lines from 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:
cat names.txt
Alice
Bob
Charlie
David
Eve
To shuffle the lines in this file, simply run:
shuf names.txt
The output will be a random permutation of the names. Each time you run the command, the order will be different.
2. Generating a Random Sample
You can use the -n
option to select only a specified number of lines from the input. For example, to select 3 random names from names.txt
:
shuf -n 3 names.txt
This will output 3 randomly selected names from the list.
3. Generating a Random Sequence of Numbers
The -i
option allows you to specify a range of integers to shuffle. For example, to generate a random sequence of numbers from 1 to 10:
shuf -i 1-10
This will output the numbers 1 through 10 in a random order.
4. Repeating the Shuffle
By default, shuf
shuffles without replacement. If you want to allow the same item to be selected multiple times, you can use the -r
option. This effectively creates a shuffle with replacement.
shuf -r -n 5 names.txt
This will output 5 randomly selected names, but names can be repeated.
5. Specifying a Seed for Reproducibility
Sometimes, you need to generate the same random sequence multiple times. The --random-source
option allows you to specify a file containing random data (typically /dev/urandom
), and the --seed
option allows you to set a seed value. If you use the same seed, you’ll get the same output.
shuf --seed 1234 -i 1-5
Running this command multiple times with the same seed (1234) will produce the same shuffled sequence.
6. Using Shuf with Pipes
shuf
can be easily integrated into pipelines. For instance, you can combine it with other commands like ls
to shuffle a list of files:
ls -l | shuf | head -n 5
This command lists all files in the current directory, shuffles the list, and then displays the first 5 lines.
7. Shuffling Input from Standard Input
You can pipe data directly to shuf
from standard input. For example:
echo -e "apple\nbanana\ncherry" | shuf
This will shuffle the list of fruits and output them in a random order.
8. Shuffling lines using character ranges
Instead of integers you can use character ranges to generate a random alphanumeric password:
shuf -n 16 -i a-z -i 0-9 | tr -d '\n'
This will generate a 16-character password from lower-case letters and numbers.
Tips & Best Practices

- Understand the Input: Always be aware of the format of your input data.
shuf
treats each line as a separate item to be shuffled. - Use Seeds for Reproducibility: If you need to repeat a specific random sequence, use the
--seed
option to set a seed value. This is useful for testing and debugging. - Be Mindful of Resource Usage: When shuffling large files,
shuf
may consume significant memory. Consider using other tools or techniques for extremely large datasets. - Combine with Other Utilities:
shuf
is most powerful when combined with other command-line utilities likegrep
,awk
,sed
, andxargs
to create complex data processing pipelines. - Error Handling: When working with input files, make sure the file exists and is readable. Use error checking in your scripts to handle potential issues.
- Use
shuf
for Non-Critical Security Tasks: Whileshuf
provides randomness, it may not be suitable for generating cryptographic keys or other highly sensitive data. Consider dedicated tools designed for cryptographic purposes for such tasks.
Troubleshooting & Common Issues

- “shuf: command not found”: This indicates that
shuf
is not installed or not in your system’s PATH. Follow the installation steps above. - Incorrect Output Order: If you’re getting the same output order every time, make sure you’re not using the same seed value repeatedly. If you don’t intend to use a seed, remove the
--seed
option. - “shuf: standard input: cannot seek”: This error can occur when you try to shuffle data from a non-seekable input source (e.g., a pipe from another command). In such cases, you may need to save the input to a temporary file first.
- Memory Errors with Large Files: If you’re shuffling a very large file and running out of memory, consider splitting the file into smaller chunks and shuffling each chunk separately.
- Unexpected Behavior with Newlines: Ensure your input data uses consistent newline characters (
\n
). Inconsistent newlines can lead to unexpected shuffling results. - Permission Denied: If you encounter permission issues when running
shuf
, check the permissions of the input file and the directory where you’re running the command. Usechmod
to adjust permissions if needed.
FAQ

- Q: What is the difference between
shuf
andsort -R
? - A: Both commands can shuffle data, but
shuf
is generally faster and more memory-efficient for large datasets.sort -R
relies on thesort
utility, which is designed for sorting, not just shuffling. - Q: Can I use
shuf
to generate a random password? - A: Yes, you can use
shuf
to generate simple random passwords, but for more secure password generation, consider using dedicated tools likeopenssl rand
orpwgen
. - Q: How can I shuffle lines in a file and save the result to a new file?
- A: You can redirect the output of
shuf
to a new file using the>
operator:shuf input.txt > output.txt
. - Q: Is
shuf
available on Windows? - A:
shuf
is not a native Windows command. However, you can use it within the Windows Subsystem for Linux (WSL) or by installing Cygwin or Git for Windows, which provide a Unix-like environment. - Q: How can I ensure the seed I use with `–seed` creates truly unpredictable results?
- A: While `–seed` guarantees reproducibility for a given seed, the initial seed itself should be unpredictable. Don’t hardcode a predictable seed value for anything security-related; instead, use random data from `/dev/urandom` to generate the seed itself.
Conclusion
The shuf
command is a valuable tool for anyone working with data on the command line. Its simplicity, efficiency, and versatility make it ideal for a wide range of tasks. From generating random samples to shuffling data for simulations, shuf
can significantly streamline your workflows. So, why not give shuf
a try? Explore its capabilities and discover how it can simplify your data manipulation tasks. Visit the GNU Core Utilities documentation for more information and advanced usage examples.