Home > bash > randomize-lines


There is a useful command in the randomize-lines package namely “rl“. It reads lines from the standard input and shuffles them. It can also be used to pick a random line from a text file.

Let’s see some examples from the man:

Play a random sound after 4 minutes (perfect for toast):

sleep 240 ; play `find /sounds -name ´*.au´ -print | rl --count=1`

Play the 15 most recent .mp3 files in random order.

ls -c *.mp3 | head -n 15 | rl | xargs --delimiter=´\n´ play

Roll a dice:

seq 6 | rl --count 2

Roll a dice 1000 times and see which number comes up more often:

seq 6 | rl --reselect --count 1000 | sort | uniq -c | sort -n

Shuffle the words of a sentence:

echo -n "The rain in Spain stays mainly in the plain." \
| rl --delimiter=´ ´;echo

Find all movies and play them in random order.

find . -name ´*.avi´ -print0 | rl -0 | xargs -n 1 -0 mplayer

Because -0 is used filenames with spaces (even newlines and other unusual characters) in them work.

Categories: bash Tags: , ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: