Ask Dave Taylor
  • Facebook
  • Instagram
  • Linkedin
  • Pinterest
  • Twitter
  • YouTube
  • Home
  • YouTube Videos
  • Top Categories
  • Subscribe via Email
  • Ask A Question
  • Meet Dave
  • Home
  • Linux Shell Script Programming
  • How do I rename hundreds of files at once?

How do I rename hundreds of files at once?

December 26, 2004 / Dave Taylor / Linux Shell Script Programming, Writing and Publishing / 10 Comments

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!

Let’s Stay In Touch!

Never miss a single article, review or tutorial here on AskDaveTaylor, sign up for my fun weekly newsletter!
Name: 
Your email address:*
Please enter all required fields
Correct invalid entries
No spam, ever. Promise. Powered by FeedBlitz
Please choose a color:
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!

10 comments on “How do I rename hundreds of files at once?”

  1. John MacAulay says:
    September 26, 2011 at 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)”
    fig=”$(echo $name | cut -c6- | cut -d. -f1)”
    Or am I missing something?
    Cheers,
    John

    Reply
  2. siva says:
    December 9, 2009 at 11:17 pm

    hai i have a no.of files like ‘Billing_TXT_99999999_’
    i want to change ‘Billing_TXT_8676C_’ how to rename no of files

    Reply
  3. ruchi says:
    May 11, 2007 at 1:09 pm

    Your suggestions are really helpful.
    Thank you,

    Reply
  4. Tips says:
    September 6, 2006 at 3:07 pm

    Sorry, that last comment should have said:
    rename -v ‘s/FIG(\d{2})(\d{2,3})\.TIF$/$1FIG$2\.TIF/’ *.TIF

    Reply
  5. Tips says:
    September 6, 2006 at 3:04 pm

    You might be able to get away with a variation of something like this::
    rename -n ‘s/FIG(\d{2})(\d{2,3})\.TIF$/$1FIG$2\.TIF/’ *.TIF
    To bulk rename files on Windows, install Cygwin and/or Perl.

    Reply
  6. Jonathan says:
    July 5, 2006 at 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

    Reply
  7. Lazy Dog says:
    January 14, 2005 at 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.
    http://www.azheavymetal.com/~lupasrename/lupasrename.php

    Reply
  8. Lockergnome's Web Developers says:
    January 4, 2005 at 12:27 am

    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,…

    Reply
  9. Dave Taylor says:
    December 30, 2004 at 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:
    http://www.download.com/Rename-It/3000-2248_4-10148114.htm
    File and MP3 Renamer 2004:
    http://www.download.com/File-and-MP3-Renamer-2004/3000-2248_4-10316856.html
    A.F.5 Rename your files:
    http://www.download.com/A-F-5-Rename-your-files/3000-2248_4-10135890.html
    Renamer 5.0:
    http://www.download.com/Renamer/3000-2248_4-10341158.html
    File Monkey:
    http://www.download.com/FileMonkey/3000-2248_4-10267483.html
    Hope that one of those helps!

    Reply
  10. Navid Azimi says:
    December 29, 2004 at 6:22 pm

    Great. Thanks. But do you have any input on accomplishing this on the Windows platform?

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Search

Recent Posts

  • How Do You Rearrange App Icons on an Android Phone?
  • How Can I Enable Emergency Alerts in Spanish on Android?
  • Switch from 24-Hour Time to AM/PM in Ubuntu Linux?
  • Protect Your Connection and Privacy with Surfshark VPN
  • Can I Send Texts in iMessage with Effects from my Mac System?

On Our YouTube Channel

Monoprice DT-3BT Bluetooth Desktop Speakers -- REVIEW

FATORK Wi-Fi Smart Portable Movie Projector -- DEMO & REVIEW

Categories

  • AdSense, AdWords, and PPC Help (106)
  • Amazon, eBay, and Online Shopping Help, (161)
  • Android Help (203)
  • Apple iPad Help (145)
  • Apple Watch Help (53)
  • Articles, Tutorials, and Reviews (344)
  • Auto Tech Help (12)
  • Business Advice (199)
  • Chrome OS Help (25)
  • Computer & Internet Basics (764)
  • d) None of the Above (165)
  • Facebook Help (383)
  • Google, Chrome & Gmail Help (180)
  • HTML & Web Page Design (245)
  • Instagram Help (48)
  • iPhone & iOS Help (607)
  • iPod & MP3 Player Help (173)
  • Kindle & Nook Help (93)
  • LinkedIn Help (85)
  • Linux Help (167)
  • Linux Shell Script Programming (87)
  • Mac & MacOS Help (895)
  • Most Popular (16)
  • Outlook & Office 365 Help (26)
  • PayPal Help (69)
  • Pinterest Help (53)
  • Reddit Help (18)
  • SEO & Marketing (81)
  • Spam, Scams & Security (93)
  • Trade Show News & Updates (23)
  • Twitter Help (217)
  • Video Game Tips (66)
  • Web Site Traffic Tips (62)
  • Windows PC Help (922)
  • Wordpress Help (204)
  • Writing and Publishing (72)
  • YouTube Help (46)
  • YouTube Video Reviews (159)
  • Zoom, Skype & Video Chat Help (57)

Archives

Social Connections:

Ask Dave Taylor


Follow Me on Pinterest
Follow me on Twitter
Follow me on LinkedIn
Follow me on Instagram


AskDaveTaylor on Facebook



microsoft insider mvp


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 site or on any linked site. Further, please note that by submitting a question or comment you're agreeing to our terms of service, which are: you relinquish any subsequent rights of ownership to your material by submitting it on this site. Our lawyer says "Thanks for your cooperation."
© 2022 by Dave Taylor. "Ask Dave Taylor®" is a registered trademark of Intuitive Systems, LLC.
Privacy Policy - Terms and Conditions - Accessibility Policy