brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: shuf picks random lines from a list or numbers from a range

Tools
~/tips/tip-tools-shuf-random-lines

shuf -n 3 films.txt

Shuffle lines, draw a few at random, or pick a random number.

Which of these twenty films tonight? I’m happy to let shuf, part of GNU coreutils, decide.

Give it a file and ask for a few lines:

shuf -n 3 films.txt

Without -n you get the whole file in random order. You can also hand it the choices on the command line, or a range of numbers:

shuf -n 1 -e pizza soup curry
shuf -i 1-100 -n 1

By default each line is used at most once, which suits drawing names for a secret Santa. Add -r to allow repeats, for example to roll a die ten times:

shuf -r -i 1-6 -n 10

It reads standard input too, so it fits at the end of a pipe. A sample of random files is a nice way to spot check a backup:

find /mnt/photos -type f | shuf -n 5

Then open those five and see that they load.

One warning: don’t write the result back to the file you are reading with >, because the shell empties it first. Use -o instead if you want to shuffle a file in place. This is for casual randomness, and I wouldn’t use it to make passwords or keys. macOS doesn’t ship it under that name.

Further reading: shuf invocation

More Tools tips xmllint --format feed.xml pdftotext -layout invoice.pdf invoice.txt qrencode -t ANSIUTF8 'https://example.com/a/long/path?id=12345' where python ffprobe -v error -show_entries format=duration -of default=nw=1:nk=1 input.mp4 All Tools tips (14) All topics

Link