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
You should follow me on twitter here or grab rss feed to keep track of new changes.
Featured Articles:
- 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
- Top 30 Nmap Command Examples For Sys/Network Admins
- 25 PHP Security Best Practices For Sys Admins
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins
- Top 20 OpenSSH Server Best Security Practices
- Top 20 Nginx WebServer Best Security Practices
- 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
- 15 Greatest Open Source Terminal Applications Of 2012

- My 10 UNIX Command Line Mistakes
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- The Novice Guide To Buying A Linux Laptop














{ 22 comments… read them below or add one }
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.
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.
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!
Take a look at log4sh [http://log4sh.sourceforge.net/] as well. It might be a better choice for your logging needs.
Awesome stuff, you have save me tons of time
Thank you. Very helpful.
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)?
What about to start apps via script and set today’s date to log at the time of app start?
Phil,
Try yest
Exactly what I needed. Thanks!
very nice and helpful…thanks
how to create log/files using yesterday,s date….
Very late to answer this, but…
One way to manipulate the date variable is:
DAY=$(date +”%d”)
DAY=$((DAY-1))
echo $DAY
Thanks
This helped me heaps with what i was trying to do.
Thanks man you are awesome…….
Great code..Helped a lot !! Thanx Vivek!!!
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)
I know it is a old post but thanks for the info anyway. Very helpful.
My littel script now looks like this :
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 …
Is this a complete script? Also, are you using a language other than English?
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
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 ;)