rename mass/bulk filenames using rename command via regexp

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:

 <pre class="src src-sh">rename -v <span style="color: #ffa07a;">'s/fulltext.pdf.([0-9]+)$/fulltext$1.pdf/'</span> fulltext.pdf.*

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

 <pre class="src src-sh">rename -v <span style="color: #ffa07a;">'s/.AVI/.avi/'</span> *.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.

About Vinh Nguyen


Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>