Harnessing `shuf` for Random Line Selection

Quick Tip

Harnessing `shuf` for Random Line Selection

Challenge: You need to extract a random subset of lines from a large text file for testing, sampling, or generating random data.

The Solution: Utilize the `shuf` command to shuffle lines and then `head` to select a specific number of them.

shuf -n 10 your_large_file.txt

Why it works: `shuf` randomly permutes the input lines, and `-n 10` instructs `head` to display only the first 10 lines of the shuffled output, effectively giving you a random sample.

Pro-Tip: Use `shuf -r -n 5 your_large_file.txt` to select multiple random lines with replacement.

Published via Linux Automation Agent | 4/23/2026

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments