(OLDER) <- More Stuff -> (NEWER) (NEWEST)
Printer Friendly Version



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.


Technorati tags:


Click here to add your comments





Mon May 4 21:26:20 2009: Subject:   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: Subject:   TonyLawrence

gravatar
The answer to your question is right on this page. If you aren't able to understand it, please see http://aplawrence.com/rightnow.html for paid assistance.

Don't miss responses! Subscribe to Comments by RSS or by Email

Click here to add your comments


If you want a picture to show with your comment, go get a Gravatar



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. We appreciate comments and article submissions.

Publishing your articles here

Jump to Comments



Many of the products and books I review are things I purchased for my own use. Some were given to me specifically for the purpose of reviewing them. I resell or can earn commissions from the sale of some of these items. Links within these pages may be affiliate links that pay me for referring you to them. That's mostly insignificant amounts of money; whenever it is not I have made my relationship plain. I also may own stock in companies mentioned here. If you have any question, please do feel free to contact me.

Specific links that take you to pages that allow you to purchase the item I reviewed are very likely to pay me a commission. Many of the books I review were given to me by the publishers specifically for the purpose of writing a review. These gifts and referral fees do not affect my opinions; I often give bad reviews anyway.

We use Google third-party advertising companies to serve ads when you visit our website. These companies may use information (not including your name, address, email address, or telephone number) about your visits to this and other websites in order to provide advertisements about goods and services of interest to you. If you would like more information about this practice and to know your choices about not having this information used by these companies, click here.


book graphic unix and linux troubleshooting guide

My Troubleshooting E-Book will show you how to solve tough problems on Linux and Unix systems!



 I sell and support
 Kerio Mail server




pavatar.jpg
More:
       - Linux
       - Unix


Unix/Linux Consultants

Skills Tests

Guest Post Here











My Favorites

Change Congress