APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed

renaming files

© August 2005 Tony Lawrence

I'm not sure how long "rename" has been hanging around Linux. I don't mean the "rename" system call, but the command line version that lets you (for example), rename your .htm files as .html as easily as:

rename .htm .html *.htm

That's pretty easy. On other Unixes, we'd have to do something like this to batch rename files:

for i in *.html
  j=`echo $i | sed 's/.htm$/.html/'`
  # or, in this simple case even just: j=$"i"l
  mv $i $j

Though if we had bash or ksh, we could make that a little less cumbersome:

for i in *.htm
  # or: j=${i}l
  mv $i $j

The Linux "rename" isn't going to handle to more complex cases though. For example, I had to transfer mail files from one system to another recently. On the old system, each message would be named something like "1124993500.7359464636.e-smith". On the new system, they'd be "00000001.eml", with hexadecimal numbering going on up, so you'd get "00000009.eml" and the next message would be "0000000a.eml", and so on. There's no way for "rename" to do that, but a loop can:

for i in *
  j=`printf "%0.9x.eml\n" $x`
  mv $i $j

I actually did that in Perl rather than the shell:

while (<*>) {

But the result is the same.

Windows XP can actually do something vaguely similar to this: if you select a bunch of files, then right click on the first and choose "rename", all the files will be renamed, but with sequential numbers assigned after the first. That wouldn't be useful for our original "htm" to "html" need, so if you do a Google search for "rename files", you'll see lots of ads for batch renamers. Amusingly enough, such things are even offered for Mac OS X, which doesn't need them because you can fire up Terminal and use scripts like these. As it's easy enough to put Perl on Windows, you really don't need them there either.

Got something to add? Send me email.

(OLDER)    <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

-> renaming files


Inexpensive and informative Apple related e-books:

Take Control of Preview

Are Your Bits Flipped?

Take Control of Automating Your Mac

Take Control of OS X Server

iOS 10: A Take Control Crash Course

More Articles by © Tony Lawrence

Mon May 4 21:26:20 2009: 6318   Cristobal

Hi there, i need to batch rename an image sequence, that is name.#.ext and I'm not shure how to solve it. This is the case:

Files are named as:

And i need them to be named as:

What would you do? Thank you in advance :)

Mon May 4 21:29:56 2009: 6319   TonyLawrence

The answer to your question is right on this page. If you aren't able to understand it, please see (link) for paid assistance.


Printer Friendly Version

Have you tried Searching this site?

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us

Printer Friendly Version

Linux source code is freely and easily available to all of us. Understanding it is much harder. (Tony Lawrence)

Linux posts

Troubleshooting posts

This post tagged:



Unix/Linux Consultants

Skills Tests

Unix/Linux Book Reviews

My Unix/Linux Troubleshooting Book

This site runs on Linode