Home > OS >  bash - for loop through multiple directories and their files
bash - for loop through multiple directories and their files

Time:03-10

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?

CodePudding user response:

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.

CodePudding user response:

for x in $@
 for i in my_path/$x/*.nc
  ...

you can do On2 for with getting parameters of sh script.

yourScript.sh 1951 1952 ...

After that you can pass all folder to your script.

yourScript.sh ./*

CodePudding user response:

You could loop like this:

shopt -s nullglob
for i in my_path/195[1-5]/*.nc
do
  ...
done 

The shopt command ensures that the loop works even if some of the directories don't contain a nc file.

  • Related