UNIX tar/zip

UCLA Mathematics Consulting Group



 
UNIX tar/zip

For those who have built up a "considerable" number of files (or size of files) on your user account, it is probably in your best interest to perform some type of compression on your files.

Two ways to do that in UNIX are to "tar" and to "zip" the files.

tar means "tape archive" (even though  you are really not "archiving tapes", but that's its roots), and the different forms of zip (gzip, pkzip, etc.) "zip" or "package and compress" the files (sorry, no acronym translation). tarring is usually the *first* step performed on a file or directory, and aftewards, you "zip" the file. tarring creates an additional file in the directory, and zipping *replaces* the file with its zipped version.

untarring and unzipping perform the "reverse" functions.  When you "unzip" a tarred/zipped directory, it *replaces* the tarred/zipped directory with just the "tarred" version of the original file).  When you "untar" that file, it will *overwrite* any existing directories and files in the directory of the same name/path, and keep a copy of the "tarred" version in the same path.

Although you can technically zip more than one file, generally you tar a group of files into one file, and then zip *that* file into a zipped version.

A) tar:

tarring a file:

To "tar" a file, you need to come up with a name of the "tarred" file; for example, if you are tarring a file called "thisfile", you need to specify a "tarred" name suchas "thisfile.tar":

tar -cf thisfile.tar thisfile
After you perfom this operation, you will see a new file in your directory (in addition to thisfile ) called thisfile.tar.  You will notice that the size of the "tarred" file is smaller than the original file.

tarring a directory:

To "tar" a directory, you perform the *same* function as if on a file. You will *also* need to come up with a name of the "tarred" directory; for example, if you are tarring a file called "thisdir", you need to specify a "tarred" name such as "thisdir.tar":

tar -cf thisdir.tar thisdir
viewing contents of a "tarred"  directory:

Once you've tarred the directory, you can view the contents of the "tarred" directory without altering its contents.  You do this by typing:

tar -tf thisdir.tar
B) zip:
zipping a file (using gzip):

To "zip" a file, you *don't* need to come up with a name of the "zipped" file. Again, you will probably be zipping a previously tarred directory, such as  "thisdir.tar":

gzip thisdir.tar
This action will *replace* the file thisdir.tar with thisdir.tar.gz, which will be in the *same* location as the previous file thisdir.tar. Again, this file (thisdir.tar.gz) will be smaller than the file thisdir.tar.


C) unzip, untar:

I have chosen to lump the two "reverse" processes, unzipping and untarring, into one discussion, because if you both tar *and* zip a file, you will need to perform both of these operations to restore the original files.

unzipping a file (using gunzip):

To "unzip" a file, you simply type (assuming the filename is  "thisdir.tar.gz":

gunzip thisdir.tar.gz
In this case, this will *replace* the zipped file thisdir.tar.gz with the file thisdir.tar (exactly the reverse of the forward process).

untarring a file (using tar):

To "untar" a file, you simply type (assuming the filename is  "thisdir.tar"):

tar -xvfp thisdir.tar
This will produce a directory thisdir in the *same* location as the previously tarred file. The file thisdir.tar will *still* exist in the same location as the new directory/files thisdir.

*** Please Note*** ... if there is "already" a directory  thisdir in the same location, it will **OVERWRITE** that existing directory plus its contents with the *new* directory/files  thisdir, from the "untarring" process.  Therefore, be careful when you untar into a directory.


D) gtar (tar + zip):

gtar is a special function that allows you to tar and zip in one step.  Because you are "tarring", you need to come up with a name of a file in which to tar/zip. Generally, we add the suffix  .tgz   at the end of the directory/file with the same name. There are applications on our system which seek out files with that suffix, in order to back them up (archive them), so it is in your best interests to do the same.

 Since you are already familiar with the tarring/zipping process, we can simply list the three processes: tar/zip, view contents, and unzip/untar.  We first take a directory (thisdir), and tar/zip it into a file called filename.tgz. We can easily view the contents (similar to before), and when we unzip/untar the file, it produces the *original* directory/files, and the "tarzipped file filename.tgz remains in that same location as well.

tar/zip

gtar -czf filename.tgz thisdir
view contents
gtar -tzf filename.tgz
unzip/untar
gtar -xzf filename.tgz

 
 
ra:3/08/99