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

renaming files

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
do
  j=`echo $i | sed 's/.htm$/.html/'`
  # or, in this simple case even just: j=$"i"l
  mv $i $j
done
 

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

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

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 *
do
  j=`printf "%0.9x.eml\n" $x`
  mv $i $j
  x=$((x+1))
done
 

I actually did that in Perl rather than the shell:

#!/usr/bin/perl
$x=1;
while (<*>) {
 $new=sprintf("%0.9x.eml",$x);
 rename($_,$new);
 $x++;
}
 

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.



2 comments



Increase ad revenue 50-250% with Ezoic


More Articles by

Find me on Google+

© Tony Lawrence







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

gravatar
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:
name1.0000.ext

And i need them to be named as:
name2.0000.ext

What would you do? Thank you in advance :)



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

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

------------------------
Kerio Samepage


Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

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





A common and not necessarily apocryphal example portrays a solo practitioner starved for business in a small town. A second lawyer then arrives, and they both prosper. (Deborah L. Rhode)

One of the main causes of the fall of the Roman Empire was that, lacking zero, they had no way to indicate successful termination of their C programs. (Robert Firth)







This post tagged: