Need Randomness? Unleash the Power of Shuf!
Have you ever needed to randomly shuffle the lines in a file, select a random sample from a list, or generate a deck of cards for a script? The `shuf` command-line tool is your answer. Part of the GNU Core Utilities, `shuf` provides a simple yet powerful way to generate random permutations of input data. It’s an indispensable tool for scripting, data analysis, and anywhere you need a touch of randomness.
Overview: Shuf – The Random Permutation Master

`shuf` is a command-line utility designed to generate random permutations of its input. Unlike more complex scripting solutions, `shuf` offers a dedicated, optimized approach to shuffling data. Its ingenuity lies in its simplicity and efficiency. It takes input from a file or standard input, then outputs a random rearrangement of those lines to standard output. This can be surprisingly useful in a wide range of situations, from selecting winners in a raffle to creating randomized test cases.
Installation: Getting Shuf on Your System

As part of the GNU Core Utilities, `shuf` is likely already installed on most Linux and Unix-like systems. You can quickly verify this by typing `shuf –version` in your terminal. If it’s not installed (or you want the latest version), the installation process depends on your operating system:
- Debian/Ubuntu:
sudo apt update sudo apt install coreutils
- Fedora/CentOS/RHEL:
sudo dnf install coreutils
- macOS (using Homebrew):
brew install coreutils
Note that on macOS, the `shuf` command might be prefixed with `g`, like `gshuf`, to avoid conflicts with other system utilities.
After installation, confirm it’s working by checking the version again.
Usage: Unleashing Shuf’s Potential

`shuf` offers a variety of options to control its behavior. Let’s explore some common use cases with practical examples.
1. Shuffling Lines in a File
The most basic use case is shuffling the lines of a file. Let’s say you have a file named `names.txt`:
Alice
Bob
Charlie
David
Eve
To shuffle these names, use the following command:
shuf names.txt
This will output a random permutation of the names to your terminal. Each time you run the command, you’ll get a different order.
2. Shuffling from Standard Input
`shuf` can also take input from standard input (stdin). This is useful when piping output from other commands. For example, to shuffle a list of files in a directory:
ls | shuf
This will list all files and directories in the current directory and then shuffle the order in which they are displayed.
3. Selecting a Random Sample
You can use the `-n` option to specify the number of lines to output. This is great for selecting a random sample from a larger dataset. For example, to select 3 random names from `names.txt`:
shuf -n 3 names.txt
This will output 3 randomly selected names from the file.
4. Generating a Random Sequence of Numbers
`shuf` can generate a sequence of numbers and then shuffle them. The `-i` option specifies a range of numbers. For example, to generate a random permutation of the numbers 1 to 10:
shuf -i 1-10
This will output a random arrangement of the numbers 1 through 10, each on a new line.
5. Generating a Random Deck of Cards
Here’s a more complex example that demonstrates `shuf`’s power in creating a random deck of cards:
suits=(Hearts Diamonds Clubs Spades)
ranks=(2 3 4 5 6 7 8 9 10 Jack Queen King Ace)
declare -a deck
for suit in "${suits[@]}"; do
for rank in "${ranks[@]}"; do
deck+=("$rank of $suit")
done
done
shuf -e "${deck[@]}"
This script first defines arrays for suits and ranks. Then, it creates a deck of cards by combining each rank with each suit. Finally, it uses `shuf -e` (treat each argument as a separate input line) to shuffle the deck and output the randomized order.
6. Controlling Randomness with a Seed
For reproducibility, you can control the random number generator with the `–random-source` option. This specifies a file to use as a source of randomness. For consistent results across multiple runs, especially useful in testing, you can create a fixed source file. However, note this drastically reduces randomness and is primarily for testing or deterministic simulations. An alternative approach is to use `$RANDOM` which is already part of bash. An example is as follows:
SHUF_RANDOM_SOURCE=/dev/urandom shuf -i 1-10 # Using /dev/urandom (or similar secure source)
# The below are not recommended in production unless you understand security implications
# echo "12345" > random_seed.txt
# SHUF_RANDOM_SOURCE=random_seed.txt shuf -i 1-10 # Uses a fixed seed, making it predictable.
Tips & Best Practices for Using Shuf
- Handle Large Files Efficiently: `shuf` loads the entire input into memory. For extremely large files, consider alternative approaches like splitting the file into smaller chunks and shuffling them separately, or using tools specifically designed for large-scale data processing.
- Be Mindful of Newlines: `shuf` treats each line as a separate item to shuffle. If your data contains embedded newlines, it might not be shuffled as expected. Pre-process your data to handle such cases.
- Use `-e` for Arguments: The `-e` option is crucial when you want to treat each argument passed to `shuf` as a separate input line. This is common when working with arrays or variables containing multiple values, as demonstrated in the deck of cards example.
- Consider Security Implications: If you need cryptographically secure randomness (e.g., for generating keys or passwords), `shuf` is not the right tool. Use dedicated cryptographic libraries or tools designed for that purpose.
- Combine with Other Utilities: `shuf` shines when combined with other command-line utilities like `grep`, `sed`, `awk`, and `sort`. This allows you to build powerful data processing pipelines with a touch of randomness.
Troubleshooting & Common Issues
- `shuf: memory exhausted` Error: This indicates that `shuf` is running out of memory because the input file is too large. Consider using alternative methods for shuffling very large files, as mentioned in the “Tips & Best Practices” section.
- Unexpected Output: If you’re not getting the expected results, double-check your input data, especially for embedded newlines or unexpected characters. Also, verify that you’re using the correct options, such as `-e` for treating arguments as separate lines.
- `command not found: shuf` Error: This means that `shuf` is not installed or not in your system’s PATH. Follow the installation instructions provided earlier in this article. If it’s installed but not found, ensure that the directory containing `shuf` is included in your PATH environment variable.
- macOS Specifics: Remember that on macOS, the command might be `gshuf` if you installed it with Homebrew. Adjust your commands accordingly.
FAQ: Your Shuf Questions Answered
- Q: Can `shuf` shuffle directories?
- A: Yes, you can use `ls | shuf` to shuffle the list of files and directories in the current directory.
- Q: How can I save the shuffled output to a file?
- A: Use the redirection operator `>` to save the output to a file. For example: `shuf names.txt > shuffled_names.txt`.
- Q: Is `shuf` suitable for generating random numbers for cryptography?
- A: No, `shuf` uses a pseudo-random number generator that is not cryptographically secure. Use dedicated cryptographic libraries or tools for that purpose.
- Q: How can I select the same random sample every time?
- A: While `shuf` doesn’t have a direct option for a seed, you can influence randomness by indirectly controlling the source used for randomness as demonstrated by the example using a fixed random source file. However, be very cautious about the security implications.
- Q: Can I use `shuf` within a shell script?
- A: Absolutely! `shuf` is designed to be used within scripts, making it a versatile tool for automating tasks that require randomness.
Conclusion: Embrace the Randomness!
`shuf` is a surprisingly powerful and versatile tool for generating random permutations in the command line. Whether you need to shuffle lines in a file, select a random sample, or create a randomized deck of cards, `shuf` has you covered. Its simplicity and efficiency make it an invaluable addition to any developer’s or system administrator’s toolkit. So, go ahead, experiment with `shuf`, and discover the many ways it can add a touch of randomness to your workflow! Visit the GNU Core Utilities documentation to learn more about `shuf` and its full range of capabilities.