Linux: how to batch rename files, truncate characters in the middle, and end filename

I have about a thousand files that I need to rename. All filenames look like this:

XXXXppppppp-ppppp_S !! _ L001XXX_001.extension

Where X is the information I need to save and! is a sequentially increasing number (i.e. 01, 02 ... 99).

I really can't outline the rename command and how to achieve what I need to do. Basically, I want to keep the first four characters, remove the next 22, keep the next 3, and remove the ending 4. I would also like to keep the extension.

Thank!

+3


source to share


1 answer


Try this:

rename -n 's/^(.{4}).{22}(.{3}).{4}(\..*)/$1$2$3/' *

      



Make sure your command rename

is perl, sometimes another one is installed there. Nice is sometimes called prename

.

When the tests are in order, remove the switch -n

(dry mode).

+2


source







All Articles