rulururu

post Rename multiple files with a Regular Expression (Regex)

July 25th, 2007

Filed under: Linux, Operating Systems — Brenton Alker @ 17:00

It is often useful to be able to rename multiple files at the same time. Typical cases include changing a file extension (eg. *.jpeg to *.jpg) or renaming a set of files moved from one server to another (such as when repairing database replication, eg. slave1-bin.* to slave2-bin.*)

It is possible to write a shell script that will perform this task, but the ones I have seen are cumbersome and not very intuitive.

There is a script, I believe it comes bundles with Perl installation (at least on Debian), that allows files to be renames using regular expressions. It is simple to use, but lets you harness the power of perl regular expressions.

The basic syntax that I use most often involves using a substitute command (similar to those used in sed for example)

eg: Rename all jpeg images to jpg files in the images directory.

rename 's/\.jpeg$/.jpg/' images/*

Mmm… Regex…

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

ruldrurd
Powered by WordPress, Web Design by Laurentiu Piron Monitored by SiteUpTime
Entries (RSS) and Comments (RSS)