tr: With a sprinkle of /dev/urandom and head

I learned this a while back and use it occasionally when I need random text, for file names or just for filler.

tr is part of coreutils in some systems, and short of text stream manipulation, you might not hear about it.

But here’s a small trick you can do with it.

< /dev/urandom tr -dc A-Za-z0-9

You should get a high speed stream of …


Or something like it. Press CTRL+C to get your life back.

What the heck just happened? You just generated random text, in only uppercase and lowercase letters, numbers.

No nonprintable, special or control characters. Nothing from extended character sets or command sequences. Just random noise, filtered down to basic characters.

“Well gee,” you’re thinking. “Thanks for that clever little tip, K.Mandla.” 🙄

Yes, it is a clever little tip, because with this you can …

< /dev/urandom tr -dc A-Za-z0-9 | head -c10 ; echo

Generate random passwords for new user accounts, without relying on the Internet.

What else? How about …

mv testfile.txt "`< /dev/urandom tr -dc A-Za-z0-9 | head -c8`.txt"

Scramble file names.

Taking it a step further …

tar -cvvf "`< /dev/urandom tr -dc A-Za-z0-9 | head -c8`.tar" testfile.txt

Package files with random names in one quick swoop … if you’re paranoid, of course.

Don’t stop there. Dump exactly a specific number of printable characters into a file. Make text noise for statistical purposes, or for graphic effect. Generate six-digit hexadecimal values and force-feed them into xsetroot. Collect only certain numbers. Only lowercase letters. Make fake phone numbers to give to creepy guys. The list goes on.

This isn’t much in the way of a tutorial for tr, or head for that matter, and I didn’t want it to be. There are plenty of sites out there that can teach you more.

This just shows a fairly basic task that is made very easy with a collection of fundamental tools. How you use it is up to you. 🙂

9 thoughts on “tr: With a sprinkle of /dev/urandom and head

  1. imgx64

    For random passwords, I usually use “apg”. It can even generates “pronounceable” passwords which are much easier to remember.

  2. Pingback: shuf: Random yet intelligible | Inconsolation

  3. Pingback: gnupg: Symmetric encryption and decryption | Inconsolation

  4. Pingback: apg: Pronounced, “ay-pee-jee” | Inconsolation

  5. Pingback: Bonus: 2013 in review | Inconsolation

  6. Pingback: mcookie: Mmm, cookies! | Inconsolation

  7. Pingback: otp: Not as simple as it looks | Inconsolation

  8. Pingback: Tricks of the trade | Motho ke motho ka botho

Comments are closed.