How to Create Compressed Archives

Disk space on a dedicated server is a premium commodity. One convenient way to save hard drive space on your server is to compress files and directories. This is particularly useful for files that are not commonly used or that only serve as backups. It is also useful if you need to transfer large amounts of data to another server.

Linux supports numerous file compression formats with the aid of free command line tools, including the popularly-used ZIP format. The most common Linux archive format, however, is tar, which is usually used in conjunction with a compressor, such as gzip or bzip2. For example, a set of files archived with tar will be named like “archive.tar”. If it is additionally compressed with gzip, it will look like “archive.tar.gz”.

Creating a tar/gzip archive

1. Login to your server via SSH
2. Place the files you want to archive in a single directory (it can contain sub-directories).
3. Enter the following command line string:

tar -czf archive-name.tar.gz /path/to/files

This will create the tar archive and then compress it with gzip.

Creating a tar/bzip2 archive

1. Login to your server via SSH
2. Place the files you want to archive in a single directory
3. Enter the following command line string:

tar -cjf archive-name.tar.bz2 /path/to/files

The resulting file with be a tar archive compressed with bzip2.

To extract a tar.gz file, enter:

tar -zxvf archive-name.tar.gz

To extract a tar.bz2 file, enter:

tar -zxvf archive-name.tar.bz2

What the commands mean

c – means that tar will create a new archive
z – tells tar to compress the file with gzip
j – tells tar to compress the file with bzip2
f – tells tar to use an archive file.
x – extract the specified archive
v – verbose (i.e. show detailed results).











Comments: