45

cat /dev/urandom is always a fun way to create scrolling characters on your display, but produces too many non-printable characters.

Is there an easy way to encode it on the command-line in such a way that all of its output are readable characters, base64 or uuencode for example.

Note that I prefer solutions that require no additional files to be created.

Phil Ross
  • 25,590
  • 9
  • 67
  • 77
alexanderpas
  • 2,712
  • 2
  • 23
  • 33

7 Answers7

69

What about something like

cat /dev/urandom | base64

Which gives (lots of) stuff like

hX6VYoTG6n+suzKhPl35rI+Bsef8FwVKDYlzEJ2i5HLKa38SLLrE9bW9jViSR1PJGsDmNOEgWu+6
HdYm9SsRDcvDlZAdMXAiHBmq6BZXnj0w87YbdMnB0e2fyUY6ZkiHw+A0oNWCnJLME9/6vJUGsnPL
TEw4YI0fX5ZUvItt0skSSmI5EhaZn09gWEBKRjXVoGCOWVlXbOURkOcbemhsF1pGsRE2WKiOSvsr
Xj/5swkAA5csea1TW5mQ1qe7GBls6QBYapkxEMmJxXvatxFWjHVT3lKV0YVR3SI2CxOBePUgWxiL
ZkQccl+PGBWmkD7vW62bu1Lkp8edf7R/E653pi+e4WjLkN2wKl1uBbRroFsT71NzNBalvR/ZkFaa
2I04koI49ijYuqNojN5PoutNAVijyJDA9xMn1Z5UTdUB7LNerWiU64fUl+cgCC1g+nU2IOH7MEbv
gT0Mr5V+XAeLJUJSkFmxqg75U+mnUkpFF2dJiWivjvnuFO+khdjbVYNMD11n4fCQvN9AywzH23uo
03iOY1uv27ENeBfieFxiRwFfEkPDgTyIL3W6zgL0MEvxetk5kc0EJTlhvin7PwD/BtosN2dlfPvw
cjTKbdf43fru+WnFknH4cQq1LzN/foZqp+4FmoLjCvda21+Ckediz5mOhl0Gzuof8AuDFvReF5OU

Or, without the (useless) cat+pipe :

base64 /dev/urandom

(Same kind of output ^^ )


EDIT : you can also user the --wrap option of base64, to avoid having "short lines" :

base64 --wrap=0 /dev/urandom

This will remove wrapping, and you'll get "full-screen" display ^^

Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
  • 26
    "All I see now is blonde, brunette, redhead." – alexanderpas Aug 08 '09 at 23:50
  • @Stefano : so did I :-D ; @alexanderpas : if watching random scrolling characters makes you see that kind of characters, maybe I should watch random characters more often ^^ – Pascal MARTIN Aug 08 '09 at 23:54
  • @alexanderpas: cat /dev/urandom | uuencode - | tr -d '0-9' | sed -e 's/\([@$&~#(?)+<>^;]\)/^[[1m^[[32m\1^[[0m/g' | sed -e 's/\([*!]\)/^[[30m\1^[[0m/g' | sed -e 's/\([A-Z]\)/^[[32m\1^[[0m/g' | tr -d '\n' # 1) convert ^[ to ctrl-v esc after pasting 2) can be improved, but now I'm tired ;) – Stefano Borini Aug 09 '09 at 00:27
  • 3
    `base64 --wrap=0 /dev/urandom |head -c 100` will get you 100 random base64 characters. If you're looking for a bash-based matrix-style screen saver, [it has been done before](http://www.commandlinefu.com/commands/view/10585/matrix-style) :-) – Adam Katz May 28 '15 at 00:01
  • 1
    `head -c128 /dev/urandom | base64 --wrap=0`, to avoid breaking the pipe @AdamKatz. – sebastian Mar 20 '19 at 13:44
  • @sebastian – True, but that shouldn't be a problem and your method requires some trial and error (or piping into another call, `head -c 100`) to get the right output character count (careful, you don't want trailing `=` characters!). Try `head -c 75 /dev/urandom |base64 -w 0` for 100 characters. – Adam Katz Mar 20 '19 at 13:58
23

A number of folks have suggested catting and piping through base64 or uuencode. One issue with this is that you can't control how much data to read (it will continue forever, or until you hit ctrl+c). Another possibility is to use the dd command, which will let you specify how much data to read before exiting. For example, to read 1kb:

dd if=/dev/urandom bs=1k count=1 2>/dev/null | base64

Another option is to pipe to the strings command which may give more variety in its output (non-printable characters are discarded, any runs of least 4 printable characters [by default] are displayed). The problem with strings is that it displays each "run" on its own line.

dd if=/dev/urandom bs=1k count=1 2>/dev/null | strings

(of course you can replace the entire command with

strings /dev/urandom

if you don't want it to ever stop).

If you want something really funky, try one of:

cat -v /dev/urandom
dd if=/dev/urandom bs=1k count=1 2>/dev/null | cat -v
Adam Batkin
  • 51,711
  • 9
  • 123
  • 115
5

So, what is wrong with

cat /dev/urandom | uuencode -

?

Fixed after the first attempt didn't actually work... ::sigh::

BTW-- Many unix utilities use '-' in place of a filename to mean "use the standard input".

dmckee --- ex-moderator kitten
  • 98,632
  • 24
  • 142
  • 234
4

There are already several good answers on how to base64 encode random data (i.e. cat /dev/urandom | base64). However in the body of your question you elaborate:

... encode [urandom] on the command-line in such a way that all of it's output are readable characters, base64 or uuencode for example.

Given that you don't actually require parseable base64 and just want it to be readable, I'd suggest

cat /dev/urandom | tr -dC '[:graph:]'

base64 only outputs alphanumeric characters and two symbols (+ and / by default). [:graph:] will match any printable non-whitespace ascii, including many symbols/punctuation-marks that base64 lacks. Therefore using tr -dC '[:graph:]' will result in a more random-looking output, and have better input/output efficiency.

I often use < /dev/random stdbuf -o0 tr -Cd '[:graph:]' | stdbuf -o0 head --bytes 32 for generating strong passwords.

Aaron J Lang
  • 2,048
  • 3
  • 20
  • 27
1
cat /dev/urandom | tr -dc 'a-zA-Z0-9'
Kalle Richter
  • 8,008
  • 26
  • 77
  • 177
Serg
  • 668
  • 1
  • 4
  • 6
  • 3
    That's the wrong way to do it. It's not Base64, the conversion lack two symbols, so it's more like Base62 and thus decoded values are going to be biased, eg. not so random. – Yann Droneaud Feb 12 '13 at 14:05
1

You can do more interesting stuff with BASH's FIFO pipes:

uuencode <(head -c 200 /dev/urandom | base64 | gzip)
greyfade
  • 24,948
  • 7
  • 64
  • 80
-2

Try

xxd -ps /dev/urandom

xxd(1)

Zombo
  • 1
  • 62
  • 391
  • 407
lhf
  • 70,581
  • 9
  • 108
  • 149