Shell Scripting: Create Report / Log File Names With Date in Filenames

Posted on in Categories Debian Linux, Howto, Linux, RedHat/Fedora Linux, Shell scripting, Solaris, Suse Linux, Tips, Ubuntu Linux, UNIX last updated February 5, 2006

Sometime you need to create a shell scripts with output filenames with date in it. For example instead of log file named “secure.log”, you can create a filename called “secure-jan-02-06.log” or “secure-DATE-FORMAT.log”. The DATE-FORMAT can be set as per requirements:
=> dd-mm-yyyy
=> mm-dd-yyyy
=> yyyy-mm-dd
=> Month-Day-Year

The date in the filename will make it easy to find out all logs or reports. You can display the current date and time in the given FORMAT using date command. If you just type date command it will display in standard FORMAT:
$ date
Output:

Sun Feb  5 18:23:44 IST 2006

date Command Syntax

The syntax is as follows for GNU/date:

date +"FORMAT"

To display date in MONTH-DD-YYYY format use the date command as follows:
$ date +"%b-%d-%y"
Sample outputs:
Feb-05-06

Where, FORMAT can be any one of the following:

  • %a : Abbreviated weekday name (Sun..Sat)
  • %b : Abbreviated month name (Jan..Dec)
  • %B : Full month name, variable length (January..December)
  • %d : Day of month (01..31)
  • %e : Day of month, blank padded ( 1..31)
  • %m : Month (01..12)
  • %Y : Year
  • %d : Day of month (e.g, 01)
  • %H : 24 hour format (00..23)
  • %I : 12 hour format (01..12)
  • %j : day of year (001..366)
  • %D : date; same as %m/%d/%y
  • %F : full date; same as %Y-%m-%d (a good format for sorting filename)

Get the date in YYYY-mm-dd format

NOW=$(date +"%Y-%m-%d")

OR

NOW=$(date +"%F")

To create a file with date in filename, enter:

LOGFILE="log-$NOW.log"

To display filename, enter:

echo "$LOGFILE"

Sample Shell Script

#!/bin/sh
# Dump MySQL database every hour using cron
# Author: Vivek Gite
# See for more info: http://www.cyberciti.biz/tips/shell-scripting-creating-reportlog-file-names-with-date-in-filename.html
# ---------------------------------------------------------------------------------------------------------
 
## date format ##
NOW=$(date +"%F")
NOWT=$(date +"%T")
 
## Backup path ##
BAK="/nfs/backup/mysql/$NOW"
 
## Login info ##
MUSER="madmin"
MPASS="PASSWORD"
MHOST="127.0.0.1"
 
## Binary path ##
MYSQL="/usr/bin/mysql"
MYSQLDUMP="/usr/bin/mysqldump"
GZIP="/bin/gzip"
 
## Get database list ##
DBS="$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse 'show databases')"
 
## Use shell loop to backup each db ##
for db in $DBS
do
 FILE="$BAK/mysql-$db-$NOWT.gz"
 echo "$MYSQLDUMP -u $MUSER -h $MHOST -p$MPASS $db | $GZIP -9 > $FILE"
done

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

24 comment

  1. IMHU, to see a sorted list of log files the best date options is “+%Y%m%d%H%M%S”. Separate fields with hyphen could make it more human readable.

    When a lot of instances of your program is running simultaneously, may you should add the PID to this string.

    1. Exactly, what kind of idiot would use the MONTH-DAY-YEAR format for filenames? Once again the comment section has a higher quality than the article.

  2. Take a look at log4sh [http://log4sh.sourceforge.net/] as well. It might be a better choice for your logging needs.

  3. Okay, but how do I retrieve and format YESTERDAY’s date? Or, more generally, how do I perform date “arithmetic” from a shell script (adding days / weeks, etc)?

    1. Very late to answer this, but…

      One way to manipulate the date variable is:

      DAY=$(date +”%d”)
      DAY=$((DAY-1))
      echo $DAY

  4. I need to use gzip and make the date (month and year) part of the output filename.
    How can this be done.

    example ;

    input filename: FMMI.HHS.JJJJ
    FMMI.HHS.JJJJ

    output file name: FMMI.HHS.0112 (MMYY)

  5. My littel script now looks like this :

    #!/bin/bash
    
    NOW=$(date +"%d.%m.%Y")
    FILE=/tmp/$NOW.log
    
    df -ahl | grep -i sda1 > "$FILE"
    

    When i start the script manually i get “: command not found” two times, thats strange but not a big problem.

    More problematic is that the filename now looks like this “02.05.2012?.log??”
    Where do these questionmarks come from ? I don’t have a clue …

  6. Hi Vivek,

    yes, this is the complete script (for now).
    Other language ? Not that i know, but how can i find out for sure ?
    System is Suse Enterprise 9, 2.6.5-7.257 x86_64

  7. Gee … found it
    For editing purposes i put the script on a samba share, from there i edited it with windows & notepad… big mistake
    vi showed that every line had a “^M” at the end
    so, cleared the script and now it runs flawless 🙂
    my fault ! but thanks anyway 😉

  8. I have shell script that send the last week report to my clients. There i am using below way to get year & week of last week:

    DATE=`date +%Y-%U`

    But today that job got failed to fetch the data (Year=2013 & Week = 52) as now Year and Week Changes,
    PLease suggest how to handle this case?

  9. Dear Sir

    A text file create in /tmp path every day with date EX. – ank20151016 and next day it create ank20151017. Now I want to make the script that every day check that this file make or not & If this file make then it copy from /tmp to /home date wise by script.

    Please Help

Comments are closed.