Skip to Content

Terminal Tips: Generate random filenames for digital photo frame images

It's not at all unlikely that you or a relative have received a digital photo frame as some sort of gift. The concept is great, one frame on your mantle, many pictures on display. Unfortunately, Apple has yet to enter the photo frame market -- at least until the iPad is released, that is. In the interim, we are stuck dealing with photo frames that look good but do not always function as we expect.

One example of a photo frame feature gap is the lack of ability to "shuffle" photos so that they can be displayed in a random order. While sitting at my grandma's house for hours on end, it quickly became a personal challenge to try and successfully guess the next photo that would appear.

Luckily, the screencast junkies over at Murphy Mac have found a somewhat simple solution to this conundrum. The problem is the result of frames using the all-too-common alphabetical sorting method when showing photos. While this is useful for organization, it is less helpful for the purposes of the photo frame. The answer lies in assigning totally random filenames to your digital photos. As the title indicates, this is a Terminal.app-based tip so you'll have to get your hands a little dirty with this one.

The process is something that could take a long time, especially when you consider that many photo libraries have thousands of images. However, through the power of Terminal and the use of a for-loop, it is possible to randomly rename an entire folder of images with one fell swoop.

To begin, you will need to place all of your images in a single folder. We here at the TUAW HQ cannot recommend strongly enough using copied image files and triple-checking your backups prior to moving forward. Open up Terminal.app and use cd to navigate to the new directory where you placed your image copies (now might be a good time to check your backups a fourth time). When done, type the following command and then press return.

for i in *.jpg; do mv $i $RANDOM.jpg; done

Assuming all of your photos are jpegs and located in same directory, then after some whizzbangery you will have a folder filled with very strangely-named images. Copy these files to your photo frame and you now have a fully randomized photo slideshow. Feel free repeat as often as your guests (or your OCD) require. Got any other handy Terminal tips? Feel free to share them in the comments!

It's not at all unlikely that you or a relative have received a digital photo frame as some sort of gift. The concept is great, one frame...
 

Add a Comment

*0 / 3000 Character Maximum

13 Comments

Filter by:
jocko

I can't understand why you would go to all this effort to set this up. Lots of Digital Frames have random playback function. My one has about 1500 pictures on the Internal Memory and when i have it in shuffle mode, it's pretty effective. It even lets me shuffle the pictures in individual folders. Only cost me $89 on amazon.com.
These are the guys who do it: www.nix-digital.com
It's their 8 Inch frame that I have. Looks much better than an ipad too.
http://www.amazon.com/s/ref=bl_sr_photo?ie=UTF8&search-alias=photo&field-brandtextbin=NIX
Keep it simple dude!!

March 25 2010 at 7:32 AM Report abuse rate up rate down Reply
Stijn Jonker

How about ln -s ; keep the original files, but still achieve the same effect.

February 26 2010 at 4:51 AM Report abuse rate up rate down Reply
jimmie

Generate file name by timestamp with microseconds. If not enough concatenate some random value to the timestamp.

February 25 2010 at 11:08 PM Report abuse rate up rate down Reply
Chuck

also with the "mv" command, you'll lose the original name of the file.

so grandma's slideshow will be "random", but you'll have no way of organizing the photos after the fact.

if you have the disk space available, swap out the "mv" command for "cp" and you'll create copies. then once you've put the randomly named files onto grandma's photo frame, just delete them.

if you didn't have disk space available, you could still "mv" the files, but retain the original filename after the $RANDOM. for example:

/Users/chuck/Desktop/PC Files/pictest> ls
scan0001.jpg scan0002.jpg scan0003.jpg
/Users/chuck/Desktop/PC Files/pictest> for picture in *.jpg; do mv $picture $RANDOM.$picture; done
/Users/chuck/Desktop/PC Files/pictest> ls
13517.scan0002.jpg 24733.scan0003.jpg 31922.scan0001.jpg

-chuck

February 25 2010 at 9:47 PM Report abuse rate up rate down Reply
1 reply to Chuck's comment
Chuck

and...

if you ever wanted to get the $RANDOM off the filename:

/Users/chuck/Desktop/PC Files/pictest> ls
13517.scan0002.jpg 24733.scan0003.jpg 31922.scan0001.jpg
/Users/chuck/Desktop/PC Files/pictest> for picture in *.jpg; do mv $picture `echo $picture | cut -d'.' -f2-`; done
/Users/chuck/Desktop/PC Files/pictest> ls
scan0001.jpg scan0002.jpg scan0003.jpg

-chuck

February 25 2010 at 9:50 PM Report abuse rate up rate down Reply
Trick

technically the photos won't display randomly... they are just reordered randomly... so it will play the same way every time.

just sayin'... this would be pretty pointless if you didn't have lots of photos

February 25 2010 at 5:48 PM Report abuse rate up rate down Reply
1 reply to Trick's comment
Chris McDonald

Ahhh, very good point, in which case we need Linux shell access to the digital photo frame so that we can run:

while true
do
N=`/bin/ls -1d *.jpg | wc -l | sed 's/ //g'`
R=`expr $RANDOM '%' $N + 1`
F=`/bin/ls -1d *.jpg | head -$R | tail -1`
display-file "$F"
done


(imagine consistent indenting)

February 25 2010 at 11:10 PM Report abuse rate up rate down Reply
Andrew

Or, for even better randomness, you can use…

head /dev/random | md5

or, if you want the string to be shorter…

head /dev/random | md5 | cut -c 1-10

will only show the first 10 characters. You can also strip all of the letters if you only want numbers for whatever reason…

head /dev/random | md5 | 's/[a-z]//'

or only letters…

head /dev/random | md5 | 's/[0-9]//'

Note: The above is not recommended, as the letters can only be a-f.

You would of course have to use `` (where is something like head /dev/random | md5) instead of $RANDOM, so…

for i in *.jpg; do mv '$i' `head/dev/random | md5`.jpg; done

will rename any JPG images in the current directory to a random 32 characters, with .jpg of course.

February 25 2010 at 4:50 PM Report abuse rate up rate down Reply
Kent

Actually, to be on the safe side, a better command is:

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

This will ensure that files with spaces or anything in the name will not screw it up.

Better still

for i in *.jpg *.JPG; do mv "$i" $RANDOM.jpg; done

will handle upper and lower case JPG extensions (and will work as expected even if they are all upper case or lower case).

February 25 2010 at 4:31 PM Report abuse rate up rate down Reply
2 replies to Kent's comment
Kent

In case you didn't realize, the difference between this and the posted command is the "s around the $i.

February 25 2010 at 4:33 PM Report abuse rate up rate down Reply
Al

This is still dangerous. Given enough files, $RANDOM will eventually be the same value, overwriting files.

February 25 2010 at 4:50 PM Report abuse rate up rate down Reply
Al

WARNING: This will delete some files if $RANDOM ever ends up being the same, which it will.

February 25 2010 at 4:22 PM Report abuse rate up rate down Reply
1 reply to Al's comment
Chris McDonald

+1
As some shells only provide a 15-bit value for $RANDOM, the chance of overwriting a photo amongst "thousands of images" is dangerously high.

I feel that this is much better:

for i in *.jpg; do mv "$i" `head -c32 < /dev/random | md5`.jpg; done

February 25 2010 at 4:56 PM Report abuse rate up rate down Reply
Buy an ad here

Hot Apps on TUAW

Tweets

© 2012 AOL Inc. All Rights Reserved.