How to batch rename files with a random name

Solution 1:

Assuming all the images are in a single folder, this would work in powershell:

Get-ChildItem *.jpg | ForEach-Object{Rename-Item $_ -NewName "$(Get-Random)-$($_.Name).jpg"}

It is possible that you would get potential name collisions, but Get-Random by default returns a 32 bit unsigned int from 0 to Int32.MaxValue (0 to 2147483647). You could certainly add another Get-Random in to reduce the likelihood of a collision just as in the Bash answer.

Solution 2:

One way if you have a bash shell handy is to use the $RANDOM environment variable. It generates random values between 0 and 32767.

A simple for loop in bash works fine if you only have a few hundred files.

for i in *.jpg; do mv -i "$i" ${RANDOM}.jpg; done

Since I had about 4000 files to rename I soon got collisions that the -i flag to mv caught. Adding another $RANDOM took care of that.

for i in *.jpg; do mv -i "$i" ${RANDOM}${RANDOM}.jpg; done

Solution 3:

for f in *; do ext=$(echo "$f" | sed 's|\([^.]*\)||'); mv "$f" "$(uuidgen)$ext"; done