how can I select only specific .nc files from long list to work with cdo?

Viewed 87

How can I merge only specific .nc files to work with cdo? from a long list of files that look like this:

var_1-2.nc

var_3-4.nc

var_5-6.nc

.

.

var_99-100.nc

I would like to to a cdo mergetime only to the last e.g 10 files Anyone could help me with the syntax here?

3 Answers

CDO can accept wild cards. Based on what you have said the following should work:

cdo mergetime var_99*.nc out.nc

The correct syntax for this issue is:

cdo mergetime $(ls | tail -n 10) outfile.nc

Just to add to Rob's answer, it is perhaps more robust to do

cdo mergetime var_9?-*.nc out.nc

The ? can only be replaced by a single character so this would pick up all the 90s files but ignore var9-10.nc

Of course if you wanted only the last 7 files then this wouldn't work and your tail solution is better.

As Rob mentions in his comments, in general it is a good idea to pad file name indices with zeros when generating them, so that a list command like ls will give them in correct numerical order - you can do this with seq -w i1 i2 for example.

Related