0

I want to create a folder for every week of the year with the name of the folder name being the Monday date of that week.

For example from this week for the next weeks it would be: 02-22 03-01 03-08 03-15

1
  • Do you want to create all directories for a particular year right now, or do you want to create the directory for the current week now, if it doesn't exist already?
    – Kusalananda
    Feb 28, 2021 at 7:39

4 Answers 4

4
#!/bin/bash
   
for n in {1..52};
do
     mkdir $(date -d"$n+monday" +%m-%d)
done

OUTPUT:

03-01

03-08

03-15

ect.

This is will print the month-day for each Monday for the next 52 weeks but can easily be changed to any number of weeks.

1
  • This will also produce the output you asked for month-day no year but all of this is easily changed if need be you can change the 52 to any number of weeks or add a filed to the date command to add year or weekday name ect. Feb 27, 2021 at 17:01
2

You could make this shorter, but my goal was to make it understandable not short. I also assume you want all the Mondays of a given year, not the next year's worth of Mondays, and that you are on a GNU system or at least have access to the GNU implementation of date in addition to the GNU shell.

#!/bin/bash

year="${1}"

if [[ "${year}" = "" ]]; then
    # Default the year to this year
    year="$(date +"%Y")"
fi

# What day of the week is January 1 of the given year?
readonly jan_one_day_of_week="$(date -d ${year}-01-01 +"%a")"

# Based on what day January 1 is, figure out when the first Monday is
case "${jan_one_day_of_week}" in
"Mon") day=1 ;;
"Tue") day=7 ;;
"Wed") day=6 ;;
"Thu") day=5 ;;
"Fri") day=4 ;;
"Sat") day=3 ;;
"Sun") day=2 ;;
*)
    echo 1>&2 "Unexpected day of the week: ${jan_one_day_of_week}"
    exit 1
esac

# Start printing each Monday until the next Monday we encounter isn't in
# the same year.
week=0
while [[ "$(date -d "${year}-01-0${day} + ${week} weeks" +%Y)" = "${year}" ]]; do
    date -d "${year}-01-0${day} + ${week} weeks" +"%m-%d"
    week=$((week + 1))
done

If you pass it a year parameter, it will print the Mondays for that year. If you do not supply a parameter, it assumes this year.

The output looks like:

$ ./ex.sh
01-04
01-11
01-18
01-25
...
12-06
12-13
12-20
12-27
1

You can rely on the date command.

$ mkdir $(date +"%d-%m-%Y")

Sample output:

27-02-2021

You can then run this command once every week using a cron job. For example, run this command every monday morning at 6am.

2
  • Oh that's pretty simple, thanks! If I wanted to create it for the whole year in advance, how would I create a script for that?
    – thiswayup
    Feb 27, 2021 at 16:51
  • I added a script that will make a year at a time. Feb 27, 2021 at 16:58
1

To get the date in %Y-%m-%d format for all the Mondays in a given year, you can do:

year=2021 ksh93 -c '
  printf "%(%F)T\n" {1..53}"th Monday in January $year" |
    grep "^$year"'

(note that some years have 52 Mondays, and some years have 53).

To get that in %m-%d format and create a directory for each, you can pipe that output to cut -d- -f2- | xargs mkdir.

To create a directory in %Y/%m-%d format for the next 300 Mondays (including today if today is a Monday):

ksh93 -c 'printf "%(%Y/%m-%d)T\n" +{0..299}Monday' | xargs mkdir -p

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .