I must admit that when I first determined to learn how to create and un-package archive files from the command line, it was a daunting prospect.
It was not so much that I didn’t understand archival and compression, but I more about the fact that I didn’t understand the difference between the utilities available in the command line.
Once I figured out the difference, actually using each one turned out to be a piece of cake (and it was good, too!)
So to make a long story short, I’ve settled on the 5 utilities that can do pretty much anything you would ever want to do on the subject.
Hopefully, the explanation and examples will give you an idea of how each utility can be used, and so prevent you from experiencing the same headache that I did.
Now the hard part will be remembering which utility to use to decompress all of your compressed files — especially 6 months later!
The zip Utility
The zip utility packages and compresses files. It can create, add to, and even un-package .zip format archive files.
The syntax is a little backwards, compared to many Linux commands. After the command and any optional options, the name of the zip file is specified (new or existing), followed by the name(s) of the file(s) or directory(ies) to put in the file. To add multiple files/directories to the .zip file, separate them with spaces. Examples:
zip LargePhotoZipped.zip MyLargePhoto.jpg
zip ArchiveFile.zip fire1.txt fire2.txt fire3.txt
zip -r backup2015-11-16.zip /home/gwen/*
Some useful options to use with the zip utility are:
-d will delete specified files from inside the archive file.
-e will prompt you for a password used to encrypt the archive file1.
-f will freshen/replace a file in the archive file if it has been updated.
-m will move files into the archive file (or delete the originals after adding them).
-r will travel the directory structure recursively to add subdirectories to the archive file.
-u will update/replace entries in the archive file if they have been modified.
-x will explicitly exclude specified files. Example: zip -r backup2015-11-16.zip /home/gwen/* -x NotImportant.txt
The zip utility, by default, prioritizes speed over compression. If the compression factor is increased, the speed at which the file is created will decrease. This is good news for anyone with a little time, that needs a very small file! Use the -1 (fastest, largest file) flag, up to the -9 (slowest, smallest file) flag before the other options, to specify the speed and compression factor of a file.
zip -9 -r TextBackup2015-11-16.zip /home/gwen/*.txt
The unzip Utility
The unzip utility is used to… you’re right! It unzips a zipped archive file! How did you guess?
The syntax is very basic: unzip backup2015-11-16.zip
-d will extract files to a specific directory (this option and the directory should be specified after the .zip filename).
-f will freshen files, or only extract files that already exist in the extraction location, and even then only if the zipped copy has been modified more recently.
-l will list the files in the specified zip archive.
-t will test an archive file for errors.
The gzip Utility
The gzip utility will compress single files into single compressed files. By default, its files are compressed with .gz extensions.
Multiple files can be compressed at the same time by separating the original file names with spaces; each one will be compressed into a separate .gz file.
The basic command syntax is: gzip filename.png
or gzip filename.png uwnrm.txt photo.jpg
Some useful options to use with the gzip utility are:
-d will un-compress a file compressed with gzip
-k will keep the original file during compression and/or decompression.
-n will not preserve the original file name and timestamp and file extension of a file.
-N will preserve the original file name and timestamp (this is the default).
-r will recursively descend into a specified directory and compress each file it finds inside.
Although gzip does not create archive files, it can compress existing archive files, making it an excellent utility to use in conjunction with tar.
The gunzip Utility
The gunzip utility will decompress files that have been compressed with the gzip utility.
It is the equivalent of using the -d option with gzip, and works with the same options/etc. as the gzip utility.
The tar Utility
The tar utility creates and extracts archive files.
The tar utility options are more specific — and necessary — than many other commands. Not only are various options nearly mandatory (which ones, depend on what you’re doing) but the option order is also very important.
To begin with, keep in mind these 3 options:
-f indicates that the name of the archive file is to directly follow.
-c will create a new archive file.
-x will extract from out of an existing archive file.
Remember how commands can be combined? Well, if you do that with tar, start by watching out for the order of the -f argument; there should be no other options between the f and the filename. Examples:
tar -cf newarchive.tar toarchive/
(Right!) … tar -fx newarchive.tar toarchive/
(Wrong!)
tar -xf newarchive.tar
(Right!) … tar -fx newarchive.tar
(Wrong!)
Notice that when the archive file is created, and argument is given that specifies which file(s) and/or directory (subdirectories are included by default) to put in the archive. Then, when the archive is extracted, no arguments are given. That is because during extraction, what may seem to you to be the path/location to extract the archive file into, would instead be interpreted by the tar utility as the file and/or directory to extract from the archive file.
As an alternative to using the gzip utility to compress a tar file, the tar utility option -z can be used, along with the appropriate file name: tar -czf newarchive.tar.gz toarchive/
Then, to decompress and extract a .tar.gz file (called a “tarball”), the same -z option should be added into the mix: tar -zxf newarchive.tar
Some tar utility functions are:
-A will append files to an archive.
-c will create a new archive file.
-d will calculate the difference between an archive and the file system.
–delete will delete files from an archive.
-r will append files to the end of a tar archive.
-t will list the contents of an archive.
-u will append only new/modified files to an archive.
-x will extract from out of an existing archive file.
Now would be a good time to go start exploring the details that have been presented here, until you can put together your own sets of options to accomplish what you want them too.
First, I will leave you will a brief list of some useful tar utility options.
-f indicates that the name of the archive file is to directly follow.
-j tells tar to use the bzip2 compressor.
-k keeps existing files (does not overwrite them when extracting from an archive).
-m sets the data modification time of extracted files to the extraction time, rather than the data modification time.
–overwrite will overwrite existing files while extracting.
–no-recursion will not allow recursion into subdirectories when archiving. (Subdirectory recursion is default in tar.)
–remove-files will remove/delete files are adding them to an archive.
-v will force verbose operation (it will print out details of what it’s doing).
-z tells tar to use the gzip compressor.
Note: Type info tar
into the command line instead of man tar
for the most comprehensive guide to using this utility.
Honorable Mention
If you run across a compressed file with an extension such as .bz2, .bz, .tbz2 or .tbz, check out man bzip2
or man bunzip
for additional details.
Summary
This is by no means a complete list of the archive and compression utilities available to Linux-users.
On that same note, it is not a comprehensive study of every detail pertaining to these five utilities.
It is, however, intended to be a good starting point for anyone learning the basics of creating and managing archive and compressed files.