Shell – Renaming Multiple Files with Rename Command

renameshellshell-script

I need to rename files in batch–the other questions I browsed don't exactly address my problem. The names of my files are generated non-deterministically, so I can't predict what they will be named. I do know that they will start with NORMAL and end with -lib*. I'd like to replace everything in between with some string X. For example,

|   |   |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib1.concordant
|   |   |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib1.deletion
|   |   |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib1.divergent
|   |   |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib1.inversion
|   |   |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib1.translocation
|   |   |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.concordant
|   |   |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.deletion
|   |   |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.divergent
|   |   |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.insertion
|   |   |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.inversion
|   |   |-- NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.translocation

So it will probably be of the form rename "s/something/X/", but I don't know what that something should be, as I don't know how to use regex.

Best Answer

If you are lucky enough to have rename available, then the following should be sufficient:

rename 's/(NORMAL).*(-lib)/$1X$2/' *
Related Question