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!
Share
Categories
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
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!!
How about ln -s ; keep the original files, but still achieve the same effect.
February 26 2010 at 4:51 AM Report abuse Permalink rate up rate down ReplyGenerate 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 Permalink rate up rate down Replyalso 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
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
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
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)
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.
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).
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 Permalink rate up rate down ReplyThis is still dangerous. Given enough files, $RANDOM will eventually be the same value, overwriting files.
February 25 2010 at 4:50 PM Report abuse Permalink rate up rate down ReplyWARNING: 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 Permalink rate up rate down Reply+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
Hot Apps on TUAW
Deals of the Day
more deals- Altec Lansing Octiv Duo iDock for $48 + free shipping
- Used Apple iMac 17" Core Duo 1.83GHz for $430 + $28 s&h
- Lounge Deluxe Stand for iPhone / iPod touch for $28 + $8 s&h
- Brookstone Surround-Sound Earbuds for $14 + $7 s&h
- Refurbished Skullcandy Tokidoki Smokin' Buds Mic'd Headset for $5 + $2 s&h
- Stitchway Backup Battery for iPod / iPhone for $5 + free shipping
Software Updates
more updates- EFI Firmware Update brings Lion Internet Recovery to 2010-model Macs
- OS X Lion 10.7.3 released with Safari 5.1.3, Wi-Fi bug fix
- Aperture updated to 3.2.2, addresses Photo Stream issue
- Apple updates Keynote to address Lion issues
- Google Search app gets new look on iPad
- Apple releases Apple TV Software Update 4.4.3



13 Comments