|
|
How do I rename hundreds of files at once?Dave, I'm in a bit of a pickle. Like you, I'm an author, and as I wrap up my latest book project, the development editor sent me a note saying that all the figures I've submitted, all 217 of them, are incorrectly named and need to be fixed. Augh! The naming scheme I'm using is FIGxxyy.TIF where xx is the chapter number and yy is the figure number. To make it a bit more complex, sometimes I use a three digit figure number and other times I use a two digit value. I need all the files to be called xxFIGyyy.TIF. How do I do that without going crazy?
I can relate to your pain! I find it amazing that it's usually at least half way through a book that a development editor tells me that I've been using the wrong naming scheme for the chapters or figures all along. The chapters aren't too bad, there are usually only 15-25 in a book total, but figures, well, I can have hundreds of 'em too. To solve your problem, I noticed that you have a nice, regular pattern that you're using and that creating the new file names is purely dependent on being able to extract the specific information from the old file names. This is where the Linux (and Mac OS X) cut command is a great tool. To extract just the third and fourth characters, for example: cut -c3-4 works just fine. The basic logic of this solution then is to, in a loop, extract the chapter and figure numbers from the existing filename, normalize the figure number to three digits, then create a new filename by putting those informational items in a different order. Make sense? Here's my short little script to do the job: #!/bin/sh
for name in *TIF
do
chap="$(echo $name | cut -c3-4)"
fig="$(echo $name | cut -c5- | cut -d. -f1)"
if [ $(echo $fig | wc -c) -eq 3 ] ; then
fig="0$fig"
fi
# change the following 'mv' to a 'cp' for insurance
mv $name "${chap}FIG${fig}.TIF"
done
exit 0
As you can see here, the chapter number is extracted by pulling out the third and fourth characters, and the figure number is extracted by discarding the first four characters of the filename, then grabbing everything up to, but not including, the '.' separator. The wc invocation is a lazy way to figure out how many characters are in the figure value $fig (including a carriage return added by the echo statement). If it's three, that is, two plus the carriage return, then simple preface an additional zero to normalize them all to three character figure values. Finally, the new filename is built with ${chap}FIG${fig}.TIF, where the curly braces are required to clearly delimit variables names versus characters to actually include in the filename itself. Save this script to a text file while in an xterm, or the Mac Terminal application, then, presuming you named it fixnames, run it with: sh fixnames in the directory that contains your existing figures. Within just a second or two all the files should be renamed properly and you should be good to go. Good luck!
Related 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!
Categorized:
Shell Script Programming
,
The Writing Business
(Article 3826,
Written by Dave Taylor)
Tagged: Previous: What is BBCode and how do I use it? Next: How do I get past single user mode in Mac OS X? Reader Comments To Date: 9Dave Taylor said, on December 30, 2004 3:09 PM:
I haven't used any Windows app that makes bulk renaming easier, but a quick check at Download.com suggests that there are some possibilities worth exploring: Rename It: Hope that one of those helps! Lazy Dog said, on January 14, 2005 5:23 AM:
I recently downloaded Lupas Rename which was mentioned in Fred Langa's newsletter. I have not used it yet, but it offers a lot of options. Jonathan said, on July 5, 2006 6:54 PM:
Dave, Thanks for the great scripts/website. I love 101 Wicked Cool Shell Scripts. I just wanted to add my two cents to this discussion for anybody searching the web for this solution. I created a Workflow in Automator to accomplish the same thing. Just choose Finder from the Automator library and use the Action Rename Finder Items. It works like a champ. Jonathan Tips said, on September 6, 2006 3:04 PM:
You might be able to get away with a variation of something like this:: To bulk rename files on Windows, install Cygwin and/or Perl. Tips said, on September 6, 2006 3:07 PM:
Sorry, that last comment should have said: ruchi said, on May 11, 2007 1:09 PM:
Your suggestions are really helpful. siva said, on December 9, 2009 11:17 PM:
hai i have a no.of files like 'Billing_TXT_99999999_' John MacAulay said, on September 26, 2011 2:46 PM:
Hi, Dave - Your site's a great resource - thank you. In your solution, the 'if' statement can be ditched completely by forcing the shell to treat the fig variable as a 3-digit integer with leading zeroes. Insert this line before the for loop: typeset -Z3 fig I'm a bit perplexed about the main body of your solution, though: the original poster said that his figure naming scheme was FIGxxyy.TIF, so shouldn't your lines read: chap="$(echo $name | cut -c4-5)" Or am I missing something? Cheers, John
I do have a comment, now that you mention it!Check This Out Too... |
Recent Entries
Look for Answers
Recommended
All Our Categories
Apple iPad Help
Articles and Reviews Auctions and Online Shopping Blogs and Blogging Building Web Site Traffic Business and Management Computer and Internet Basics d) None of the Above Facebook Help Google Gmail Help Google Plus Help HTML, JavaScript and Web Site Programming Industry News and Trade Shows iPhone and Cell Phone Help iPod, Sony PSP and MP3 Player Help Kindle Fire Help Mac OS X Help Pay Per Click (PPC) Advertising Pinterest Help Search Engine Optimization (SEO) Shell Script Programming Tech Support Video Help The Writing Business Twitter, LinkedIn and Social Network Help Unix and Linux Help Video Game Tips and Help Windows PC Help Find Me on Google+ ADT on G+ |
Great. Thanks. But do you have any input on accomplishing this on the Windows platform?