Quickie: Batch Renaming Files in Linux
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 mv command 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
- Login to post comments


![[FSF Associate Member] [FSF Associate Member]](http://www.ossolutions.org/lores/img/fsfMember.png)
Subscribe to this Feed