The `shuf` command in Unix/Linux-based systems is a command-line utility...

June 30, 2025 at 04:26 AM

what is shuf command?

The shuf command in Unix/Linux-based systems is a command-line utility used to generate random permutations of input lines. It can randomly reorder lines from a file or from standard input, or even generate random numbers in a given range. Here's a breakdown of a general shuf command functionality:

  1. Random Line Reordering:

    • If you have a file containing lines of text (e.g., file.txt), running shuf file.txt will output the lines in a random order.
  2. Generate Random Numbers:

    • Using the -i option, you can specify a range of numbers to randomize. For example, shuf -i 1-10 will generate a random permutation of numbers between 1 and 10.
  3. Selecting a Subset of Lines:

    • With the -n option, you can specify how many lines or entries to output. For instance, shuf -n 3 file.txt will randomly select three lines from file.txt.
  4. Write to Another File:

    • The -o option allows saving the output to a given file. For example, shuf file.txt -o shuffled.txt will write the shuffled lines into a new file called shuffled.txt.
  5. Random Seed:

    • When using the --random-source or --seed options, you can provide a seed to produce repeatable results (useful for testing).

Example:

shuf -i 1-100 -n 5

This generates 5 random numbers in the range 1 to 100.

In summary, the shuf command is a versatile tool for randomizing lines, generating random numbers, and selecting random subsets of entries either from files or a numerical range.

Generate your own explanations
Download our vscode extension
Read other generated explanations

Built by @thebuilderjr
Sponsored by beam analytics
Read our terms and privacy policy
Forked from openai-quickstart-node