skip to content
 

Packaging and compressing files

If you have large quantities of data which you don't refer to very often, it is useful to compress it so that it takes up less space. If you have a directory full of files and want to transfer the whole thing from one place to another, or to make it available on the web for other people to download, it is useful to package aka archive it up into a single file as well as compressing it.

Note: When you archive files, the original files do not disappear, and when you extract files from an archive, the archive file does not disappear either. However, compressing files replaces them by the compressed version, and the same for uncompressing files.

Linux provides several tools for doing this. The most commonly used and versatile one is tar. The others are listed largely in case you come across files which have been packaged or compressed in an unusual way and want to retrieve the original files.

For Windows users, 7-Zip is a free program which can cope with most archive file formats.

tar

tar is a Linux program for concatenating multiple files into one big file called a "tarball" and reversing this process by extracting the files from the tarball.

To create a tarball of a directory:

tar czvf directory_name.tar.gz directory_name

To see what's in the tarball:

tar tzvf directory_name.tar.gz

To extract the files from the tarball:

tar xzvf directory_name.tar.gz

Or you could use "j" instead of "z" for more compression:

tar cjvf directory_name.tar.bz2 directory_name # Create
tar tjvf directory_name.tar.bz2                # List contents
tar xjvf directory_name.tar.bz2                # Extract

There are many other tar options - type man tar for details.

Other archivers - zip and unrar

  • zip -r foo.zip foo creates an archive of the directory foo called foo.zip.
  • unzip -l foo.zip lists the contents of the zip file.
  • unzip foo.zip extracts the files from the zip file.

rar is a proprietary archive format, so Linux does not come with a program to create .rar files. If someone sends you a .rar file, view its contents with unrar l file.rar and extract them with unrar e file.rar.

Compression programs - bzip2, gzip, compress

File extension Compression command Decompression command
.bz2 bzip2 bunzip2
.gz gzip gunzip
.Z compress uncompress