I had a need to rename a file with the current date after running a backup script. This was done on Debian 8 Linux. The backup script would make a file name called “/mnt/backup.chadchenault.com/backup/server.cc.com.webmin/webmin.tar.gz“. I like my backup files with year-month-day at the front of the filename for easy sorting by date. My format is 2016-0422 for the date of April 04, 2016. I am using the date command to insert the current date and this is command that runs automatically after the backup command is run.
mv /mnt/backup.chadchenault.com/backup/server.cc.com.webmin/webmin.tar.gz /mnt/backup.chadchenault.com/backup/server.cc.com.webmin/$(date “+%Y-%m%d.webmin.tar.gz”)
This results in a new filename of “/mnt/backup.chadchenault.com/backup/server.cc.com.webmin/2016-0422.webmin.tar.gz”
Here are an example of testing the date command from the command line.
Create a file “delete.txt”
touch delete.txt
Copy the file to a new name with current Year-MonthDay.delete.txt format
cp -v delete.txt /root/$(date “+%Y-%m%d.delete.txt”)
The output of the command on 2016-0422 was:
‘delete.txt’ -> ‘/root/2016-0422.delete.txt’
The command was run successfully.