0

I got the first section to create the years, but getting the months and incrementing them is stumping me.

#!/bin/bash
varRan=$(( $RANDOM ))
newdir="testing_"$varRan
#echo  $newdir

mkdir $newdir
cd ~/$newdir
for   ((i = 2000; i<= 2022 ; i++; )); do
        for  ((m = 1; m<= 12 ; m++; )) do
        touch $i$m".txt"
done
#       touch $i$m".txt"
done
1
  • printf: printf -v filename '%4d%02d.txt' "$i" "$m"; touch "$filename" – glenn jackman Jan 12 at 14:05
1

Using a shell expansion with a leading zero does what you need.

#/bin/bash

newdir="testing_$RANDOM"

mkdir "${newdir}"; cd "${newdir}"

for y in {2000..2022}; do
    for m in {01..12}; do
        touch "prefix_$y$m.txt"
    done
done
3
  • 2
    Or touch prefix_20{00..22}{01..12}.txt – Freddy Jan 11 at 23:08
  • Freaking awesome. Thanks. – Christopher Simpson Jan 11 at 23:33
  • @Freddy: Much better than mine. Succinct, saves running 275 processes, and no issues with length of command line args. – Paul_Pedant Jan 11 at 23:33
0

You can use date for generating dates in shell with custom format

$ date +%Y-%m-%d -d "2002-03-28 +1 days" 
2002-03-29

$ date +%Y-%m-%d -d "2002-03-28 +1 months"
2002-04-28

$ date +%Y-%m-%d -d "2002-03-28 +1 years" 
2003-03-28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.