Need Randomness? Master the `shuf` Command!
In the world of data manipulation and scripting, the ability to generate random sequences is invaluable. Whether you’re creating test data, picking a random winner in a contest, or simulating real-world events, you need a reliable tool. Enter `shuf`, a powerful command-line utility that provides a simple yet effective way to shuffle lines in a file or generate random sequences.
This article will guide you through the ins and outs of the `shuf` command, demonstrating its versatility and providing practical examples to get you started. Get ready to unlock the power of randomness with `shuf`!
Overview

The `shuf` command is part of the GNU Core Utilities package and is designed to generate random permutations of input data. It reads input from a file or standard input, shuffles the lines, and writes the shuffled output to standard output. What makes `shuf` so ingenious is its simplicity and efficiency. It’s a single-purpose tool that excels at its job, making it a valuable addition to any data wrangler’s toolkit. Unlike more complex scripting solutions, `shuf` provides a straightforward, performant way to introduce randomness into your workflows. No need for complex coding when you have this little gem!
Think of it as a digital deck of cards – you feed it a list, and it gives you back a randomized order. This is incredibly useful for tasks where you need unpredictability, like selecting random samples, creating randomized test cases, or even just surprising your colleagues with a randomly generated “employee of the month” list.
Installation

Since `shuf` is part of GNU Core Utilities, it’s likely already installed on most Linux and macOS systems. However, if you find that it’s missing, here’s how you can install it:
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, you might need to use `gshuf` instead of `shuf` on macOS, as macOS might have its own (potentially outdated) version of `shuf`. To avoid confusion, add the GNU version to your `PATH`:
echo 'alias shuf=gshuf' >> ~/.bashrc # Or ~/.zshrc, depending on your shell
source ~/.bashrc # Or source ~/.zshrc
To verify the installation, run:
shuf --version
This should display the version information for `shuf`, confirming that it’s installed and accessible.
Usage

The `shuf` command offers a variety of options to control its behavior. Let’s explore some common use cases with practical examples:
Shuffling Lines from a File
The most basic usage of `shuf` is to shuffle the lines of a file. Create a text file named `names.txt` with the following content:
Alice
Bob
Charlie
David
Eve
Now, shuffle the lines using:
shuf names.txt
The output will be a random permutation of the names, like this (the exact order will vary):
Charlie
Alice
Eve
David
Bob
The original `names.txt` file remains unchanged. The shuffled output is sent to standard output.
Shuffling a Range of Numbers
`shuf` can also generate a random sequence of numbers within a specified range using the `-i` option. For example, to generate a random permutation of numbers from 1 to 10:
shuf -i 1-10
This might produce output like:
7
3
1
9
5
2
8
10
6
4
This is useful for generating test data or creating randomized indexes.
Sampling Without Replacement
By default, `shuf` shuffles the entire input. However, you can use the `-n` option to select a specific number of random lines without replacement. This is useful for creating a random sample from a larger dataset. For example, to select 3 random names from `names.txt`:
shuf -n 3 names.txt
This might output:
David
Alice
Bob
Each name will appear at most once in the output.
Sampling With Replacement
To sample with replacement (where the same line can be selected multiple times), combine the `-n` option with the `–random-source` option and provide a file with random numbers. The easiest way to do this is to use `/dev/urandom` on Linux/macOS.
shuf -n 3 --random-source=/dev/urandom names.txt
This might produce:
Bob
Eve
Bob
Notice that “Bob” appears twice in the output.
Reading from Standard Input
`shuf` can also read from standard input, which allows you to pipe the output of other commands into it. For example, to shuffle a list of files generated by `ls`:
ls -l | shuf
This will list the files in the current directory in a random order. This is a good way to introduce unpredictability into scripts.
Writing to a File
To save the shuffled output to a file, use the redirection operator `>`:
shuf names.txt > shuffled_names.txt
This will create a new file named `shuffled_names.txt` containing the shuffled names.
Generating Random Passwords
`shuf` can be used to generate random passwords by shuffling a set of characters. For example:
echo "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*" | shuf -n 16 | tr -d ' '
This command generates a 16-character random password by shuffling the characters in the echo’d string and then removing any spaces. This can be integrated into password generation scripts.
Tips & Best Practices
- Understand the Randomness Source: By default, `shuf` uses a pseudo-random number generator (PRNG). For security-sensitive applications, consider using a true random number generator (TRNG) like `/dev/urandom` (as shown in the sampling with replacement example).
- Handle Large Files: `shuf` loads the entire input into memory. For very large files, consider alternative approaches like splitting the file into smaller chunks, shuffling each chunk, and then concatenating the results, or using a stream-based approach.
- Combine with Other Utilities: `shuf` shines when combined with other command-line tools like `awk`, `sed`, and `grep`. Use pipes to create powerful data manipulation pipelines.
- Use the `–head-count` argument for sampling: This argument is more explicit compared to using `-n`, and it makes the code more readable.
- Test Your Scripts: Always test your scripts thoroughly to ensure that `shuf` is behaving as expected, especially when dealing with sensitive data.
- Be Aware of Locale: The sorting behavior of `shuf` can be affected by the current locale. Ensure that your locale is set appropriately if you need consistent sorting behavior.
Troubleshooting & Common Issues
- `shuf: command not found`: This indicates that `shuf` is not installed or not in your system’s PATH. Follow the installation instructions above.
- `shuf: memory exhausted`: This error occurs when `shuf` tries to load a file that is too large for available memory. Consider using techniques for handling large files as described above.
- Incorrect output: Double-check your command-line options and input data. Ensure that the file paths are correct and that the input data is in the expected format.
- macOS specific issues: Remember to use `gshuf` instead of `shuf` on macOS if you installed it via `brew`. If you forget, you might get different results or errors from the system `shuf`.
FAQ
- Q: Can I use `shuf` to generate random numbers with decimals?
- A: No, `shuf` is designed to shuffle lines or generate integers within a range. For generating random decimal numbers, consider using tools like `awk` or scripting languages like Python or Perl.
- Q: Is `shuf` cryptographically secure?
- A: No, `shuf`’s default pseudo-random number generator is not suitable for cryptographic purposes. For security-sensitive applications, use a true random number generator (TRNG) and appropriate cryptographic libraries.
- Q: How can I shuffle lines in place (i.e., modify the original file)?
- A: `shuf` does not directly support in-place shuffling. You can achieve this by redirecting the output to a temporary file and then replacing the original file with the temporary file using `mv`. Be careful with this approach, as data loss can occur if the process is interrupted.
- Q: Can I exclude certain lines from being shuffled?
- A: Yes, you can use `grep -v` to exclude certain lines before passing the output to `shuf`. For example, `grep -v “exclude_pattern” input.txt | shuf` will shuffle all lines in `input.txt` except those containing “exclude_pattern”.
- Q: How can I generate random numbers with a specific seed for reproducibility?
- A: `shuf` itself doesn’t offer a seed option. However, you can pipe random data from other utilities that do support seeding. For example using `openssl` on linux you could generate a seed and pipe it to `shuf` with `openssl rand -base64 10 | shuf -i 1-10` (this isn’t a proper use of seed, but demonstrates that it can be combined with other tools)
Conclusion
The `shuf` command is a simple yet powerful tool for generating random permutations of data. Its ease of use and versatility make it an essential utility for any command-line enthusiast. Whether you’re shuffling lines in a file, generating random numbers, or creating randomized test data, `shuf` has you covered. So, dive in, experiment with its options, and discover the many ways it can simplify your data manipulation tasks. Give `shuf` a try and inject some randomness into your workflows today!
For more information and advanced usage, visit the official GNU Core Utilities documentation.