Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I know I can get random (well, pseudo-random) characters, unconstrained, from '/dev/random' (or /dev/urandom). But what if I need these characters to generate a filename? I need to somehow have a stream or a sequence of printable characters, or alphanumeric ones to be on the safe side. What's the best way to do that in the shell?

(I know I could theoretically do a tr on chars from /dev/random but that doesn't sound like the right thing to do.)

share|improve this question
1  
You may want to look at the mktemp command, unless for some reason you really only need to generate a name, without ever creating a file by that name. – chepner Apr 22 '13 at 18:05
    
@chepner: That's a great solution to my motivating problem, thanks! Unfortunately it's not what I ended up asking... – einpoklum Apr 22 '13 at 21:10
    
Convert the output of urandom to printable. Or keep reading it and only output those in the printable range. – Matt H Apr 22 '13 at 21:30

I see /usr/bin/jot is capable of suppling a specified count of constrained random characters, which can be reshaped to a word suitable for a filename. There's probably a more eloquent way of specifying all printable characters, but I'm a bit rusty on my shell scripting.

[mini-nevie:~] nevinwilliams% jot -r -c 25 A Z | rs -g
EUCGGSIOIDMPCHXCHIGYXBYWJ
[mini-nevie:~] nevinwilliams% jot -r -c 25 A Z | rs -g
PMKQQZOCYETVIHIMTIQFYTFZK
[mini-nevie:~] nevinwilliams% jot -r -c 25 A Z | rs -g
CXBKEFSIRCOOGUICJPOMZKMSG
[mini-nevie:~] nevinwilliams% jot -r -c 25 A Z | rs -g
MEHSFOSHLKKJRKQLRCXUCBJAD
[mini-nevie:~] nevinwilliams% jot -r -c 25 A Z | rs -g
ZCCGKYIWLBTJUVIHQBCHXCVEU
share|improve this answer
    
That's quite nice! ... of course, you need jot installed and I don't think it's installed by default (at least not on Debian systems). – einpoklum Apr 20 '13 at 8:07
1  
It seems to be a BSD thing... As you mentioned using the generated text (however you choose to generate it) for a filename, you might wish to tag on a sufficiently precise timestamp, such that as unlikely as it may be to fetch the same random string more than once, you'll still have a unique file name. – Nevin Williams Apr 20 '13 at 9:12

$ dd if=/dev/urandom of=/dev/stdout bs=124 count=1 2>/dev/null | sha256sum | awk '{print $1}'

share|improve this answer
up vote 1 down vote accepted

A tweaking of @dawud's solution:

dd if=/dev/urandom bs=124 count=1 2>/dev/null | sha256sum | cut -c1-64

gets you 64 characters; and you can use /dev/random on UN*X systems without /dev/urandom.

A different, simpler approach, based on @NevinWilliam's suggestion: just do

date +%s.%N

No alphabet characters is a special case of alphanumeric... this doesn't cover the whole range of possible strings, and is limited to 9 characters, and it's not very random - except maybe the last 2-4 digits or so - but since we're not doing crypto, this should be good enough.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .