How to Compress and Extract Files in Linux via Command Line

Quick Answer

On Linux, you can compress and extract files using the tar or zip commands. For example: run tar -czvf archive.tar.gz folder/ to compress a directory, and tar -xzvf archive.tar.gz to extract it. For ZIP files, use zip -r archive.zip folder/ and unzip archive.zip.

When administering a Linux VPS, creating source code backups or transferring data between servers is a daily task. Mastering compression and extraction commands via the Command Line Interface (CLI) not only speeds up your workflow but also significantly optimizes storage and network bandwidth.

Table of Contents

1. Quick Commands Summary (Reference Table)

Here is a quick reference table for sysadmins who need the right command immediately:

Format Compress Command Extract Command
.tar.gz tar -czvf file.tar.gz folder/ tar -xzvf file.tar.gz
.tar.bz2 tar -cjvf file.tar.bz2 folder/ tar -xjvf file.tar.bz2
.zip zip -r file.zip folder/ unzip file.zip

2. How TAR, GZIP, and XZ Work

In Windows environments, formats like .zip and .rar handle both archiving (bundling) and compression simultaneously. On Linux, these two processes are technically distinct:

  • TAR (Tape Archive): A tool used to bundle multiple files and directories into a single file (called a tarball) without reducing size. For example, to create an uncompressed archive, use: tar -cvf backup.tar folder/
  • GZIP / BZIP2 / XZ: Algorithms that actually compress data to reduce file size.

This is why Linux archives often have double extensions like .tar.gz, .tar.bz2, or .tar.xz (Bundled by TAR, then compressed by the respective algorithm).

3. Managing .tar.gz Files

This is the most common compression standard on Linux due to its excellent balance between speed and compression ratio.

How to compress a directory

CREATE TAR.GZ
tar -czvf backup-source.tar.gz /var/www/html/

How to extract

To extract the archive into your current directory, run:

EXTRACT TAR.GZ
tar -xzvf backup-source.tar.gz

Tip: Use the -C flag to extract to a specific destination folder: tar -xzvf file.tar.gz -C /destination-folder/

How to list contents without extracting

LIST ARCHIVE
tar -tzvf backup-source.tar.gz

4. Managing .tar.bz2 Files

This uses the BZIP2 algorithm, providing a higher compression ratio than GZIP (smaller file sizes) at the cost of higher CPU usage and longer processing times.

How to compress

CREATE TAR.BZ2
tar -cjvf backup-source.tar.bz2 /var/www/html/

How to extract

EXTRACT TAR.BZ2
tar -xjvf backup-source.tar.bz2

5. Managing .zip Files

The ZIP format is incredibly useful when you need to download files from your VPS to a personal computer (Windows/macOS) due to its cross-platform compatibility. Note: The zip tool might not be pre-installed on minimal Linux distributions.

INSTALL ZIP
# On Ubuntu/Debian:
sudo apt-get install zip unzip -y

# On CentOS/AlmaLinux:
sudo yum install zip unzip -y

How to recursively compress a directory

CREATE ZIP
zip -r backup-source.zip /var/www/html/

How to extract

EXTRACT ZIP
unzip backup-source.zip

6. Common TAR Command Flags Explained

To master the tar command, you need to understand the arguments constructing it:

  • -c (Create): Tells tar to create a new archive.
  • -x (Extract): Tells tar to extract an archive.
  • -z (Gzip): Directs tar to use gzip compression (.tar.gz).
  • -j (Bzip2): Directs tar to use bzip2 compression (.tar.bz2).
  • -v (Verbose): Verbose output, showing the progress of files being processed.
  • -f (File): Specifies the filename of the archive (must be the last flag before the filename).
  • -t (List): Lists the contents of an archive without extracting it.

7. Comparison of Compression Formats

Choosing the wrong format for a multi-gigabyte backup can lock up your server's CPU. Reference this table:

Format Compression Ratio Speed Best Use Case
.tar.gz Good Fast Daily tasks, internal website backups.
.tar.bz2 / .tar.xz Highest Very Slow (CPU heavy) Long-term archiving (Cold storage) to save disk space.
.zip Medium Fast Cross-platform sharing (Linux to Windows/macOS).

8. Conclusion

Mastering .tar.gz and .zip commands is a mandatory skill for any system administrator. It gives you full control over data backups, website migrations, and storage optimization across open-source operating systems.

Select the Optimal Server Solution

Depending on your requirements for performance and flexibility, VietHosting has the ready-to-deploy hardware infrastructure to fuel your business growth.

More Technical Guides

Explore additional technical guides and practical tutorials to optimize performance and manage your server infrastructure efficiently.