Append Current Date To Filename in Bash Shell

Originally published at: https://www.cyberciti.biz/faq/unix-linux-appleosx-bsd-shell-appending-date-to-filename/

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?

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

1 Like

@Nordri

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

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

But - would work:

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

HTH


Linux sysadmin blog - Linux/Unix Howtos and Tutorials - Linux bash shell scripting wiki