Wednesday, June 03, 2009

How to create a tar archive file?

Creating tar files is essential for certain processes. It's an easy way to package up files and directories for moving on the file system or storing on tape. The simplest form of a tar file contains just files and lives as a file itself. Here is an example of that:

Say you have a directory full of html files, a web site perhaps with no directories. This is uncommon but will show a very simple tar file.

First cd into the directory: cd /my/web/root
/my/web/root> 

If you list the contents of the directory using ls, you might see something like this:

index.html one.html two.html bio.html about.html

Now let's create an archive of these files.

/my/web/root> tar -cvf mywebroot.tar *.html

This will create a file called mywebroot.tar that contains all the html files in the current directory. 

To view the contents of the file type:

/my/web/root> tar -tvf mywebroot.tar 

You will see a list like this:
-rw-r--r-- jsmith/www 2057134 2009-04-16 18:11:33 index.html
-rw-r--r-- jsmith/www       1947 2009-04-16 18:11:33 one.html
-rw-r--r-- jsmith/www         134 2009-04-16 18:11:33 two.html
-rw-r--r-- jsmith/www           57 2009-04-16 18:11:33 bio.html
-rw-r--r-- jsmith/www         713 2009-04-16 18:11:33 about.html

Tar stores information about the files as well as the files. You get the permission of the file the date stamp the owner and group as well as the size.