I want to tar a directory at a regular interval (say every morning at 4am) so I was planning on using crontab. How can I have the date be in the filename ie srcdirectorybackup5-30-09.tar so that I can differentiate between all of the tar files.

5 Answers 5


You want dates in ISO format - YYYY-MM-DD. They sort properly that way.

tar -cf srcdirectorybackup`date +%F`.tar /path/to/src

Assumes tar and date are in the path. You can add this line to a crontab.

Cripes! ericslaw is absolutely right. Those %'s need to be escaped if they're used in the crontab file or they'll be interpreted as newlines!

So, if you're going to put this line right into the crontab, escape the "%" with a "\".

tar -cf srcdirectorybackup`date +\%F`.tar /path/to/src

The rep really ought to go to ericslaw...

  • %U can also be useful. It's the week number and can be useful if you want to put your tar files in per week directories.
    – radius
    Jun 30, 2009 at 1:04


several folks have suggested the wonderful date +%Y%m%d_%H%M%S style solution but nobody has mentioned the major caveat of '%' in crontabs...

'%' it is equivalent to '\n' so your cronjob will likely fire and fail mystereously!

You'll more likely want to simply escape it with backslash like this (and I also like to get some kind of inventory or other output to check that it ran).

0 4 * * * tar vcf /path/to/dsttarfile.tar.`date +\%Y\%m\%d_\%H\%M\%S` /path/to/srcdir > /path/to/logfile.log 2>&1

You might consider using `date +%w' as part of your tarfile, so you have a tar file for each of the last 7 days and dont have to worry about purging old copies.

  • 1
    I struggled with this for a full day before I discovered the fix. solid advice +1
    – egorgry
    Jun 30, 2009 at 3:06
  • This should be selected as the correct answer. I have been trying to figure this out for a while, damn you cron and your mysterious errors!
    – Dallen
    Jan 3, 2017 at 23:22

In many Linux distribution this are the steps you have to follow:

  1. cronttab -e
  2. On the text editor enter: 0 4 * * * /home/scripttaringsrc.sh
  3. exit the editor saving the files with ":wq"

The create the file /home/scripttaringsrc.sh

tar -cf srcdir 'date +%Y-%m-%d_%Hh%Mm'.tar /path/to/dst

Don't forget to chmod the scripttaringsrc.sh to execute permissions.


assuming you can handle the crontab stuff, for bash, the command would be as follows:

bash -c "tar -cf `date +%F`test.tar foo bar"

Of course, foo and bar are the files, test.tar is the rest of the suffix you want for the file, and you put in any option you need :)

This works in both cygwin and linux. I'm not familiar enough with cron to know if telling it which shell to use like that is necessary.


I have this little script that pack my entire etc dir and saves it in a directory with the correct date.

Save this script and put it in /etc/cron.daily/ (if that is available in your dist), since the scripts that is in that dir executes "ones per day" most often sometimes around 4. However a quick look in /etc/crontab will show the exact details.


date=`date +%Y%m%d-%H%M`
#echo Tid: $date


cd $bpath/
mkdir sys$date
cd sys$date

> $datumfil

tar -cvzf etc.tar.gz /etc/ > etc.tar.gz.list

md5sum * >  md5sum
ls -lh   >> md5sum

Note: you need to change this a little so you get the "correct" filename you wanted.


You must log in to answer this question.

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