Linux Archive Extraction (tar, gz, bz2, zip)

Linux-based operating systems support various archive formats, making it essential to know how to work with them. This comprehensive tutorial provides an in-depth overview of common archive formats and their essential parameters.

Background

Archive Formats Overview

Linux users commonly encounter archive formats such as tar, gz (gzip), bz2 (bzip2), and zip. Each format serves specific purposes and comes with its unique features.

tar (Tape Archiver)

The tar program, originally named for "Tape Archiver," was initially used to back up data on tape drives. It is still widely used for its versatility.

Extracting a tar Archive

To extract a tar archive, use the following command:

1tar xfv archive.tar

Legend: (x = extract, f = file, v = verbose)

Creating a tar Archive

To create a tar archive with files or folders, use:

1tar cfv archive.tar content1 content2 content3

Legend: (c = create)

Creating Compressed tar Archives

To create compressed tar archives, use:

1tar cfzv archive.tar.gz content1 content2 content3

Legend: (z = compress)

Listing Contents of a tar Archive

To list the contents of a tar archive, use:

1tar tfv archive.tar

gz (gzip)

Since tar archives are not compressed by default, you can use gzip to compress them.

Compressing with gzip

To compress a file using gzip, run:

1gzip file

Result: file.gz

Decompressing with gzip

To decompress a gzip-compressed file, use:

1gunzip file.gz

Combining and Extracting gzipped tar Archives

To combine and extract from gzipped tar archives, use:

1tar xfvz archive.tar.gz

bz2 (bzip2)

Similar to gzip, bzip2 provides an alternative compression algorithm.

Compressing with bzip2

To compress a file using bzip2, run:

1bzip2 file

Result: file.bz2

Decompressing with bzip2

To decompress a bzip2-compressed file, use:

1bunzip2 file.bz2

Combining and Extracting bzipped tar Archives

To combine and extract from bzipped tar archives, use:

1tar xfvj archive.tar.bz2

zip

The zip tool, known in the Windows world, is also available on Linux-based systems.

Compressing with zip

To compress individual files into a zip archive, use:

1zip archive.zip content1 content2

Compressing Folders with zip

To compress entire folders into a zip archive, use:

1zip -r archive.zip folder1 folder2 folder3

Extracting zip Archives

To extract a zip archive, use:

1unzip archive.zip

Listing Contents of a zip Archive

To view the contents of a zip archive, use:

1unzip -l archive.zip

Examples (Highlighted)

  • Example 1 (tar Extraction):

    To extract a tar archive, use the following command:

    1tar xfv archive.tar
    

    Legend: (x = extract, f = file, v = verbose)

  • Example 2 (gzip Compression):

    To compress a file using gzip, run:

    1gzip file
    

    Result: file.gz

  • Example 3 (Combining and Extracting gzipped tar Archives):

    To combine and extract from gzipped tar archives, use:

    1tar xfvz archive.tar.gz
    

This tutorial provides comprehensive guidance on extracting and compressing various archive formats on Linux-based systems. Use the appropriate method based on your specific needs and background information.