Append date to filename in linux

Info/Summary

With bash scripting you can enclose commands in back ticks or parantheses. This works great for labling files, the following wil create a file name with the date appended to it.

Methods

Backticks -

$ echo myfilename-"`date +"%d-%m-%Y"`"

$(parantheses) - :

$ echo myfilename-$(date +"%d-%m-%Y")

Example Usage:

echo "Hello World" > "/tmp/hello-$(date +"%d-%m-%Y").txt"

(creates text file '/tmp/hello-28-09-2022.txt' with text inside of it)

Note, in Linux quotes are your friend, best practice to enclose the file name to prevent issues with spaces and such in variables.

Leave a Comment