≡ Menu

Append Current Date To Filename in Bash Shell

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"
Share this tutorial on:

Like this? Follow us on Twitter OR support us by using Patreon

{ 17 comments… add one }
  • tiwale August 21, 2012, 12:19 pm

    great tip, it will be handy one day…

  • john September 20, 2012, 2:26 pm


  • David H April 6, 2013, 5:04 pm

    Thanks. This was really handy reference.

  • JohnSilvanus June 6, 2013, 5:06 pm

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

  • Marcin September 8, 2013, 10:15 am

    Yeah, finally found it. Thank you!

  • Duc September 20, 2013, 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?

    • lap September 24, 2013, 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”).

  • Martin December 10, 2013, 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
  • Freddythunder December 11, 2013, 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
    • JuanK November 23, 2015, 1:00 pm

      Brilliant, elegant, thanks so much

  • UnixDude September 14, 2014, 1:37 am

    Try this on for size, in bash:
    bkup ()
    ## shell function, placed in your profile
    STAMP=`date +%m%d%y-%H%M%S`
    cp -i ${1}{,.${BKUP_DATE_STAMP}}

    Place the above function in your .profile or .bash_profile or .bashrc. Wherever you like or paste it into a command line on the terminal. Then use it like this:

    touch name.conf
    bkup name.conf

    To check and see if it worked,

    ls -l name.conf*

    The advantage to this approach is that if you are backing up a file in a subdirectory:

    bkup /path/to/the/file/name.conf
    ls -l /path/to/the/file/name.conf*

    -rw-r–r– 1 unixDude unixDude 0 Sep 13 21:21 /path/to/the/file/name.conf
    -rw-r–r– 1 unixDude unixDude 0 Sep 13 21:23 /path/to/the/file/name.conf.091314-211838

    Now, the challenge is to backup the file and then vi it or what you will, within the same function.


    • UnixDude September 14, 2014, 1:42 am


      STAMP=`date +%m%d%y-%H%M%S`

      should be:

      BKUP_DATE_STAMP=`date +%m%d%y-%H%M%S`

      Or, what you want as long as it matches.

  • kudinsuparjo May 10, 2015, 2:56 pm

    I am very newbie about bash.
    Why there’s a plus sign in front of the pattern?
    date +”%m_%d_%Y”


    • luofanbin November 30, 2015, 9:17 am

      man date

  • BW March 12, 2016, 8:22 pm

    How do you put a string and the date in one line, e.g.
    MyDate=’Mystring’ date +”%Y-%m-%d_%H.%m”
    echo $MyDate
    I know it’s not working, but how do you do that?

    • Vivek Gite March 13, 2016, 12:45 am


      MyDate="Mystring $(date +'%Y-%m-%d_%H.%m')"
      echo "$MyDate"
  • Jose Ortiz December 15, 2016, 2:11 pm

    Awesome. Your final example was almost exactly what I needed.

Security: Are you a robot or human?

Leave a Comment

You can use these HTML tags and attributes: <strong> <em> <pre> <code> <a href="" title="">

   Tagged with: , , , , , , , , , , , ,