Industry guru Dave Taylor offers tech support on technical and business topics, including iPhone, iPod, Microsoft Windows, Sony PSP, cellphones, online advertising, CSS, Web design, business, Unix, Linux, SEO, Mac OS X, and shell script programming.     


How can I bulk rename Apple iPhone screen captures?

As part of my work, I take tons of screen captures on our test Apple iPhone for documentation, but my boss wants me to use SEO friendly file names and it's a huge hassle to rename them one by one. Is there some sort of script I can use on my Mac to fix things?


Dave's Answer:

You can indeed solve this problem a wide variety of ways, including some slick Mac graphical utilities that you can find at VersionTracker.com, but let's look at a simple script that solves this problem because I like writing shell scripts!

The basic idea is that we're going to replace "IMG_" with a pattern that you specify. For example, perhaps you need "prod-v11" so that IMG_0001.PNG becomes "prod-v11-0001.PNG". Better yet, let's go ahead and fix the all uppercase filename suffix while we're at it too, so it'll end up as "prod-v11-0001.png".

To do this, we'll need to transform the old file name into a new one. This is easily done with the powerful "sed" command.

Here's how I'd do it:

newname="$(echo $name | sed "s/IMG_00/$basename/;s/PNG/png/")"

Well, what I'm doing here is actually transforming IMG_0001.PNG into "pattern01.png" because I know that for my uses I'll never have more than 99 images in the same collection. If you might have more, you'll want to use IMG_0 instead of IMG_00.

The transforms are to substitute IMG_00 for the basename pattern, then PNG substituted with png. See how that works?

Since I don't want to type in each filename but have them all transform magically in a loop, that's exactly how I'll code it:

for name in IMG*PNG
do
  newname="$(echo $name | sed "s/IMG_00/$basename/;s/PNG/png/")"
  echo "renaming $name as $newname"
  mv $name $newname
  done

One more piece is necessary for this to be a useful script: the ability to specify the pattern that should be used for the rename. This is done by grabbing the command argument to the script.

Here's my full script:

#!/bin/sh

if [ $# -ne 1 ] ; then
  echo "Usage: $(basename $0) new-base-pattern"
  exit 0
fi

basename="$1"

for name in IMG*PNG
do
  newname="$(echo $name | sed "s/IMG_00/$basename/;s/PNG/png/")"
  echo "renaming $name as $newname"
  mv $name $newname
done

exit 0

When run against a bunch of iPhone screen shots on my Mac, here's what I see:

$ sh rename-iphone-pics.sh apple-iphone-download-app-
renaming IMG_0001.PNG as apple-iphone-download-app-01.png
renaming IMG_0002.PNG as apple-iphone-download-app-02.png
renaming IMG_0003.PNG as apple-iphone-download-app-03.png
renaming IMG_0004.PNG as apple-iphone-download-app-04.png
renaming IMG_0005.PNG as apple-iphone-download-app-05.png
renaming IMG_0006.PNG as apple-iphone-download-app-06.png

Quickly and easily done. Works for you, I hope?


More Useful Shell Script Programming Articles:
✔   Secretly capture screenshots on my Mac?
When I used to work on a Linux system, there was a utility we had that would let me take screen captures every...
✔   Parsing "id" strings in a Shell Script?
Hello Dave. I need a Bash shell script that creates a directories with the group names automatically when user logs in to the...
✔   Copy and Paste from the Mac OS X Command Line?
I am constantly running commands in Terminal.app on my MacBook and then copying and pasting the results into email messages or documents. Yes,...
✔   Script to test line lengths for Twitter compatibility?
I've been tasked with writing a series of tweets for a Black Friday marketing campaign and am finding it a bit tricky because...
✔   Shell script to convert lowercase to title case?
As part of a project I'm working on, I find myself deep in a Linux shell script, needing to have a subroutine that...

Let's stay in touch!
Sign up for my weekly AskDaveTaylor Newsletter and you'll receive even more tech and gadget help right to your inbox, along with exclusive news and industry updates. It's good stuff. I promise!
    Enter your name: and your email addr:  




Categorized: Mac OS X Help , Shell Script Programming   (Article 8823, Written by )
Tagged: apple iphone, renaming files, screen captures, shell scripts
Previous: How do I filter out status updates on Facebook?
Next: How do I download a new application to my Apple iPhone?




Reader Comments To Date:

Starbucks coffee cup I do have a lot to say, and questions of my own for that matter, but first I'd like to say thank you, Dave, for all your helpful information by buying you a cup of coffee!
Rather amazingly, there are no comments on this article yet.

I do have a comment, now that you mention it!











I will never send you any unsolicited email. Ever.






Check This Out Too...

 
Look for Answers
Need Help? Ask Dave Taylor!


Follow Me on Pinterest

Find Me on Google+
ADT on G+
© 2002 - 2013 by Dave Taylor. All Rights Reserved.

Note: This web site is for the purpose of disseminating information for educational purposes, free of charge, for the benefit of all visitors. We take great care to provide quality information. However, we do not guarantee, and accept no legal liability whatsoever arising from or connected to, the accuracy, reliability, currency or completeness of any material contained on this web site or on any linked site. Further, please note that by submitting a question or comment you're agreeing to my terms of service, which are: you relinquish any subsequent rights of ownership to your material by submitting it on this site. My lawyer says "Thanks".
"Ask Dave Taylor®" is a registered trademark of Intuitive Systems, LLC.