在 unixshell 中生成一个随机文件名

我想在 unixshell (比如 tcshell)中生成一个随机文件名。文件名应该由随机的32个十六进制字母组成,例如:

c7fdfc8f409c548a10a0a89a791417c5

(除此之外,我还会加上一些必要的东西)。关键是只能在 shell 中完成,而不需要求助于程序。

68045 次浏览

Tested in zsh, should work with any BASH compatible shell!

#!/bin/zsh


SUM=`md5sum <<EOF
$RANDOM
EOF`


FN=`echo $SUM | awk '// { print $1 }'`


echo "Your new filename: $FN"

Example:

$ zsh ranhash.sh
Your new filename: 2485938240bf200c26bb356bbbb0fa32
$ zsh ranhash.sh
Your new filename: ad25cb21bea35eba879bf3fc12581cc9

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

why do not use unix mktemp command:

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

Grab 16 bytes from /dev/random, convert them to hex, take the first line, remove the address, remove the spaces.

head /dev/random -c16 | od -tx1 -w16 | head -n1 | cut -d' ' -f2- | tr -d ' '

Assuming that "without resorting to a program" means "using only programs that are readily available", of course.

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.

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.

Yet another way[tm].

R=$(echo $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM | md5 | cut -c -8)
FILENAME="abcdef-$R"

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

Hope to add a (maybe) better solution to this topic.

Notice: this only works with bash4 and some implement of mktemp(for example, the GNU one)

Try this

fn=$(mktemp -u -t 'XXXXXX')
echo ${fn/\/tmp\//}

This one is twice as faster as head /dev/urandom | tr -cd 'a-f0-9' | head -c 32, and eight times as faster as cat /dev/urandom | tr -cd 'a-f0-9' | head -c 32.

Benchmark:

With mktemp:

#!/bin/bash
# a.sh
for (( i = 0; i < 1000; i++ ))
do
fn=$(mktemp -u -t 'XXXXXX')
echo ${fn/\/tmp\//} > /dev/null
done


time ./a.sh
./a.sh  0.36s user 1.97s system 99% cpu 2.333 total

And the other:

#!/bin/bash
# b.sh
for (( i = 0; i < 1000; i++ ))
do
cat /dev/urandom | tr -dc 'a-zA-Z0-9' | head -c 32 > /dev/null
done


time ./b.sh
./b.sh  0.52s user 20.61s system 113% cpu 18.653 total

Another thing you can add is running the date command as follows:

date +%S%N

Reads nonosecond time and the result adds a lot of randomness.

If you are on Linux, then Python will come pre-installed. So you can go for something similar to the below:

python -c "import uuid; print str(uuid.uuid1())"

If you don't like the dashes, then use replace function as shown below

python -c "import uuid; print str(uuid.uuid1()).replace('-','')"

The first answer is good but why fork cat if not required.

tr -dc 'a-f0-9' < /dev/urandom | head -c32

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

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 '-'