MoonPoint Support Logo

 


Shop Amazon Warehouse Deals - Deep Discounts on Open-box and Used ProductsAmazon Warehouse Deals



Advanced Search
January
Sun Mon Tue Wed Thu Fri Sat
   
   
2019
Months
Jan
May Jun
Jul Aug Sep
Oct Nov Dec


Tue, Jan 01, 2019 10:42 pm

Creating new directories for a new year

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:

  1. Loop through an array in Bash
  2. CRONTAB
  3. crontab -e
  4. smlogstats

[/os/unix/bash] permanent link

Once You Know, You Newegg AliExpress by Alibaba.com

Shop Amazon Local - Subscribe to Deals in Your Neighborhood

Valid HTML 4.01 Transitional

Privacy Policy   Contact

Blosxom logo