In this lesson, we are going
to practice using the tar, gzip, and bzip2 utilities. These archiving
utilities are commonly used in Linux and UNIX systems.
TAR
tar stands for Tape ARchive.
It was originally designed for tape backups, but it is used to create
a tar file anywhere on the filesystem. tar creates one "tar file"
(also known as a "tarball") out of several files and directories.
A tar file isn't compressed, it's just a heap of files assembled together
in "one container". So, the tar file will take up the same
amount of space as all the individual files combined, plus a little
extra. A tar file can be compressed by using gzip or bzip2.
Here are some examples:
- tar -xvf example.tar
Extract the contents of example.tar and display the files as they
are extracted.
- tar -cf backup.tar
/home/ftp/pub Create a tar file named backup.tar from the contents
of the directory /home/ftp/pub
- tar -tvf example.tar
list contents of example.tar to the screen
Now, let's practice.
- Log in to the linux box
and make sure you are in the home directory.
- Do an ls -al to see what
is in the home directory. We should have a directory called "Desktop".
cd into that directory and look around. Type "cd" to bring
yourself back to the home directory. Again, confirm that you are in
the home directory.
- Type in the following
command: "tar -cvf desktop.tar Desktop" (please pay attention
to CaSe)
- Do an ls -al again to
see what is in the directory now. You should see a file called desktop.tar
- Now type in "mv Desktop
Desktop.old" to rename the Desktop directory. Do an ls -l to
confirm that the directory name has been changed and there no longer
is a directory named Desktop in the home directory.
- cd into the Desktop.old
directory and confirm that the files are the same as what you saw
in #2.
- cd back to your home directory
(to move up (back) one directory, you can type in "cd .."
- Now type in the command
"tar -xvf desktop.tar" to extract the contents of the archive.
- Do an ls -al again. You
should see the original Desktop directory. cd into it and make sure
the files are in it.
- Remove the tar file and
the Desktop.old directories if everything worked properly.
("rm desktop.tar", "rm -rf Desktop.old")
GZIP
gzip is the original UNIX
ZIP format. It's common to first "tar" files and then compress
them afterwards using gzip. These files are normally given the extentions
.tar.gz to show that they are tar archives zipped up with gzip. You
may also see the extension, .tgz. An archive that is compressed with
gzip is compatible with WinZip and PkZip. So, a file zipped up on a
UNIX box can be unzipped with a Windows box.
Here are some examples:
To compress a file using
gzip, execute the following command: gzip filename.tar
(where filename.tar is the name of the file you wish to compress) The
result of this operation is a file called filename.tar.gz. By default,
gzip will delete the filename.tar file.
To decompress a file using
gzip, execute the following command: gzip -d filename.tar.gz
The result of this operation is a file called filename.tar. By default,
gzip will delete the filename.tar.gz file.
You can also decompress the
file using the command: gunzip filename.tar.gz
This is the same as using the gzip -d command.
There are many options that
you can use with gzip. Do a man on the utility to learn more.
BZIP2
bzip2 and bunzip2 are file
compression and decompression utilities. The bzip2 and bunzip2 utilities
are newer than gzip and gunzip and are rapidly gaining popularity. The
bzip2 utility is capable of greater compression ratios than gzip. Therefore,
a bzip2 file can be 10-20% smaller than a gzip version of the same file.
Usually, files that have been compressed by bzip2 will have a .bz2 extension.
The utilities work very similar to gzip and gunzip. Here are a couple
of examples.
To compress a file using bzip2, execute the following command: bzip2
filename.tar
(where filename.tar is the name of the file you wish to compress) The
result of this operation is a file called filename.tar.bz2. By default,
bzip2 will delete the filename.tar file.
To decompress a file using
gzip, execute the following command: bunzip2 -d filename.tar.bz2
The result of this operation is a file called filename.tar. By default,
bunzip2 will delete the filename.tar.bz2 file.
|
Assignment:
Please email me the answer to the following questions:
- Make a file using vi named like your firstnameLastname.txt In that file, please write out the steps to zip up a directory in UNIX.
- Now, tar up the file you
just created and zip it up using bzip2.
- From your windows machine,
ftp into your account and get the file you just made.
NEXT LESSON
|
| |