I'm trying to make a script to back up my we servers, but I have very limited knowledge with bash scripting. I want the script to name the backup file ht-"today's date". How would I go about doing that? Is it possible to use a command, like the date command, as a variable?

1 Answer 1


To backup directory dir into a file named after today's date and time:

name=$(date '+ht-%F-%H-%M-%S.tgz')
tar -czf "$name" dir

You may eventually want to use a program such as rsnapshot, dirvish, or one of their competitors. These programs automate the process and also, via the use of hardlinks, save on disk space if you are keeping multiple backups.

  • 1
    Thank you very much for this. I have a data logger that runs all the time creating the daily file called today.dat and have been having to be up at midnight every night to "mv today.dat Archive/2017-05-22.dat" before the master system scoops up all the archives after midnight.
    – SDsolar
    May 27, 2017 at 4:27
  • @SDsolar So, I presume you use: mv today.dat "$(date '+Archive/%F.dat')"
    – John1024
    May 27, 2017 at 5:46
  • Actually, I am using mv today.dat Archive/$(date +%F).dat now.
    – SDsolar
    Oct 4, 2017 at 20:20
  • 1
    @SDsolar Small changes can make a big difference. Instead of date +F%, try date +%F
    – John1024
    Oct 5, 2017 at 1:08
  • 1
    Yikes. Gadzooks. Yes, of course that works. Thank you! It does help to actually understand why I am typing it so I won't make a copying error like that.
    – SDsolar
    Oct 5, 2017 at 5:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .