Ubuntu – Renaming multiple files with complex numbered names

batch-renamecommand linemvrename

I have 365 files named

MOD11A1.A2010001.h24v06.006.2016025150444.hdf
MOD11A1.A2010002.h24v06.006.2016025151748.hdf
MOD11A1.A2010003.h24v06.006.2016025163706.hdf

and I want to rename these files as:

modis1.hdf
modis2.hdf
modis3.hdf

I am using the mv command separately for each file to rename them.

How do I achieve this in one go?

Best Answer

Since you only care about the A2010... bit, using rename in the directory with the files:

rename -n 's/.*A2010*(\d+).*/modis$1.hdf/' *

Example:

$ rename -n 's/.*A2010*(\d+).*/modis$1.hdf/' *
rename(> MOD11A1.A2010001.h24v06.006.2016025150444.hdf, modis1.hdf)
rename(> MOD11A1.A2010002.h24v06.006.2016025151748.hdf, modis2.hdf)
rename(> MOD11A1.A2010003.h24v06.006.2016025163706.hdf, modis3.hdf)
rename(> MOD11A1.A2010365.h24v06.006.2016025150444.hdf, modis365.hdf)

Run without -n to actually rename the files.

Related Question