How to Compress and Archive Files in Unix

File Compression and Archiving in Unix

Unix provides powerful tools to compress and archive files, making it easier to store, transfer, and back up large sets of data. Whether you need to reduce file sizes or bundle multiple files into a single archive, Unix has built-in utilities to handle these tasks efficiently.

In this blog, we’ll explore the most commonly used commands for file compression and archiving: tar, gzip, bzip2, xz, and zip. You’ll learn how to create, extract, and manage compressed files in Unix.


1. Archiving Files with tar

The tar (tape archive) command is the most common tool for archiving files in Unix. It combines multiple files into one archive file, which can then be compressed to save space.

Basic Syntax

tar [options] archive_name files_to_archive
  • archive_name: The name of the archive file.
  • files_to_archive: The files or directories to include in the archive.

Examples of Using tar

  1. Create a Simple Archive:
    To create an archive of a directory (myfolder), use:
   $ tar -cvf archive.tar myfolder/
  • -c: Create a new archive.
  • -v: Verbose output, showing the files being archived.
  • -f: Specify the name of the archive file (archive.tar).
  1. Extract an Archive:
    To extract the contents of archive.tar:
   $ tar -xvf archive.tar
  • -x: Extract the contents of the archive.
  1. Create a Compressed Archive with gzip:
    You can combine tar with compression to save space. For example, to create a .tar.gz archive:
   $ tar -czvf archive.tar.gz myfolder/
  • -z: Compress the archive using gzip.
  1. Extract a Compressed Archive:
    To extract a .tar.gz file:
   $ tar -xzvf archive.tar.gz
  1. Create a .tar.bz2 Archive (using bzip2):
   $ tar -cjvf archive.tar.bz2 myfolder/
  • -j: Compress the archive using bzip2.

2. Compressing Files with gzip, bzip2, and xz

While tar is great for archiving, you can also use standalone compression tools like gzip, bzip2, and xz to compress individual files or archives.

Using gzip

gzip is a popular tool for compressing files, producing .gz files.

Basic Syntax
gzip filename
Example:

Compress a file (file.txt):

$ gzip file.txt

This creates a compressed file file.txt.gz and deletes the original file.txt by default.

To keep the original file and create a compressed copy:

$ gzip -c file.txt > file.txt.gz

Using bzip2

bzip2 offers better compression than gzip, producing .bz2 files.

Basic Syntax
bzip2 filename
Example:

Compress a file (file.txt) using bzip2:

$ bzip2 file.txt

This creates a compressed file file.txt.bz2.

To keep the original file and create a compressed copy:

$ bzip2 -c file.txt > file.txt.bz2

Using xz

xz is another compression tool that provides even higher compression ratios than both gzip and bzip2, resulting in .xz files.

Basic Syntax
xz filename
Example:

Compress a file (file.txt) using xz:

$ xz file.txt

This creates a compressed file file.txt.xz.

To keep the original file and create a compressed copy:

$ xz -c file.txt > file.txt.xz

3. Decompressing Files

You can use the same tools (gzip, bzip2, xz) to decompress files. Here’s how:

Decompress a .gz File with gunzip

$ gunzip file.txt.gz

Decompress a .bz2 File with bunzip2

$ bunzip2 file.txt.bz2

Decompress a .xz File with unxz

$ unxz file.txt.xz

4. Creating and Extracting .zip Archives

The zip and unzip utilities are commonly used for compressing and extracting .zip files, especially for cross-platform compatibility (e.g., with Windows users).

Create a .zip Archive

$ zip archive.zip file1.txt file2.txt

You can also zip entire directories:

$ zip -r archive.zip myfolder/
  • -r: Recursively zip a directory and its contents.

Extract a .zip Archive

$ unzip archive.zip

5. Combining tar with zip (Rare Use)

Although tar is more common for Unix-based compression, you might encounter .tar.zip archives. These files are a combination of both .tar and .zip formats, and can be handled as follows:

  1. Create a .tar.zip File:
   $ tar -cvf - myfolder/ | zip archive.zip -
  1. Extract a .tar.zip File:
   $ unzip archive.zip
   $ tar -xvf archive.tar

Practice Time!

  1. Create a .tar.gz archive of a directory (mydata) and extract it:
   $ tar -czvf mydata.tar.gz mydata/
   $ tar -xzvf mydata.tar.gz
  1. Compress a file (file.txt) using gzip, bzip2, and xz:
   $ gzip file.txt
   $ bzip2 file.txt
   $ xz file.txt
  1. Create a .zip archive and extract it:
   $ zip myarchive.zip file1.txt file2.txt
   $ unzip myarchive.zip

Summary

  • Use tar for archiving files and combine it with compression (gzip, bzip2, xz) for efficient storage.
  • Use gzip, bzip2, and xz for compressing individual files.
  • Use zip and unzip for cross-platform file compression and extraction.
  • Decompress files with gunzip, bunzip2, and unxz.

Leave a Reply

Your email address will not be published. Required fields are marked *