Category Archives: date

Backup your code using a shell script

There are a number of version control system such as CVS and svn that help you do a lot of stuff one of which is to have a backup of your code so that you can revert back just in case you screw up. But it’s not really worth it if you’re working alone on that code and its not too many a files. For example you’re doing your coding assignment that won’t last for more than 15 days. And yes, as per the lab instructions you have to do the coding all by yourself. In such cases, its good to have a script handy that can create a backup of your code and name it appropriately (say today’s date) and store it in a backup directory.

I created such a script for my day to day assignments and am sharing with you all.



FILENAME=`date +%y_%m_%d.tar`;

tar -cvvf $FILENAME code/;
mv $FILENAME backup/;


This script creates a tar archive of the code directory, names it today’s date and stores it in the backup directory.

A sample run of the code gives the following output and a tar archive by the name 09_10_02.tar is added to the backup directory.


amit@texens:~/btp/src$ sh
drwxr-xr-x amit/amit 0 2009-10-02 01:39 code/
-rwx—— amit/amit 10077 2009-10-02 01:39 code/main.c
-rw-r–r– amit/amit 93 2009-10-02 00:04 code/Makefile
-rwxr-xr-x amit/amit 18344 2009-10-02 01:38 code/main

amit@texens:~/btp/src$ ls backup


NOTE: one may change the date format to reflect the current time in addition to the date if several backups are made in a single day.