Generate a random filename in unix shell

64,934

Solution 1

Assuming you are on a linux, the following should work:

cat /dev/urandom | tr -cd 'a-f0-9' | head -c 32

This is only pseudo-random if your system runs low on entropy, but is (on linux) guaranteed to terminate. If you require genuinely random data, cat /dev/random instead of /dev/urandom. This change will make your code block until enough entropy is available to produce truly random output, so it might slow down your code. For most uses, the output of /dev/urandom is sufficiently random.

If you on OS X or another BSD, you need to modify it to the following:

cat /dev/urandom | env LC_CTYPE=C tr -cd 'a-f0-9' | head -c 32

Solution 2

why do not use unix mktemp command:

$ TMPFILE=`mktemp tmp.XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX` &&  echo $TMPFILE
tmp.MnxEsPDsNUjrzDIiPhnWZKmlAXAO8983

Solution 3

One command, no pipe, no loop:

hexdump -n 16 -v -e '/1 "%02X"' -e '/16 "\n"' /dev/urandom

If you don't need the newline, for example when you're using it in a variable:

hexdump -n 16 -v -e '/1 "%02X"' /dev/urandom

Using "16" generates 32 hex digits.

Solution 4

As you probably noticed from each of the answers, you generally have to "resort to a program".

However, without using any external executables, in Bash and ksh:

string=''; for i in {0..31}; do string+=$(printf "%x" $(($RANDOM%16)) ); done; echo $string

in zsh:

string=''; for i in {0..31}; do string+=$(printf "%x" $(($RANDOM%16)) ); dummy=$RANDOM; done; echo $string

Change the lower case x in the format string to an upper case X to make the alphabetic hex characters upper case.

Here's another way to do it in Bash but without an explicit loop:

printf -v string '%X' $(printf '%.2s ' $((RANDOM%16))' '{00..31})

In the following, "first" and "second" printf refers to the order in which they're executed rather than the order in which they appear in the line.

This technique uses brace expansion to produce a list of 32 random numbers mod 16 each followed by a space and one of the numbers in the range in braces followed by another space (e.g. 11 00). For each element of that list, the first printf strips off all but the first two characters using its format string (%.2) leaving either single digits followed by a space each or two digits. The space in the format string ensures that there is then at least one space between each output number.

The command substitution containing the first printf is not quoted so that word splitting is performed and each number goes to the second printf as a separate argument. There, the numbers are converted to hex by the %X format string and they are appended to each other without spaces (since there aren't any in the format string) and the result is stored in the variable named string.

When printf receives more arguments than its format string accounts for, the format is applied to each argument in turn until they are all consumed. If there are fewer arguments, the unmatched format string (portion) is ignored, but that doesn't apply in this case.

I tested it in Bash 3.2, 4.4 and 5.0-alpha. But it doesn't work in zsh (5.2) or ksh (93u+) because RANDOM only gets evaluated once in the brace expansion in those shells.

Note that because of using the mod operator on a value that ranges from 0 to 32767 the distribution of digits using the snippets could be skewed (not to mention the fact that the numbers are pseudo random in the first place). However, since we're using mod 16 and 32768 is divisible by 16, that won't be a problem here.

In any case, the correct way to do this is using mktemp as in Oleg Razgulyaev's answer.

Solution 5

uuidgen generates exactly this, except you have to remove hyphens. So I found this to be the most elegant (at least to me) way of achieving this. It should work on linux and OS X out of the box.

uuidgen | tr -d '-'
Share:
64,934

Related videos on Youtube

Demonsoul
Author by

Demonsoul

Updated on August 17, 2020

Comments

  • Demonsoul
    Demonsoul over 3 years

    I would like to generate a random filename in unix shell (say tcshell). The filename should consist of random 32 hex letters, e.g.:

    c7fdfc8f409c548a10a0a89a791417c5
    

    (to which I will add whatever is neccesary). The point is being able to do it only in shell without resorting to a program.

  • aarona
    aarona almost 14 years
    I tested this out. Its a lot slower then the other solutions, but does work.
  • Keyur Padalia
    Keyur Padalia almost 14 years
    Yeah, it's a fairly long pipe for something simple like that. I'd hoped for some more flags on od, but couldn't get it to do what I wanted. fmark's answer, though it processes more bytes, might actually be faster.
  • fmark
    fmark almost 14 years
    It would be faster if it used /dev/urandom rather than /dev/random
  • LukeN
    LukeN almost 14 years
    fmark is right, it could be the /dev/random part here, because /dev/random is a truly random number generator and will block when there's no more entropy :)
  • President James K. Polk
    President James K. Polk almost 14 years
    @LukeN: Yours was good. And technically, this solution is not guaranteed to terminate :)
  • LukeN
    LukeN almost 14 years
    This solution was actually doing weird things for me, as it appended a white-backgrounded "%" sign after the actual random hash, but because my shell is generally behaving strange on some occasions, I didn't want to make this look bad before it was accepted :)
  • Gareth Stockwell
    Gareth Stockwell almost 14 years
    I tried this on a Mac, which has a /dev/urandom. Executing the command in a bash shell causes an error - 'tr: Illegal byte sequence'
  • fmark
    fmark almost 14 years
    I think the problem here is that BSD and Mac's interpret the string as being multibyte instead of single byte. I haven't got a machine to try this on, so report back here if this works: cat /dev/urandom | env LC_CTYPE=C tr -cd 'a-f0-9' | head -c 32
  • President James K. Polk
    President James K. Polk almost 14 years
    @fmark: your modified version works perfectly on my Snow Leopard bash.
  • Jens
    Jens almost 12 years
    Looks like another useless use of cat :-)
  • SourceSeeker
    SourceSeeker almost 12 years
    This will only generate 32768 unique filenames rather than 16^32.
  • Igor Chubin
    Igor Chubin over 10 years
    In this case you will not only generate the string, but create a file also.
  • Admin
    Admin about 10 years
    the program available on most systems is md5sum. I like this solution, because if you don't need high randomness, you can just use $RANDOM only once, making this the briefest most readable solution.
  • reto
    reto about 10 years
    a) If you have md5sum you can replace md5 with md5sum and update the cut accordingly. b) Using only one $RANDOM is dangerous, it produces only values between 0 and 32767, thats much to short, depending on the application.
  • glenn jackman
    glenn jackman almost 10 years
    some implementations have a --dry-run flag to prevent a file being created. That of course opens a possible race condition.
  • ghoti
    ghoti over 9 years
    This will also only work on systems that include a md5sum binary. FreeBSD and OSX, for example, have a /sbin/md5 which serves the same purpose but uses different option syntax.
  • user1643723
    user1643723 over 7 years
    This solution is definitely the most portable one! It even works on the latest Android (except you should replace -w16 with -N16).
  • John P
    John P about 7 years
    @LukeN You may have figured this out by now, 5 years later, but that '%' just means the line ended without a newline character. unix.stackexchange.com/a/167600/105858
  • Walf
    Walf about 7 years
    Ace. Use -n16 fro 32 characters.
  • avetisk
    avetisk almost 7 years
    This is awesome. I never thought using hexdump for that.
  • GG2
    GG2 almost 6 years
    Even though I know it strays from the literal constraints of the question, I still want to record this here. For my purposes, just change the f to z, and 32 to 9 (or whatever length of filename you desire) ... beautiful.
  • Santosh Kumar Arjunan
    Santosh Kumar Arjunan over 5 years
    Thank you Dennis, it helped me.. I just set the string to be empty, so that the lengh of string is always as expected that appending each time while rerunning the same script.. randomId=$(string="";for i in {0..5}; do string+=$(printf "%x" $(($RANDOM%16)) ); done; echo $string); echo "Random Id value is ${randomId}"
  • SourceSeeker
    SourceSeeker over 5 years
    @SantoshKumarA: Thanks for pointing out that the variable needs to be initialized. I added that to my answer. I also add another interesting technique and an explanation for it.
  • Blindleistung
    Blindleistung almost 2 years
    Not using any calls out of the bash process makes this the best answer for me. In preference to the accepted answer.