Compressing and Decompressing Files Through the Shell

Another file management operation is compression and decompression of files, or the creation, listing, and expansion of file and directory archives. Linux distributions usually include several compression utilities that you can use to create, compress, expand, or list the contents of compressed files and archives. These commands include

• bunzip2 Expands a compressed file bzip2 Compresses or expands files and directories

• gunzip Expands a compressed file

• gzip Compresses or expands files and directories

• shar file Creates a shell archive of files

• tar Creates, expands, or lists the contents of compressed or uncompressed file or directory archives known as tape archives or tarballs

• unshar Reassembles files from the shell archive

• uudecode file.uu Decodes an uuencoded text file to its binary form

• uuencode file Encodes a binary file to text file format for transmission via email

Most of these commands are easy to use. The tar command, however, has a somewhat complex (although capable) set of command-line options and syntax. Even so, you can quickly learn to use tar by remembering a few simple invocations on the command line. For example, to create a compressed archive of a directory, use tar's czf options like this:

$ tar czf dirname.tgz dirname

The result will be a compressed archive (a file ending in .tgz) of the specified directory (and all files and directories under it). Add the letter v to the preceding options to view the list of files added during compression and archiving. To list the contents of the compressed archive, substitute the c option with the letter t, like this:

$ tar tzf archive

Of course, if many files are in the archive, a better invocation (to easily read or scroll through the output) is

$ tar tzf archive | less

To expand the contents of a compressed archive, use tar's zxf options, like so: $ tar zxf archive tar decompresses the specified archive and extracts the contents in the current directory.


Was this article helpful?

0 0

Post a comment