Append Current Date To Filename in Bash Shell

Originally published at:

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?

I just wonder why do you use underscore to name your variables.

1 Like


No reasons. You can use - and any other valid characters. For example, the following in not a valid filename:

now=$(date +"%m/%d/%Y")
## the following is going to fail ##
touch "${file}"
ls -l /tmp/backup*

But - would work:

now=$(date +"%Y-%m-%d") 
touch "${file}"
ls -l /tmp/backup*