I have a number of log directories where I store log files where the directory name is the current year. On the last day of the old year, I want to create new directories named for the upcoming year. I use the following Bash script to create those directories. Since I have several email log directories, I use a for loop to create a directory named after the upcoming year in each one.
#!/bin/bash # # Create a new directory corresponding to the upcoming year on December 31 of # every year newyear=$(date --date=tomorrow +%Y) echo $newyear # Apache logs # Check on whether the directory for the current year exists and, if it doesn't, # create it. if [ ! -d /home/jdoe/www/logs/apache/"$newyear" ]; then mkdir /home/jdoe/www/logs/apache/"$newyear" chown apache /home/jdoe/www/logs/apache/"$newyear" chgrp apache /home/jdoe/www/logs/apache/"$newyear" fi # Email logs # Create an array holding the names of the 4 directories within which # subdirectories will be created using the name of the new year. E.g., # /home/jdoe/www/logs/mail/sendmail_stats/2019 logdirs=( "dnsbl_count" "sendmail_stats" "smlogstats" "smreject" ) for i in "${logdirs[@]}" do : # Check on whether the directory for the current year exists and, if it # doesn't, create it. if [ ! -d /home/jdoe/www/logs/mail/$i/"$newyear" ]; then mkdir /home/jdoe/www/logs/mail/$i/"$newyear" chown jdoe /home/jdoe/www/logs/mail/$i/"$newyear" chgrp jdoe /home/jdoe/www/logs/mail/$i/"$newyear" fi done
I then have a crontab entry containing the following line that will result in the Bash script above, named end-of-year-dirs, being run at 7;00 AM on December 31 of each year.
0 7 31 DEC * /root/bin/end-of-year-dirs
Related articles: