4 Archiving Tools for Linux Server Admins
tar and zipThere are all kinds of fancy backup applications, from free to complicated and expensive. But it's still hard to beat the speed, simplicity, and flexibility of the old standbys.
When tar creates an archive, it concatenates the files, each with a file header containing metadata: the file name, owner, permissions, and any link information. The metadata is in ASCII for portability. The archive will therefore transfer all of this file information along with the file data.
One occasional problem you might encounter when extracting a tarball happens when it has been created such that it will extract directly into the working directory (rather than creating its own directory). At best this is untidy; at worst it could overwrite existing files. Avoid the problem by using tar -tf file.tar to get a file listing before actually untarring it; you can then move it into a new clean directory first if necessary.
- tar -cf archive.tar mydirectory/
Creates a tarball archive.tar of the directory mydirectory/ with contents. (This will unpack correctly, by creating its own directory to unpack into.)
- tar -xf archive.tar
Extracts the tarball archive.tar.
- tar -zxf archive.tar.gz
Uncompresses and extracts the tarball. Use -j for a bzip2 archive.
- tar -vxf archive.tar
Extract the tarball with verbose output (lists all files as extracted).
zip provides both data compression and archiving; you can compress multiple files into a single archive. It's been around since 1989 and implementations exist on numerous platforms, so it's one of the most portable options (especially if you need to be able to access your archive on a Windows box).
Zip archives include a general directory, with file names and file metadata, at the end of the file. This makes it quick to list the archive as it's not necessary to read the whole thing, but only the directory.
Compression is not actually required for zip archives, although it normally is used. The DEFLATE method, which uses duplicate string elimination, as well as swapping in short versions of common symbols (and long ones of less common ones), is most commonly used. Files are compressed separately, rather than the whole archive being compressed as one. This makes access easier, but limits how much the files can be compressed by – since metadata is not compressed, an archive with many small files won't shrink as much as an archive of a small number of large files.
- zip -r myarchive dir
Creates myarchive.zip with the contents of the directory dir/
- unzip myarchive
Unzip myarchive.zip, creating subdirectories as needed. (Use -j to avoid subdirectory creation.)