I am working with thousands of files whose names contain sequential dates ranging from 2001-01-01 to 2020-12-31.
A sample of such files can be seen below:
gpm_original_20010101.nc
gpm_cressman_20010101_cor_method-add_fac-0.5_pass-1_radius-500km.nc
gpm_cressman_20010101_cor_method-add_fac-0.5_pass-2_radius-250km.nc
gpm_cressman_20010101_cor_method-add_fac-0.5_pass-3_radius-150km.nc
gpm_cressman_20010101_cor_method-add_fac-0.5_pass-4_radius-75km.nc
gpm_cressman_20010101_cor_method-add_fac-0.5_pass-5_radius-30km.nc
.
.
.
gpm_original_20010131.nc
gpm_cressman_20010131_cor_method-add_fac-0.5_pass-1_radius-500km.nc
gpm_cressman_20010131_cor_method-add_fac-0.5_pass-2_radius-250km.nc
gpm_cressman_20010131_cor_method-add_fac-0.5_pass-3_radius-150km.nc
gpm_cressman_20010131_cor_method-add_fac-0.5_pass-4_radius-75km.nc
gpm_cressman_20010131_cor_method-add_fac-0.5_pass-5_radius-30km.nc
and so on until 2020-12-31
. What I need to do is to reorganize these files into new folders based on years and months.
The directory tree needs to follow the logic year
with sub-directories months
, like this:
2001
01
02
03
04
05
06
07
08
09
10
11
12
2002
01
02
03
04
05
06
07
08
09
10
11
12
and so on. And the files should be moved to these directories based on the equivalent date in their filenames. For example: all files containing 200101xx
in their names should be moved to the 2001/01
folder.
What is the most straightforward way to achieve this using bash?
[_.]
? I'm not saying it's wrong, just trying to understand. An elegant answer, BTW. – spuck Jan 19 '21 at 15:55XXXXXXXX_
andXXXXXXXX.
, so both have to be taken into account. Is that what you're asking? – schrodingerscatcuriosity Jan 19 '21 at 16:00_([0-9]{8})
would be sufficient. I was wondering why to also match the underscore or period. – spuck Jan 20 '21 at 17:15_123456789
in its name, it will match but it's not 8 digits, it has 9 digits. I know it seems not the case in OP's file names, but you never know. You could be go even further and verify that the pattern is indeed a date... but that would be more complicated stuff... and well, takes much more time :). – schrodingerscatcuriosity Jan 20 '21 at 17:30