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:
# ---------------------------------------------------------------------------------------------------------
## 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"

🥺 Was this helpful? Please add a comment to show your appreciation or feedback.

nixCrat Tux Pixel Penguin
Hi! 🤠
I'm Vivek Gite, and I write about Linux, macOS, Unix, IT, programming, infosec, and open source. Subscribe to my RSS feed or email newsletter for updates.

Comments on this entry are closed.

  • andreyev Jun 14, 2006 @ 18:21

    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 Sep 12, 2011 @ 16:47

      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.

      • 🛡️ Vivek Gite (Author and Admin) nixCraft Sep 13, 2011 @ 5:28

        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 Jan 18, 2007 @ 20:15

    Take a look at log4sh [] as well. It might be a better choice for your logging needs.

  • Aaron Oct 8, 2007 @ 18:44

    Awesome stuff, you have save me tons of time

  • Itpatil May 13, 2008 @ 17:19

    Thank you. Very helpful.

  • Phil May 16, 2008 @ 13:10

    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 Jul 26, 2011 @ 9:03

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

  • 🛡️ Vivek Gite (Author and Admin) nixCraft May 16, 2008 @ 13:16


    Try yest

  • Spenser Jan 2, 2010 @ 16:28

    Exactly what I needed. Thanks!

  • richa Mar 11, 2010 @ 6:09

    very nice and helpful…thanks

  • prasanna Mar 2, 2011 @ 5:46

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

    • mule.ear Oct 19, 2011 @ 18:17

      Very late to answer this, but…

      One way to manipulate the date variable is:

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

  • John Jun 9, 2011 @ 4:39


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

  • Habeeb Oct 8, 2011 @ 23:40

    Thanks man you are awesome…….

  • Priyanka Nov 23, 2011 @ 8:25

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

  • Walt Jan 13, 2012 @ 13:47

    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 Apr 5, 2012 @ 15:19

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

  • Marc May 2, 2012 @ 13:08

    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 …

    • 🛡️ Vivek Gite (Author and Admin) nixCraft May 2, 2012 @ 13:18

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

  • Marc May 3, 2012 @ 5:11

    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

    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 Jan 1, 2014 @ 17:04

    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 Oct 16, 2015 @ 13:45

    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. Still have questions? Post it on our forum