Need Randomness? Unleash the Power of `shuf`
Have you ever needed to randomly shuffle lines in a file, pick a random winner from a list, or generate a random sequence of numbers for testing? The `shuf` command-line tool, a part of GNU Core Utilities, provides a simple yet powerful way to create random permutations of input. It’s a surprisingly versatile tool that can be a lifesaver in various scripting and data manipulation scenarios. This article will guide you through installing and using `shuf` to add randomness to your workflows.
Overview

The `shuf` command is designed to take input—either from a file or standard input—and produce a random permutation of those lines or numbers on the standard output. What makes `shuf` ingenious is its simplicity and efficiency. Instead of writing complex scripts to shuffle data, you can use a single, well-optimized command. It’s particularly useful in scenarios where you need to select a random sample, randomize the order of items for a survey, or introduce randomness into automated processes. Because it’s part of GNU Core Utilities, it’s available by default on most Linux distributions, making it readily accessible.
Installation
Since `shuf` is part of GNU Core Utilities, it’s typically pre-installed on most Linux distributions. However, if for some reason it’s missing, or you want to ensure you have the latest version, you can install it using your distribution’s package manager.
Debian/Ubuntu:
sudo apt update
sudo apt install coreutils
Fedora/CentOS/RHEL:
sudo dnf install coreutils
macOS (using Homebrew):
brew install coreutils
After installing on macOS, you may need to use `gshuf` instead of `shuf` to call the GNU version. You can also add /opt/homebrew/opt/coreutils/libexec/gnubin to your PATH to use the standard `shuf` command name.
Once installed, you can verify its presence by checking the version:
shuf --version
Usage
The `shuf` command offers various options for controlling how it shuffles and what it shuffles. Here are some practical examples:
1. Shuffling Lines from a File
The most basic usage is to shuffle the lines of a file. Let’s say you have a file named `names.txt` containing a list of names, one name per line:
cat names.txt
# Output:
Alice
Bob
Charlie
David
Eve
To shuffle these names randomly and print them to the console, use:
shuf names.txt
# Example Output (will vary on each run):
David
Eve
Bob
Charlie
Alice
2. Generating a Random Number Sequence
`shuf` can also generate random sequences of numbers within a specified range. The `-i` option defines the inclusive range. For example, to generate a random permutation of the numbers 1 through 10:
shuf -i 1-10
# Example Output (will vary):
7
3
9
1
5
8
4
2
10
6
3. Selecting a Random Sample
To select a random sample of a specific size from a larger dataset, use the `-n` option followed by the number of samples you want. For instance, to randomly select 3 names from `names.txt`:
shuf -n 3 names.txt
# Example Output (will vary):
Charlie
Eve
Alice
4. Shuffling Standard Input
`shuf` can also take input from standard input (stdin). This is useful for piping data from other commands. For example, you can generate a list of numbers using `seq` and then shuffle them:
seq 1 20 | shuf
# Example Output (will vary):
18
3
15
7
1
10
20
16
11
5
9
13
4
14
8
2
19
12
6
17
5. Controlling the Random Seed
For reproducibility, you can control the random seed using the `–random-source` option along with a file containing random data. However, a simpler approach is to use the `–seed` option, although it’s not part of the POSIX standard and may not be available on all systems (but is on GNU `shuf`):
shuf --seed 123 -i 1-5
# Output (will be the same every time with the same seed):
4
3
2
5
1
Using the same seed will produce the same sequence, which is very valuable for testing and debugging purposes.
6. Shuffling with a Specific Output File
To save the shuffled output to a file instead of printing it to the console, use standard output redirection:
shuf names.txt > shuffled_names.txt
This will create a new file named `shuffled_names.txt` containing the randomly shuffled names.
Tips & Best Practices
- Use seeds for reproducibility: When you need predictable results, especially for testing, always use the `–seed` option to control the random number generation.
- Handle large files efficiently: `shuf` loads the entire input into memory, so for extremely large files, consider alternative approaches like streaming shuffles or using databases. For very large datasets, it may not be the optimal solution.
- Combine with other utilities: `shuf` shines when combined with other command-line tools like `seq`, `awk`, `grep`, and `xargs` to create complex data processing pipelines.
- Be mindful of line endings: `shuf` treats each line as a separate item to be shuffled. Ensure that your input file has consistent line endings (e.g., LF on Linux/macOS, CRLF on Windows) to avoid unexpected behavior.
- Understand the limitations: `shuf` is not a cryptographically secure random number generator. Do not use it for security-sensitive applications. For such applications, use tools like `/dev/urandom` or specialized cryptographic libraries.
Troubleshooting & Common Issues
- `shuf: invalid option` error: This usually indicates that the option you’re using is not supported by your version of `shuf`. Check the `shuf` manual page (`man shuf`) for the available options. This is especially common with the `–seed` option, which isn’t part of POSIX.
- `shuf: memory exhausted` error: This means that `shuf` ran out of memory while trying to load the input. This can happen with extremely large files. Consider using a different approach or processing the file in smaller chunks.
- Unexpected shuffling behavior: If you’re not getting the expected randomness, ensure that your input data is correctly formatted (e.g., one item per line) and that you haven’t accidentally introduced any biases in your data.
- macOS: `command not found: shuf`: If you installed with Homebrew, remember to use `gshuf` or update your PATH.
- Script hangs indefinitely: Ensure that the input to `shuf` is finite. If `shuf` is reading from a continuously generating stream, it will wait indefinitely for the input to complete. Use `head`, `tail`, or other tools to limit the input size.
FAQ
- Q: Can I use `shuf` to shuffle characters within a string instead of lines?
- A: No, `shuf` shuffles lines (or numbers). To shuffle characters within a string, you’ll need to use other tools like `sed`, `awk`, or scripting languages like Python or Perl.
- Q: Is `shuf` suitable for generating random passwords?
- A: No, `shuf` uses a pseudo-random number generator and is not cryptographically secure. For generating secure passwords, use dedicated password generation tools or libraries that employ cryptographically secure random number generators.
- Q: How can I shuffle multiple files together?
- A: You can concatenate the files using `cat` and then pipe the output to `shuf`: `cat file1.txt file2.txt file3.txt | shuf`.
- Q: Can I use `shuf` to generate unique random numbers?
- A: Yes, by using the `-i` option to specify a range and then shuffling that range. The output will be a random permutation of the numbers in the range, guaranteeing uniqueness within that permutation.
- Q: How can I select multiple random samples without replacement?
- A: `shuf` with `-n` selects *one* random sample of the specified size. If you want *multiple* different random samples, you need to re-run `shuf` each time, potentially excluding previously selected items. Alternatively, you would need a more complex script to manage the selection process.
Conclusion
`shuf` is a surprisingly powerful and versatile command-line utility for generating random permutations. Its simplicity and efficiency make it an excellent choice for a wide range of tasks, from shuffling lines in a file to generating random number sequences. While it has limitations, particularly with very large files and security-sensitive applications, it remains a valuable tool in any command-line user’s arsenal. Experiment with `shuf` and discover how it can streamline your data manipulation workflows. Visit the GNU Core Utilities page for more information and the official documentation: GNU Core Utilities.