If you've been using a Mac, you have most likely seen files compressed using StuffIt, Aladdin Systems' popular file-compression utility.
In the Unix world there are a handful of commands, already part of the operating system, that are used for archiving and compressing files. It's likely that command-line software you download from the Internet will be compressed using these tools.
gzip is a program for compressing files. The g in gzip is from GNU (see the sidebar "What Is a GNU?"), and the zip is a reference to an earlier program called zip . gzip is not the same as zip , though. The older zip program not only compresses files, but it also combines multiple files and/or directories into a single file. The gzip program is more widely used than zip in the Unix world because gzip provides better compression (smaller files), and the Unix tar command is the commonly used program to combine multiple files into a single file. (The version of tar on Mac OS X can also compress its output with gzip . See tar below.)
To compress a file using gzip:
localhost:~/Desktop vanilla$ ls -l *.tiff -rw-r--r-- 1 vanilla wheel 186590 Apr 14 20:49 Picture 1.tiff -rw-r--r-- 1 vanilla wheel 569553 Apr 15 10:41 Picture 2.tiff -rw-r--r-- 1 vanilla vanilla 517795 Apr 15 10:51 Picture 3.tiff localhost:~/Desktop vanilla$ gzip *.tiff localhost:~/Desktop vanilla$ ls -l *.tiff.gz -rw-r--r-- 1 vanilla vanilla 93648 Apr 14 20:49 Picture 1.tiff.gz -rw-r--r-- 1 vanilla vanilla 170010 Apr 15 10:41 Picture 2.tiff.gz -rw-r--r-- 1 vanilla vanilla 119743 Apr 15 10:51 Picture 3.tiff.gz localhost:~/Desktop vanilla$
Tips
You can use gzip to compress the output of other commands. Just pipe the output of the other commands into gzip and redirect the output to a file. (See Chapter 2 to review pipelines.) For example:
grep root /var/log/mail.log gzip > output.gz
That command line runs the grep command to search for the string root in the file /var/log/mail.log . The output of grep (the lines containing root ) is piped into the gzip program, and the output of gzip is redirected into the file output.gz .
If you use gzip to compress a Mac file that has a resource fork, the file loses both its icon and its association with the application that created it, and thus it may become useless. To safely compress traditional Mac files, use StuffIt.
Of course, once you have a compressed file, you'll want to know how to uncompress it.
To uncompress a gzipped file:
Tips
You can use gunzip in a pipeline. If you want the output of gunzip to go into a pipe, use the -c option:
gunzip -c filename.gz command
When gunzip is used this way, it does not replace the compressed file.
You can use the -t option to prevent removal of the compressed file.
What Is a GNU?GNU is a recursive acronym (an acronym that refers to itself) that stands for Gnu's Not Unix . The GNU project is the domain of the Free Software Foundation, which coordinates a huge volunteer effort to create a complete Unix-like operating system that has the following four freedoms:
There are hundreds of GNU software packages. There is even a GNU-Darwin project: the GNU-Darwin Distribution (http://gnu-darwin. sourceforge .net). There's more information on the GNU project and the Free Software Foundation at GNU's Not Unix! (www.gnu.org). |
In many cases you will want to compress an entire directory (folder). Unlike the StuffIt program, gzip does not compress directories. To deal with this, you use another program from the command line, called tar , which creates a single file from a directory and all its contents (a tar file ). tar was originally used only for making backups to tape systems (hence its name, for tape archive ), but is now used far more frequently to combine entire folders into a single tar file prior to compression.
The version of tar included with Mac OS X can simultaneously combine a directory full of files into one file and compress the result with gzip .
To archive a directory using tar:
localhost:~/Desktop vanilla$ ls -l code total 16 -rw-r--r-- 1 matisse staff 156 Nov 4 09:57 Changes -rw-r--r-- 1 matisse staff 1444 Nov 4 09:57 README drwxr-xr-x 4 matisse staff 92 Apr 16 12:44 src [localhost:~/Desktop] vanilla% tar -cvzf code.tar.gz code code code/Changes code/README code/src code/src/syntax.c code/src/syntax.h localhost:~/Desktop vanilla$ ls -l code.tar.gz -rw-r--r-- 1 matisse staff 3203 Apr 16 12:56 code.tar.gz localhost:~/Desktop vanilla$
Tips
tar has a plethora of options. See man tar for the complete list.
If you want to use the tar command in a pipeline, use the special filename - (a hyphen). This is especially useful if you are working on a Unix system where the version of tar cannot compress its output. On those systems you might use
tar -cvf - code gzip > code.tar.gz
Of course, once you have a tar file, you want to be able to reverse the process and turn it back into a directory (this is called untarring or unpacking ).
To unpack a tar archive:
localhost:~/Desktop vanilla$ tar -xvzf code.tar.gz code code/Changes code/README code/src code/src/syntax.c code/src/syntax.h localhost:~/Desktop vanilla$