Using bash command rename to rename multiple files at once

Using rename against multiple patterns on Ubuntu version=”15.04 (Vivid Vervet)”

So I had several folders of files where I wanted to rename them all cutting the cruft off the beginning and end of the file. Fortunately the part of the file that I wanted to keep was also incrementing.

Probably a thousand ways to do this but rename is so easy.

A few options to consider when using the rename command.
-v: Print names of files successfully renamed.
-n: Show what files would have been renamed.
-f: Force overwrite existing files.

The rename command uses perlexpr: Perl Expression.

The issue:

Example files:
xxxxxxxxxText01yyyyyyyyyy.zip
xxxxxxxxxText02yyyyyyyyyy.zip
xxxxxxxxxText03yyyyyyyyyy.zip

First we can do a test run by specifying the n option.
rename -n ‘s/xxxxxxxxx//g’,’s/yyyyyyyyyy//g’ *.zip
rename(xxxxxxxxxText01yyyyyyyyyy.zip, Text01.zip)
rename(xxxxxxxxxText02yyyyyyyyyy.zip, Text02.zip)
rename(xxxxxxxxxText03yyyyyyyyyy.zip, Text03.zip)

We see the results are what we are after and now what we need so to complete the rename.

rename ‘s/xxxxxxxxx//g’,’s/yyyyyyyyyy//g’ *.zip

ls -ltra
Text03.zip
Text02.zip
Text01.zip

That is about as simple as it can be.

This entry was posted in bash, Linux. Bookmark the permalink.

Leave a Reply

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


*