Skip navigation.
Related Links
How to batch rename files

How to batch rename files

19
Apr
2008

This afternoon I needed to rename a bunch of files from one form to another in a command shell… Well technically I didn't need to do it in a shell - but, as sure as there is a hole in my ass, I wasn't gonna go through renaming them all manually!

They needed to go from, for example, add.png to add_32.png. After a little research into commands like printf, awk, bison and so on - I suddenly realized that 'cut' held the key!

for i in *; do j=`echo $i | cut -d . -f 1`; j=$j"_32.png"; mv $i $j; done

Basically, this says "for every file in the folder, cut the filename on all dots and take the first result into variable 'j'. Then append '_32.png' onto the end of the variable. Finally move the original file to the new filename".

I hope this saves someone else a bit of time!

It'd be nice if you asked before taking stuff from my site. Contact me at webmaster [at] thingy - ma - jig . co . uk

This site was based on the Cobalt 2.0 Theme for phpBB written by Jakob Persson

Free MiniMac

Free MiniMac

Social Statistics
Search
Google



Weblinks

Add to Technorati Favorites

TGC Webring

CMS Drupal Showcase

Feedburner for ThingyMaJig

View Nicholas Thompson's profile on LinkedIn

My Twitter
  • Loading Twitter
bile-edge