Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading