4

I'd like to make a .tar file of a particular directory every day of the week. The way I'd like to do it is with a bash script making a tar file every day with crontab.

I was wondering, is there a way to regulate the name of the tar file the the bash script makes? Right now it's the same every time the file is made, becase the bash has just one command:tar -cvf file.tar /home/name/folder/

But I'd like for it to be slightly differnt based on the day e.g. FileTus.tar , FileWed.tar, FileFri.tar,etc..

Is there a way to do this though terminal commands?

Thanks

2 Answers 2

8

The date command can produce a date as text in whatever format you want. You would then use it in a script something like:

tar -cvf File$(date +%a).tar /home/name/folder

This would produce FileMon.tar, FileTue.tar as above. See man date for a description of the different date formats it can produce.

2
  • Exactly how I would have done it. Nice answer. Btw: The usual way to include a date in an archive is filename$(date +%y%m%d). That assures that you can sort them by date in a standard file browser such as nautilus. Optionally you can include the time $(date +%y%m%d-%H%M%S) (or even nanoseconds S->N) depending on how unique it should be one can also include the process id to prevent collisions.
    – con-f-use
    Aug 14, 2012 at 14:37
  • 1
    I find it quite useful to put the date at the beginning of the filename, in YYYY-MM-DD format. That way, directory listings are always sorted correctly. For example, 2012-08-15.filename.tar, generated by: tar -cvf "$(date +%F).filename.tar" /path/to/target. Aug 14, 2012 at 16:27
5

Instead of just names of days, why not do a full date format? That way the same filename ain't generated every week :)

Something like this -

nits@excalibur:~$ tar -cvf /home/nits/Desktop/File$(date +%d%B%Y).tar Music/

would produce output like this -

nits@excalibur:~$ ls ~/Desktop/
File14August2012.tar

For something along with a timestamp (as asked in the comments):

nits@excalibur:~$ tar -cvf "/home/nits/Desktop/File$(date +%d%B%Y_%H:%M).tar" Music/
nits@excalibur:~$ ls ~/Desktop/
File14August2012_20:14.tar
4
  • thanks very much for that. Btw is there a way to add time to that too?
    – agmb
    Aug 14, 2012 at 14:40
  • @agmb Yup, take a look at man date . Add %H:%M . Edited answer Aug 14, 2012 at 14:43
  • Thanks, but now I get an error everytime I run the bash tar: Cannot connect to File14August2012_16: resolve failed
    – agmb
    Aug 14, 2012 at 15:05
  • @agmb The colon is causing this. Use double-quotes around the path.
    – Peter Smit
    Aug 14, 2012 at 18:17

You must log in to answer this question.

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