Need Randomness? Unleash the Power of “shuf”!
Do you need to generate a random sample from a list? Perhaps you need to shuffle lines in a file for unbiased processing? The `shuf` command-line utility is your answer. Part of the GNU Core Utilities, `shuf` offers a simple yet powerful way to create random permutations of input, making it invaluable for data analysis, scripting, and various other tasks. Let’s dive into how `shuf` can revolutionize your workflow.
Overview

The `shuf` command, short for “shuffle,” is a command-line tool that generates random permutations of input. It’s an ingenious tool because it takes a common problem – the need for randomness or unbiased ordering – and solves it with remarkable efficiency and simplicity. Instead of writing complex scripts or relying on external libraries, `shuf` provides a direct and readily available solution. Its brilliance lies in its ability to transform a simple input (a file, a range of numbers, or standard input) into a randomly ordered output, making it perfect for tasks like randomly selecting participants for a contest, shuffling survey responses, or creating training datasets.
`shuf` is part of the GNU Core Utilities package, which is a standard component of most Linux distributions. This means that you likely already have `shuf` installed on your system. If not, the installation process is straightforward, as we will see in the next section.
Installation

As `shuf` is part of GNU Core Utilities, it’s usually pre-installed on most Linux distributions, including Ubuntu, Debian, Fedora, and CentOS. If for some reason it’s missing, you can install the `coreutils` package using your distribution’s package manager.
Here are the installation commands for some popular distributions:
- Debian/Ubuntu:
sudo apt update sudo apt install coreutils - Fedora/CentOS/RHEL:
sudo dnf install coreutils - macOS (using Homebrew):
brew install coreutils
After installation, you can verify that `shuf` is installed correctly by running:
shuf --version
This command should display the version information of the `shuf` utility.
Usage
`shuf` is incredibly versatile. Let’s explore some common use cases with practical examples:
1. Shuffling Lines in a File
This is perhaps the most common use case. Suppose you have a file named `names.txt` with a list of names, one name per line, and you want to shuffle the order of these names randomly.
shuf names.txt
This command will output the names from `names.txt` in a random order to the standard output. The original `names.txt` file remains unchanged.
2. Sampling from a File
You can use `shuf` to extract a random sample of lines from a file using the `-n` option, which specifies the number of lines to output.
shuf -n 5 names.txt
This command will output 5 random lines from the `names.txt` file. This is useful for selecting a random subset of data for testing or analysis.
3. Generating Random Numbers
`shuf` can also generate random numbers within a specified range using the `-i` option. The syntax is `-i start-end`.
shuf -i 1-10
This command will output a random permutation of the numbers from 1 to 10.
To generate a single random number within that range:
shuf -n 1 -i 1-10
This command will output one random number between 1 and 10.
4. Shuffling Input from Standard Input
`shuf` can also accept input from standard input, which allows you to pipe the output of other commands into `shuf` for shuffling.
ls -l | shuf
This command lists the files in the current directory using `ls -l` and then shuffles the output lines before displaying them.
5. Using `shuf` with other Utilities (Creating Training Data)
Imagine you have two files, `features.txt` and `labels.txt`, representing features and corresponding labels for a machine learning dataset. You want to split the data into training and validation sets, ensuring the data is shuffled to avoid any bias.
paste features.txt labels.txt | shuf | tee training_data.txt | head -n 80 > validation_data.txt
This command does the following:
- `paste features.txt labels.txt`: Merges the two files side by side, separated by a tab.
- `shuf`: Randomly shuffles the combined lines.
- `tee training_data.txt`: Copies the shuffled data to `training_data.txt` AND also sends the output to the next command.
- `head -n 80 > validation_data.txt`: Takes the first 80 lines (or adjust this number as desired for your validation dataset size) and saves them to `validation_data.txt`. The remaining data is already stored in `training_data.txt`.
This creates two datasets, `training_data.txt` and `validation_data.txt`, that are shuffled and ready for use in a machine learning pipeline.
6. Generating Random Passwords
While `shuf` isn’t a dedicated password generator, it can be combined with other tools to create reasonably secure, random passwords. This is more for simple scripts than for production-level password generation.
cat /dev/urandom | tr -dc A-Za-z0-9\!@#\$%\^\&\*()_+| head -c 16 | shuf | head -c 16
This command:
- `cat /dev/urandom`: Generates a stream of random bytes.
- `tr -dc A-Za-z0-9\!@#\$%\^\&\*()_+|`: Filters the random bytes, keeping only alphanumeric characters and special characters.
- `head -c 16`: Takes the first 16 characters.
- `shuf`: Shuffles these 16 characters. While seemingly redundant, it helps to further randomize the order.
- `head -c 16`: Takes the first 16 characters from the shuffled string (again, to handle potential edge cases).
Tips & Best Practices
- Understanding the `-n` option: When using the `-n` option for sampling, ensure that the value of `n` is not greater than the total number of lines in the input file. If `n` is larger, `shuf` will output all lines in the file in a random order.
- Using a Seed for Reproducibility: For reproducible results, you can use the `–random-source=FILE` option to specify a file containing random data or the `–seed=NUMBER` option to seed the random number generator. This is crucial for testing and debugging purposes. However, using a seed makes the output predictable and therefore unsuitable for security-sensitive applications.
shuf --seed=12345 names.txtThis will always shuffle names.txt in the same order, as long as the file remains unchanged.
- Handling Large Files: When dealing with very large files, `shuf` might consume a significant amount of memory. Consider using techniques like chunking the file into smaller pieces and shuffling each chunk separately before combining them. Alternatively, explore streaming solutions if possible.
- Combining with Other Tools: `shuf` shines when combined with other command-line tools like `awk`, `sed`, and `grep`. You can create powerful data processing pipelines by chaining these tools together.
Troubleshooting & Common Issues
- “shuf: invalid option”: This error usually indicates that you are using an older version of GNU Core Utilities that doesn’t support a specific option. Ensure that you have the latest version installed.
- “shuf: cannot open ‘filename’ for reading”: This error means that the specified file does not exist or that you do not have permission to read it. Double-check the filename and permissions.
- `shuf` seems to be stuck or taking a very long time: This can happen when shuffling extremely large files, or when using /dev/random (instead of /dev/urandom). If using /dev/random directly, the system might be waiting for more entropy. Try using `/dev/urandom` instead or consider alternative approaches for large datasets as mentioned earlier.
- Output isn’t truly random: While `shuf` uses a pseudorandom number generator, its output might not be suitable for cryptographic purposes or other applications that require high levels of randomness. For those cases, consider using dedicated random number generators. Also, always ensure sufficient entropy if sourcing random numbers from `/dev/random` (though, for most common shuffling tasks, `/dev/urandom` is sufficient).
FAQ
- Q: Is `shuf` available on Windows?
- A: `shuf` is primarily a Unix-like operating system tool. However, you can use it on Windows through environments like Cygwin, MSYS2, or the Windows Subsystem for Linux (WSL).
- Q: How can I shuffle a directory of files instead of lines in a file?
- A: You can combine `ls` with `shuf` to shuffle a directory of files: `ls | shuf`. This will list the files and directories and shuffle their names before outputting them.
- Q: Can I use `shuf` to generate a random sequence of characters?
- A: Yes, you can use `shuf` with `echo` and character ranges to achieve this: `echo {a..z} | tr ‘ ‘ ‘\n’ | shuf | head -n 10`. This will generate 10 random lowercase letters.
- Q: How can I ensure that the output of `shuf` is truly unpredictable?
- A: While `shuf` provides good randomness for most use cases, for security-sensitive applications, consider using a cryptographically secure random number generator (CSPRNG) and seeding it properly.
- Q: What’s the difference between `/dev/random` and `/dev/urandom`?
- A: `/dev/random` blocks until sufficient environmental noise is gathered, making it potentially slower but more cryptographically secure. `/dev/urandom` uses a pseudorandom number generator, which might be less secure in certain scenarios but is generally faster and suitable for most everyday uses.
Conclusion
`shuf` is a powerful and versatile command-line utility for generating random permutations. Its simplicity and integration into the GNU Core Utilities make it an indispensable tool for anyone working with data manipulation, scripting, or system administration. Whether you need to shuffle lines in a file, generate random numbers, or create unbiased datasets, `shuf` provides an efficient and elegant solution. So, go ahead and explore the possibilities – try `shuf` today and discover how it can streamline your workflow and bring a touch of randomness to your projects!