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

by on February 5, 2006 · 23 comments· LAST UPDATED September 13, 2011

in , ,

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"
TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 23 comments… read them below or add one }

1 andreyev June 14, 2006 at 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.


2 Bernd September 12, 2011 at 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.


3 nixCraft September 13, 2011 at 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!


4 Kate Ward January 18, 2007 at 8:15 pm

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


5 Aaron October 8, 2007 at 6:44 pm

Awesome stuff, you have save me tons of time


6 Itpatil May 13, 2008 at 5:19 pm

Thank you. Very helpful.


7 Phil May 16, 2008 at 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)?


8 Poli July 26, 2011 at 9:03 am

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


9 nixCraft May 16, 2008 at 1:16 pm


Try yest


10 Spenser January 2, 2010 at 4:28 pm

Exactly what I needed. Thanks!


11 richa March 11, 2010 at 6:09 am

very nice and helpful…thanks


12 prasanna March 2, 2011 at 5:46 am

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


13 mule.ear October 19, 2011 at 6:17 pm

Very late to answer this, but…

One way to manipulate the date variable is:

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


14 John June 9, 2011 at 4:39 am


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


15 Habeeb October 8, 2011 at 11:40 pm

Thanks man you are awesome…….


16 Priyanka November 23, 2011 at 8:25 am

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


17 Walt January 13, 2012 at 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)


18 Memscan April 5, 2012 at 3:19 pm

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


19 Marc May 2, 2012 at 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 …


20 nixCraft May 2, 2012 at 1:18 pm

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


21 Marc May 3, 2012 at 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


22 Marc May 3, 2012 at 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 ;)


23 Anoor January 1, 2014 at 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?


Leave a Comment

Tagged as: , , , , , , , , , , , , , , , , , , , , ,

Previous post:

Next post: