9

I have a bunch of photos with varying names.
I want to give each photo a random name(*), how do I do that?

(*)I'm going to put them on a digital photo-frame that can't shuffle

5
  • 2
    Do you want a single solution that works for all of those platforms? Or a solution that works on any one? What script languages are you limited to or prefer? Are all the photos in one folder or multiple?
    – EBGreen
    Jun 30, 2011 at 20:43
  • @EBGreen - I have a solution that works wherever bash is available. I would also like one that works on windows. (and recommending cygwin is cheating)
    – Nifle
    Jun 30, 2011 at 20:51
  • Why not use the MD5 sum? Jun 30, 2011 at 22:54
  • While MD5 sums would make name collisions less likely, MD5 collisions are a known issue. mscs.dal.ca/~selinger/md5collision
    – EBGreen
    Jul 1, 2011 at 0:01
  • @Angel - Using the MD5 sum as a name would have worked pretty well for uniqueness. The extra time to calculate the sum on 4000+ photos would have been a boring wait.
    – Nifle
    Jul 2, 2011 at 10:52

4 Answers 4

11

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)"}

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.

2
  • 2
    Looks like this appends a second .jpg to the result.
    – useSticks
    Sep 5, 2019 at 0:23
  • @useSticks the answer is now edited to remove the duplicate ".jpg" May 23, 2023 at 15:08
12

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
4
  • 4
    Instead of "${RANDOM}${RANDOM}.jpg" you could also say "${RANDOM}-$i". This will avoid collisions and keep the old name information (as well as satisfy the random sort requirement).
    – bitmask
    Jun 30, 2011 at 21:17
  • Oooh...I like it....I'll tweak the PS solution to incorporate that.
    – EBGreen
    Jun 30, 2011 at 23:57
  • @bitmask - A good point. In this case the images didn't have any meaningful names and sometimes contained spaces so I was happy to get rid of them.
    – Nifle
    Jul 1, 2011 at 8:46
  • how can this be generalized to accept any extension?
    – yarun can
    Jan 12, 2019 at 0:40
4

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

1
  • This does not work properly when the file name is like "myfile.is.a.jpg" the resulting randomm name is like "8c5b4ee8-abd8-4f62-b641-48e58baf9186.is.a.jpg:
    – yarun can
    Jan 12, 2019 at 0:39
-1

Most batch file renamers can do this, here is one

Easy method would be to do a sequential numbering of the file, I am sure there other options with this software, use your imagination.

Here is the one I use for bulk renaming tasks

You must log in to answer this question.

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