Quickie: Batch Renaming Files in Linux

Quick and easy way to rename files

All Articles

Tags: quickie, linux, bash

For those wanting to be able to quickly and efficiently rename a bunch of files all at once, below is some example code to help you achieve that:


ls *.xml | sed 's/\(testing\)\(.*\)/mv \1\2 production\2/' | sh


The first piece ls *.xml simply lists the file types we want to change. You can, of course, alter this to anything you'd like.


The second part uses sed so search for a text and replace it with another. There are numerous regex tutorials around the net.


Within the second part, items are grouped so that I may reuse these items in the next part. Specifically, I am looking for (part1) the word "testing" and (part2) anything else. The mvcommand then takes as the first parameter the original two values to denote what file is to be changed and the second parameter of the command does not use the first variable but rather the word "production" and then appends whatever was in the second variable.


Part 3 simply executes this.


The end result makes a sample file named: testing_1.xml be production_1.xml