Append Current Date To Filename in Bash Shell

by on April 26, 2010 · 9 comments· LAST UPDATED April 26, 2011


How do I append current date (mm_dd_yyyy format) to a filename (e.g., backup_mm_dd_yyyy.sql) under UNIX like operating systems?

To get the current date in mm_dd_yyyy format use the following date format syntax:

date +"%m_%d_%Y"

You can store this to a variable name:

now=$(date +"%m_%d_%Y")


now=`date +"%m_%d_%Y"`

Finally, you can create a filename as follows:

now=$(date +"%m_%d_%Y")
echo "Filename : /nas/backup_$now.sql"

Sample outputs:

Filename : /nas/backup_04_27_2010.sql

You can create a shell script as follows:

_now=$(date +"%m_%d_%Y")
echo "Starting backup to $_file..."
# mysqldump -u admin -p'myPasswordHere' myDbNameHere > "$_file"
TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 9 comments… read them below or add one }

1 tiwale August 21, 2012 at 12:19 pm

great tip, it will be handy one day…


2 john September 20, 2012 at 2:26 pm



3 David H April 6, 2013 at 5:04 pm

Thanks. This was really handy reference.


4 JohnSilvanus June 6, 2013 at 5:06 pm

Thank you, just used this to save a version of my thesis every time I compile the .tex into a .pdf.


5 Marcin September 8, 2013 at 10:15 am

Yeah, finally found it. Thank you!


6 Duc September 20, 2013 at 3:58 pm

This is not working for me.
It always says “date: zusätzlicher Operand „%Y-%m-%d“”, which means “date: extra operand „%Y-%m-%d“” or something like that.

here is my script:
#! /bin/bash
current_date=$(date + “%Y-%m-%d)
echo “Starting backup to $file…”
dd bs=1M if=/dev/mmcblk0 |pv| dd of=$file

You know what i’ve done wrong?


7 lap September 24, 2013 at 7:36 am

There should not be a whitespace between + and the format string on line 3. Change $(date + “%Y-%m-%d”) to $(date +”%Y-%m-%d”).


8 Martin December 10, 2013 at 9:56 am

Great blog post! Thank you very much!

I wrote the following script to save the screenshots with date and time in the filename similar as in Mac OS X:

echo "Taking screenshot via adb from connected Android device..."
dateAndTime=$(date +"%Y-%m-%d at %H.%M.%S")
filename="ADB Screen $dateAndTime.png"
adb shell screencap -p | perl -pe 's/\x0D\x0A/\x0A/g' > "$filename"
echo "Successfully saved screenshot to $filename"
exit 0


9 Freddythunder December 11, 2013 at 2:58 am

You can also avoid the bash shell altogether and put the one line in your crontab like:

mysqldump --user=[user] --password=[pass] Database>Database_`date +"%m%d%y"`.sql


Leave a Comment

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

Previous Faq:

Next Faq: