Skip to content

Overview

Linux provides powerful tools to archive and compress files/directories. The most commonly used utilities are:

  • tar → Create and extract archive files

  • gzip → Compress files using gzip format

These tools are widely used for:

  • Backups

  • File transfer

  • Log storage

  • Packaging projects


tar — Archive Utility

Description

tar (Tape Archive) is used to combine multiple files and directories into a single archive file.

  • Archive format: .tar

  • Does not compress by default


Basic Syntax

tar [options] archive_name.tar files/directories

Create Archive

tar -cf file.tar File/

Explanation

  • -c → create archive

  • -f → specify archive file name

  • File/ → directory or file to archive


Create Archive with Verbose Output

tar -cvf file.tar File/

Explanation

  • -v → verbose (shows files being processed)

Example Output

File/
File/test1.txt
File/test2.txt

Extract Archive

tar -xvf file.tar

Explanation

  • -x → extract archive

  • -v → verbose output

  • -f → archive file


Extract to Specific Directory

tar -xvf file.tar -C /path/to/directory

Explanation
Extracts contents into a specified location.


gzip — Compression Utility

Description

gzip is used to compress files and reduce size.

  • Common format: .gz

  • Often combined with tar to create .tar.gz


Create Compressed Archive (.tar.gz)

tar -czf file.tar.gz File/

Explanation

  • -c → create archive

  • -z → compress using gzip

  • -f → specify file name


Create with Verbose Output

tar -czvf file.tar.gz File/

Extract .tar.gz Archive

tar -xzf file.tar.gz

Explanation

  • -x → extract

  • -z → decompress gzip

  • -f → file name


Extract with Verbose Output

tar -xzvf file.tar.gz

Extract to Specific Directory

tar -xzf file.tar.gz -C /path/to/directory

Common tar Options Summary

Option Meaning
-c Create archive
-x Extract archive
-f Specify file name
-v Verbose output
-z Use gzip compression

Practical Examples

Example 1: Archive a Folder

tar -cvf backup.tar project/

Example 2: Compress a Folder

tar -czvf backup.tar.gz project/

Example 3: Extract Archive

tar -xvf backup.tar

Example 4: Extract Compressed Archive

tar -xzf backup.tar.gz

tar vs gzip

Feature tar gzip
Purpose Archive files Compress files
Output .tar .gz
Compression No Yes
Usage Together Yes (.tar.gz) Common

Important Notes

  • tar only archives, not compresses (unless -z is used)

  • .tar.gz is widely used for backups and distribution

  • Always verify archive contents before extraction

  • Use -v for debugging and visibility


Summary Table

Command Purpose
tar -cf Create archive
tar -cvf Create archive with details
tar -xvf Extract archive
tar -czf Create compressed archive
tar -xzf Extract compressed archive

Conclusion

The tar and gzip utilities are essential for:

  • Managing backups

  • Reducing file sizes

  • Transferring data efficiently

Mastering these commands enables efficient file handling in Linux environments, especially in system administration and cybersecurity workflows.