Generate a random filename in unix shell

前端 未结 14 628
说谎
说谎 2020-12-22 23:39

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

c7fdfc8f409c548a10a0a89a7         


        
相关标签:
14条回答
  • 2020-12-23 00:09

    Yet another way[tm].

    R=$(echo $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM | md5 | cut -c -8)
    FILENAME="abcdef-$R"
    
    0 讨论(0)
  • 2020-12-23 00:12

    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
    
    0 讨论(0)
  • 2020-12-23 00:12

    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.

    0 讨论(0)
  • 2020-12-23 00:12

    If you have openssl in your system you can use it for generating random hex (also it can be -base64) strings with defined length. I found it pretty simple and usable in cron in one line jobs.

     openssl rand -hex 32
     8c5a7515837d7f0b19e7e6fa4c448400e70ffec88ecd811a3dce3272947cb452
    
    0 讨论(0)
  • 2020-12-23 00:14

    This answer is very similar to fmarks, so I cannot really take credit for it, but I found the cat and tr command combinations quite slow, and I found this version quite a bit faster. You need hexdump.

    hexdump -e '/1 "%02x"' -n32 < /dev/urandom
    
    0 讨论(0)
  • 2020-12-23 00:17

    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.

    0 讨论(0)
提交回复
热议问题