bashloopsmkdir

Bash Syntax error with multiple two paramter mkdir


mkdir ./output
for year in {1990..2025}
do
    mkdir ./$year \n
   for week in {1..52}
        mkdir ./"$year"/"$week" \n
   done
done

gives syntax error on second call of mkdir in innerloop:

    mkdir ./"$year"/"$week" \n

Solution

  • You were missing do in the for loop. Here's the fixed code:

    
    mkdir ./output
    cd ./output ## so that we create the other dirs within output dir
    for year in {1990..2025} ; do
      mkdir ./$year
      for week in {01..52} ; do  ## do was missing here. Also 01 to pad with 0
        mkdir ./"$year"/"$week"
      done
    done
    
    
    

    Here cd ./output so that the year/week directories are created within output directory. The zero padding in {01..52} results in one-digit numbers zero padded.