This laboratory involves the use of common file and directory archiving and compression utilities available on UNIX-like systems.
Setup
Log into the FreeBSD machine for the class.
First, run:
% ~bmcdonald/class/lab09.sh
Next, run:
% cd ~/lab09
In order to find out what's in the laboratory directory you just created, run:
% ls -R
The -R option generates a recursive listing; if there are subdirectories within the location specified for the ls command, those subdirectories will also be listed, and if those subdirectories contain subdirectories, this process continues for the full depth of the file system hierarchy specified.
You can combine flags in the ls command to obtain more information.  Do this now as follows:
% ls -lR
That's ell ess space dash ell are.  That should give you extra information on the files and directories, including file size, and permissions.
Creating an Archive
Now, create an archive of file system hierarchy for which the directory tarDir is the root, by running:
% tar -cvf archive.tar tarDir
Meaning of Arguments
The arguments for this tar command include:
c – create an archive
v – be verbose in providing a lot of information about the tar operation being run
f – create the archive in the file specified by the argument immediately following this flag (the convention is to end the names of archives created by tar with .tar)
Special Note on Argument Syntax
Notice that we could have ran:
% tar -c -v -f archive.tar tarDir
The tar command accepts an alternate form of argument flags, in that all flags that do not have a parameter associated with them can be combined after a single dash/hyphen.  In this case, the f flag is at the end of the combined argument list because it requires a single parameter.  This is a useful thing to know about some UNIX command line utilities.
The Last Argument
The last argument is the names of the directory to archive.  You can list more than one file or directory to include in the archive being created just by adding their names to the end of the command.
Results
Your current directory (~/lab09) will now contain a file named archive.tar.
Listing an Archive
To find out what's in the archive you created, run the following:
% tar -tvf archive.tar
You should get a listing similar to the following:
drwxr-xr-x bmcdonald/bmcdonald 0 Mar 28 23:20 2006 tarDir/
drwxr-xr-x bmcdonald/bmcdonald 0 Mar 28 23:20 2006 tarDir/tarSubdir/
-rw-r--r-- bmcdonald/bmcdonald 193 Mar 28 23:20 2006 tarDir/tarSubdir/one
-rw-r--r-- bmcdonald/bmcdonald 232 Mar 28 23:20 2006 tarDir/tarSubdir/two
-rw-r--r-- bmcdonald/bmcdonald 208 Mar 28 23:20 2006 tarDir/tarSubdir/three
-rw-r--r-- bmcdonald/bmcdonald  46 Mar 28 23:20 2006 tarDir/mixedText
-rw-r--r-- bmcdonald/bmcdonald 106 Mar 28 23:20 2006 tarDir/onechar
This listing is similar to what you got when you ran ls -lR earlier.
Look at the size of the archive by:
% ls -l archive.tar
Notice that the archive file is larger than the sum of the sizes of the files and directories it contains.  The tar command does not compress by default.
Extracting an Archive
Extract the archive you made into a different directory as follows:
% mkdir newDir
% cd newDir
% tar xvf ../archive.tar
You'll see the names of the files appear as they are extracted.  Notice another variant of the argument syntax of the tar command.
Verifying an Archive
Run:
% ls -R
You'll see the same hierarchy that you archived.  Prove this by using the recursive option of the diff command as follows to compare the hierarchy created by extracting the archive with the hierarchy used to create the archive:
% cd ..
% diff -R tarDir newDir/tarDir
%
You won't see any output from of the diff command, indicating that the two hierarchies are identical.
Another way to prove this is by comparing the archive contents against a hierarchy created by extracting from the archive.  Run:
% cd newDir
% tar -dvf ../archive.tar
Let's change a file in the extracted hierarchy to see what happens when a file is changed.  Run:
% vi tarDir/mixedText
Add or delete some text, then save your work by:
:wq
Now, run the comparison:
% tar -dvf ../archive.tar
You'll see something like:
tarDir/
tarDir/tarSubdir/
tarDir/tarSubdir/one
tarDir/tarSubdir/two
tarDir/tarSubdir/three
tarDir/mixedText
tarDir/mixedText: Mod time differs
tarDir/mixedText: Size differs
tarDir/onechar
This can be useful if you extract an archive, make some changes, and later wonder which files you have changed.
Command Argument Placement
The first argument to tar must specify the operation it will be performing (one of the following):
•	c – create
•	d – difference
•	r - append
•	t – table of contents
•	u - update
•	x – extract
Security Issues
There can be some security issues in tar, mainly due to bugs.  Notice all the paths within a tar archive are relative, not absolute.  If you experimented, you'd notice that tar would fail if you gave it a path with double dots in it.  Appropriate usage of tar is to create and extract archives made up of files at or below the hierarchy specified by the last argument(s).  If tar accepted the double dot or absolute paths, this directive would be violated; attackers could cause you to overwrite key system files if you extracted such an archive while running with sufficient privilege.
Other Archiving Utilities
Two other archiving utilities, pax and cpio, are common in UNIX-like systems.
Compression Utilities
Common compression utilities include gzip, bzip2, and compress.  We'll use gzip in this lab.
There is built-in support for gzip in tar; the z argument specifies to use gzip to compress an archive when creating it.
Run:
% cd ~/lab09
% ls -l archive
This gets you into the directory containing the archive, and shows you how large the archive is.  
Compressing an Archive
Make a copy of the archive by:
% cp archive.tar archive2.tar
You have two identical copies now.
Compress the archive copy by:
% gzip archive2.tar
This creates the compressed archive, renaming it to archive2.tar.gz.
If you had used tar to create a compressed archive with the z argument, the name would likely be archive.tgz; this is a typical convention for gzipped tar archives.
Notice the size of the compressed archive.  Run:
% ls -l archive2.tar.gz
It's much smaller than the original archive.  It removed redundant information.
Uncompressing an Archive
Fortunately, gzip is a lossless compression algorithm, or we could never obtain the original archive by decompression.  With lossy compression algorithms like MPEG-2, we can afford that because the consumer of the compressed medium is not expected to be interested in uncompressing it to its exact original state (and also there are vested interests in preventing consumers from uncompressing copyrighted material to its original state).  However, if we are archiving our valuable data, we need to be able to restore it fully from a compressed state, exactly as it was when we archived and compressed it.
Run:
% gunzip archive2.tar.gz
Then:
% ls -l
% diff archive*.tar
%
You should see no difference between the two files.