rename mass/bulk filenames using rename command via regexp


Vinh Nguyen


July 28, 2010

tag: rename, files, filenames, mass, bulk, multiple, linux, regexp, regex, regular expressions, perl

I've always been aware of rename in Linux. It uses perl's regular expression to rename multiple files.

For example, suppose I have a list of files fulltext.pdf.1, fulltext.pdf.2, …, fulltext.pdf.10, …, and I want to rename them to fulltext1.pdf, etc. In the shell, do:

rename -v 's/fulltext.pdf.([0-9]+)$/fulltext$1.pdf/' fulltext.pdf.*

It goes s/old/new/ to change. This is a good tutorial on regexp. Some other examples to remind me:

rename -v 's/.AVI/.avi/' *.AVI
rename -v 's/[]//' *.avi
rename -v -n 's/(D*)([0-9]+).avi$/$2_$1.avi/' *.avi
rename -v -n 's/([a-z|A-Z|_])([0-9])./$1_0$2./' *.avi ## one number padded with a 0 before it, or use sprintf in a real perl script

'-n' is to test and not actually do.