Need Randomness? How to Use the Shuf Command
In the world of data manipulation and scripting, the need for randomness often arises. Whether you’re shuffling data for analysis, selecting random samples, or simulating events, a reliable tool for generating random permutations is invaluable. Enter shuf
, a powerful command-line utility that offers a simple yet effective solution for randomizing input. This article will guide you through the ins and outs of shuf
, demonstrating its capabilities and practical applications.
Overview of Shuf

shuf
is a command-line tool that’s part of the GNU Core Utilities package, a collection of essential utilities found on most Linux and Unix-like operating systems. Its primary function is to generate random permutations of the input it receives. This input can be lines from a file, a range of numbers, or any other sequence of items. shuf
reads the input, shuffles it randomly, and then writes the shuffled output to standard output. The beauty of shuf
lies in its simplicity and versatility. It’s designed to be a single-purpose tool, doing one thing exceptionally well: shuffling data. This makes it easy to integrate into scripts and pipelines, where its output can be used as input for other commands.
What makes shuf
particularly ingenious is its efficient handling of large datasets. It doesn’t load the entire input into memory before shuffling; instead, it uses algorithms that allow it to shuffle data in place, minimizing memory usage. This makes it suitable for shuffling even very large files without performance bottlenecks. Furthermore, shuf
offers various options for controlling the shuffling process, such as limiting the number of output lines, specifying a random seed, and repeating the output.
Installation of Shuf

Since shuf
is part of the GNU Core Utilities, it’s typically pre-installed on most Linux distributions. However, if you find that it’s missing or need to update it, you can install it using your system’s package manager.
On Debian-based systems (like Ubuntu and Mint), you can install or update coreutils using:
sudo apt update
sudo apt install coreutils
On Red Hat-based systems (like Fedora and CentOS), use:
sudo yum install coreutils
Or, using `dnf` (on newer Fedora versions):
sudo dnf install coreutils
On macOS, if you don’t already have it, you will first need to install Homebrew.
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
After installing Homebrew, you can install GNU core utilities using:
brew install coreutils
This installs all the GNU core utilities, including shuf
, but they are prefixed with `g` to avoid conflicting with the BSD versions that come pre-installed. To use `gshuf` you will want to add the following to your `.zshrc` or `.bashrc` file.
alias shuf='gshuf'
After installation, you can verify that shuf
is installed correctly by running:
shuf --version
This should display the version number of the shuf
utility.
Usage: Shuf Examples

Now that you have shuf
installed, let’s explore some practical examples of how to use it.
1. Shuffling Lines from a File
The most common use case for shuf
is to shuffle the lines of a file. Suppose you have a file named `data.txt` with the following content:
apple
banana
cherry
date
fig
To shuffle the lines of this file and print the shuffled output to the console, use the following command:
shuf data.txt
This will output the lines of `data.txt` in a random order. For example:
date
apple
fig
cherry
banana
Each time you run this command, you’ll get a different random permutation of the lines.
2. Shuffling a Range of Numbers
shuf
can also generate random permutations of a range of numbers. To shuffle the numbers from 1 to 10, use the `-i` option:
shuf -i 1-10
This will output the numbers from 1 to 10 in a random order. For example:
7
3
1
9
2
5
8
4
10
6
3. Limiting the Number of Output Lines
Sometimes you don’t need to shuffle the entire input; you just want a random sample of a specific size. The `-n` option allows you to limit the number of output lines.
To select 3 random lines from `data.txt`, use:
shuf -n 3 data.txt
This will output 3 randomly selected lines from the file. For example:
banana
date
apple
Similarly, to select 5 random numbers from the range 1 to 20, use:
shuf -i 1-20 -n 5
4. Specifying a Random Seed
For reproducibility, you can specify a random seed using the `–random-source` option. This ensures that you get the same sequence of random numbers each time you run the command with the same seed.
First, create a random seed file.
head /dev/urandom | tr -dc A-Za-z0-9!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~ | head -c 256 > random_seed.txt
Then, use that `random_seed.txt` file in the following command. This is a more secure way of seeding the random number generator in shuf than just using `–random-source=1234`, for example.
shuf --random-source=random_seed.txt -i 1-10
This is helpful when you need to repeat an experiment or ensure that the random selection is consistent across multiple runs.
5. Repeating the Output
The `-r` or `–repeat` option allows shuf
to output the shuffled data repeatedly. This is useful for generating continuous streams of random data. Be careful as the output will continue indefinitely until interrupted.
shuf -r data.txt
This will continuously shuffle and output the lines from `data.txt`.
6. Shuffling Input from Standard Input
shuf
can also read input from standard input (stdin). This allows you to pipe the output of other commands into shuf
for shuffling. For example, to shuffle a list of files generated by `ls`, use:
ls | shuf
This will list the files in the current directory and then shuffle the order in which they are displayed.
7. Creating Random Passwords
A practical use of `shuf` is to generate random passwords. Here, we use `tr` to remove newlines, and limit the password length to 16 characters.
cat /dev/urandom | tr -dc A-Za-z0-9\!\@\#\$\%\^\&\*\(\)\_\+\=\-\`\~\{\}\[\]\:\"\;\<\>\,\.\?\/ | head -c 16
However, piping directly from `/dev/urandom` can be unpredictable. We can use `shuf` to create a more controlled and customized password generator:
chars="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_+=-`~{}[]:;<>,.?/"
shuf -n 16 -e $(echo $chars | sed 's/./& /g') | tr -d ' '
This command first defines a string of allowed characters. Then, it expands each character to be a separate argument to `shuf -e` using `sed`, which treats each character as a separate line for `shuf` to shuffle. Finally, the command selects 16 random characters and joins them to form a password, using `tr -d ‘ ‘` to remove spaces.
Tips & Best Practices for Shuf

- Understand Input Sources: Be clear about where
shuf
is getting its input. Whether it’s a file, a range of numbers, or standard input, ensure the input is in the format you expect. - Handle Large Files:
shuf
is efficient with large files, but it’s still a good practice to monitor memory usage if you’re dealing with extremely large datasets. - Use Seeds for Reproducibility: When you need repeatable results, always use the `–random-source` option to specify a random seed.
- Combine with Other Utilities:
shuf
is most powerful when combined with other command-line tools. Use pipes to create complex data manipulation pipelines. - Escape Special Characters: When using
shuf
with strings containing special characters, be sure to escape them properly to prevent unexpected behavior. - Be Mindful of Resource Usage: If you use `-r` be careful of resource consumption since it runs indefinitely until stopped.
Troubleshooting & Common Issues

- “shuf: command not found”: This error indicates that
shuf
is not installed or not in your system’s PATH. Double-check the installation steps and ensure that the directory containingshuf
is in your PATH environment variable. - Unexpected Output: If you’re getting unexpected output, review your input data and the options you’re using with
shuf
. Ensure that the input is in the correct format and that you’re using the options correctly. Pay attention to potential issues with newline characters or other special characters in the input. - “Invalid Argument” Error: This error usually occurs when you provide an invalid argument to
shuf
, such as an invalid range of numbers or an unrecognized option. Double-check the command syntax and ensure that all arguments are valid. - Slow Performance: While
shuf
is generally efficient, it can be slow with extremely large files or when used with complex pipelines. If you encounter slow performance, try optimizing your pipeline or consider using alternative tools for very large datasets.
FAQ: Shuf Command

- Q: Is
shuf
available on all operating systems? - A:
shuf
is part of the GNU Core Utilities, which are typically pre-installed on most Linux distributions. You may need to install coreutils separately on macOS using Homebrew. - Q: Can I use
shuf
to shuffle non-text data? - A:
shuf
is designed to shuffle lines of text. To shuffle binary data, you might need to use other tools or write a custom script. - Q: How can I use
shuf
in a shell script? - A: You can directly include
shuf
commands in your shell scripts, just like any other command-line utility. Use pipes and variables to integrateshuf
into your script’s workflow. - Q: How do I generate truly random numbers with shuf?
- A: For security-sensitive applications, use a cryptographically secure random number generator as the source for the seed, such as reading from `/dev/urandom` (as demonstrated in the example above). Ensure that you understand the security implications of randomness when using it for cryptography or other sensitive applications.
Conclusion
shuf
is a versatile and powerful command-line tool for generating random permutations of input. Its simplicity, efficiency, and flexibility make it a valuable addition to any data scientist’s or system administrator’s toolkit. Whether you’re shuffling data for analysis, selecting random samples, or generating random passwords, shuf
provides a reliable and efficient solution.
Now that you’ve learned the basics of shuf
, it’s time to put it to the test. Experiment with different options, integrate it into your scripts, and discover new ways to leverage its capabilities. Visit the GNU Core Utilities website for more information and documentation. Happy shuffling!