≡ Menu

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

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

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:

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")


NOW=$(date +"%F")

To create a file with date in filename, enter:


To display filename, enter:

echo "$LOGFILE"

Sample Shell Script

# 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 ##
## Login info ##
## Binary path ##
## 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
 echo "$MYSQLDUMP -u $MUSER -h $MHOST -p$MPASS $db | $GZIP -9 > $FILE"
Share this on:

Your support makes a big difference:
I have a small favor to ask. More people are reading the nixCraft. Many of you block advertising which is your right, and advertising revenues are not sufficient to cover my operating costs. So you can see why I need to ask for your help. The nixCraft, takes a lot of my time and hard work to produce. If you use nixCraft, who likes it, helps me with donations:
Become a Supporter →    Make a contribution via Paypal/Bitcoin →   

Don't Miss Any Linux and Unix Tips

Get nixCraft in your inbox. It's free:

Comments on this entry are closed.

  • andreyev June 14, 2006, 6:21 pm

    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.

    • Bernd September 12, 2011, 4:47 pm

      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.

      • nixCraft September 13, 2011, 5:28 am

        No need to be rude. Each setup has unique requirements. So I used MM-DD-YYYY format, but that doesn’t mean I’m an idiot.

        Have a nice day!

  • Kate Ward January 18, 2007, 8:15 pm

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

  • Aaron October 8, 2007, 6:44 pm

    Awesome stuff, you have save me tons of time

  • Itpatil May 13, 2008, 5:19 pm

    Thank you. Very helpful.

  • Phil May 16, 2008, 1:10 pm

    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)?

    • Poli July 26, 2011, 9:03 am

      What about to start apps via script and set today’s date to log at the time of app start?

  • nixCraft May 16, 2008, 1:16 pm


    Try yest

  • Spenser January 2, 2010, 4:28 pm

    Exactly what I needed. Thanks!

  • richa March 11, 2010, 6:09 am

    very nice and helpful…thanks

  • prasanna March 2, 2011, 5:46 am

    how to create log/files using yesterday,s date….

    • mule.ear October 19, 2011, 6:17 pm

      Very late to answer this, but…

      One way to manipulate the date variable is:

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

  • John June 9, 2011, 4:39 am


    This helped me heaps with what i was trying to do.

  • Habeeb October 8, 2011, 11:40 pm

    Thanks man you are awesome…….

  • Priyanka November 23, 2011, 8:25 am

    Great code..Helped a lot !! Thanx Vivek!!!

  • Walt January 13, 2012, 1:47 pm

    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

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

  • Memscan April 5, 2012, 3:19 pm

    I know it is a old post but thanks for the info anyway. Very helpful.

  • Marc May 2, 2012, 1:08 pm

    My littel script now looks like this :

    NOW=$(date +"%d.%m.%Y")
    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 …

    • nixCraft May 2, 2012, 1:18 pm

      Is this a complete script? Also, are you using a language other than English?

  • Marc May 3, 2012, 5:11 am

    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

  • Marc May 3, 2012, 5:18 am

    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 ;)

  • Anoor January 1, 2014, 5:04 pm

    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?

  • ankur October 16, 2015, 1:45 pm

    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