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 …
1XPHjX5DrgO5ufDTGIz3Vv6O3GtVkNjcuTgIQ6IB5GLJOzkPiKrwAMSVlgdYkofXcajw02GZ33l7f98suzAm5UG5lFpwSFM0AFRm5SAbYMcs
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. 🙂
For random passwords, I usually use “apg”. It can even generates “pronounceable” passwords which are much easier to remember.
On Mac OS X, I got a “tr: illegal byte sequence” error message running the command. A thread on Stackexchange produced this that took care of the issue for me:
< /dev/urandom LC_CTYPE=C tr -dc A-Za-z0-9 | head -c25 ; echo
Pingback: shuf: Random yet intelligible | Inconsolation
Pingback: gnupg: Symmetric encryption and decryption | Inconsolation
Pingback: apg: Pronounced, “ay-pee-jee” | Inconsolation
Pingback: Bonus: 2013 in review | Inconsolation
Pingback: mcookie: Mmm, cookies! | Inconsolation
Pingback: otp: Not as simple as it looks | Inconsolation
Pingback: Tricks of the trade | Motho ke motho ka botho