Search code examples
linuxbashfor-loopnestedcdo-climate

bash - for loop through multiple directories and their files


I have 5 directories in the same path my_path/:

my_path/1951 
my_path/1952 
my_path/1953 
my_path/1954 
my_path/1955

Each directory contains a different number of netcdf files ending in .nc.

And I need to perform a CDO command by looping in each directory and files. The command below applies only to the first directory and files my_path/1951/*.nc:

for i in my_path/1951/*.nc
do
   cdo selyear,1951/1970 "$i" "$i"2
done

The command select years from the nc files in the directory starting from the year of the directory and ending 20 years later.

So for the second directory it will be:

for i in my_path/1952/*.nc
do
   cdo selyear,1952/1971 "$i" "$i"2
done

and for the third:

for i in my_path/1953/*.nc
do
   cdo selyear,1953/1972 "$i" "$i"2
done

etc...

Is there a way I can do everything with a unique for loop or nested for loop instead of repeating the for loop above for each directory?


Solution

  • Would you please try the following:

    #!/bin/bash
    
    for i in my_path/*/; do
        year=${i%/}; year=${year##*/}       # extract year
        year2=$(( year + 19 ))              # add 19
        for j in "$i"*.nc; do
            echo cdo "selyear,${year}/${year2}" "$j" "$j"2
        done
    done
    

    It outputs command lines as a dry run. If it looks good, drop echo and run.