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 Linux and UNIX like operating systems? How can I append a current date from a variable to a filename under Linux or Unix bash shell? How do I append date to filename?

We use the date command to show or set the system date and time. Further we can show the current date and time in the given FORMAT. This page explains how to append current date to a filename using various command line options.

ADVERTISEMENTS

Append current date to a filename

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

date +"%FORMAT_STRING"
date +"%m_%d_%Y"
date +"%Y-%m-%d"

You can store this to a variable name:

var=$(date +"%FORMAT_STRING")
now=$(date +"%m_%d_%Y")
printf "%s\n" $now
today=$(date +"%Y-%m-%d")
printf "Today we are going to backup file to NFS server at AWS '%s'\n" "/efs/my-blog-${today}.sql.tar.gz"

Sample outputs:

Today we are going to backup file to NFS server at AWS '/efs/my-blog-2020-02-29.sql.tar.gz'

Alternat syntax for command substitution is as follows:

var=`date +"%FORMAT_STRING"`
now=`date +"%m_%d_%Y"`
now=`date +"%Y-%m-%d"`
echo "${now}"

Sample outputs:

2020-02-29

Now we can append the current date stored in $now to a filename as follows:
echo "Coping data to /tmp/filename-${now} ..."

date command FORMAT_STRING

FORMAT_STRING (FORMAT) controls the output. Interpreted sequences are as follows (taken from GNU/date man page)

  • %a – locale’s abbreviated weekday name (e.g., Sun)
  • %A – locale’s full weekday name (e.g., Sunday)
  • %b – locale’s abbreviated month name (e.g., Jan)
  • %B – locale’s full month name (e.g., January)
  • %C – century; like %Y, except omit last two digits (e.g., 20)
  • %d – day of month (e.g., 01)
  • %D – date; same as %m/%d/%y
  • %F – full date; same as %Y-%m-%d
  • %g – last two digits of year of ISO week number (see %G)
  • %G – year of ISO week number (see %V); normally useful only with %V
  • %H – hour (00..23)
  • %I – hour (01..12)
  • %j – day of year (001..366)
  • %m – month (01..12)
  • %M – minute (00..59)
  • %n – a newline
  • %N – nanoseconds (000000000..999999999)
  • %p – locale’s equivalent of either AM or PM; blank if not known
  • %P – like %p, but lower case
  • %q – quarter of year (1..4)
  • %r – locale’s 12-hour clock time (e.g., 11:11:04 PM)
  • %R – 24-hour hour and minute; same as %H:%M
  • %s – seconds since 1970-01-01 00:00:00 UTC
  • %S – second (00..60)
  • %T – time; same as %H:%M:%S
  • %u – day of week (1..7); 1 is Monday
  • %U – week number of year, with Sunday as first day of week (00..53)
  • %V – ISO week number, with Monday as first day of week (01..53)
  • %w – day of week (0..6); 0 is Sunday
  • %W – week number of year, with Monday as first day of week (00..53)
  • %x – locale’s date representation (e.g., 12/31/99)
  • %X – locale’s time representation (e.g., 23:13:48)
  • %y – last two digits of year (00..99)
  • %Y – year
  • %z – +hhmm numeric time zone (e.g., -0400)
  • %:z – +hh:mm numeric time zone (e.g., -04:00)
  • %::z – +hh:mm:ss numeric time zone (e.g., -04:00:00)
  • %:::z – numeric time zone with : to necessary precision (e.g., -04, +05:30)
  • %Z – alphabetic time zone abbreviation (e.g., EDT)

Bash shell append date to filename in Linux or Unix

Finally, you can create a filename as follows:

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

Sample outputs:

Filename : /nas/backup_04_27_2010.sql

How do I add date to filename?

Here is a quick demo:
Linux Append Current Date To Filename in Bash Shell

Append current date to filename shell script

You can create a shell script as follows:

#!/bin/bash
#
# Backup mysql/mariadb database
 
## Get current date ##
_now=$(date +"%m_%d_%Y")
 
## Appending a current date from a $_now to a filename stored in $_file ##
_file="/nas/backup_$_now.sql"
 
## Do it ##
echo "Starting backup to $_file..."
mysqldump -u admin -p'myPasswordHere'  myDbNameHere > "$_file"
 
## Add more stuff below ##

Conclusion

You learned how to add the date to a filename under Linux or Unix-like operating systems. For further information type the following man command:
man date

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
17 comments… add one
  • tiwale Aug 21, 2012 @ 12:19

    great tip, it will be handy one day…

  • john Sep 20, 2012 @ 14:26

    nice!

  • David H Apr 6, 2013 @ 17:04

    Thanks. This was really handy reference.

  • JohnSilvanus Jun 6, 2013 @ 17:06

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

  • Marcin Sep 8, 2013 @ 10:15

    Yeah, finally found it. Thank you!

  • Duc Sep 20, 2013 @ 15:58

    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)
    file="/mnt/Daten/piBackup/Arch${current_date}.img"
    echo "Starting backup to $file..."
    dd bs=1M if=/dev/mmcblk0 |pv| dd of=$file
    

    You know what i’ve done wrong?

    • lap Sep 24, 2013 @ 7:36

      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 Dec 10, 2013 @ 9:56

    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:

    #!/bin/bash
    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 Dec 11, 2013 @ 2:58

    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 Nov 23, 2015 @ 13:00

      Brilliant, elegant, thanks so much

  • UnixDude Sep 14, 2014 @ 1:37

    Try this on for size, in bash:

    12673468035f96a3440f7a3_000005

    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*
    

    Outputs:

         -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 Sep 14, 2014 @ 1:42

      ERROR:

      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 @ 14:56

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

    Thanks

    • luofanbin Nov 30, 2015 @ 9:17

      man date

  • BW Mar 12, 2016 @ 20:22

    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 Mar 13, 2016 @ 0:45

      Try:

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

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

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.