Skip to main content

Command Palette

Search for a command to run...

Archiving and Compressing Files

Updated
View as Markdown

Overview

In Linux, archiving and compression are two different operations:

  1. Archiving = Combine multiple files/directories into a single file.
  2. Compression = Reduce the size of that file.
Files/Directories
       │
       ▼
     tar
       │
       ▼
 archive.tar
       │
       ▼
    gzip
       │
       ▼
archive.tar.gz

1. gzip

gzip (GNU Zip) is a compression utility.

A file ending in .gz is a gzip-compressed file.

Compress a File

gzip file

Result:

file.gz

Decompress a File

gunzip file.gz

Result:

file

Important

gzip does not create archives.

It can only compress a single file.

report.txt
     │
     ▼
   gzip
     │
     ▼
report.txt.gz

2. tar

tar (Tape Archive) is used for archiving files and directories.

Unlike gzip, tar can package multiple files and directories into a single archive.

Create an Archive

tar cvf archive.tar file1 file2 dir1

Result:

archive.tar

Understanding tar Options

c = Create

Create a new archive.

tar cvf archive.tar files

x = Extract

Extract files from an archive.

tar xvf archive.tar

t = Table of Contents

List files inside an archive without extracting.

tar tvf archive.tar

v = Verbose

Show files being processed.

tar xvf archive.tar

Example output:

file1
file2
dir1/file3

Double Verbose

Some tar versions support:

tar xvvf archive.tar

Shows additional details such as:

  • Permissions
  • Ownership
  • File size

f = File

Specifies the archive file name.

tar cvf archive.tar files

The argument immediately after f must be the archive filename.


Creating Archives

Suppose we have:

project/
├── app.py
├── config.yaml
└── README.md

Create archive:

tar cvf project.tar project/

Result:

project.tar

Extracting Archives

Extract all files:

tar xvf project.tar

Result:

project/
├── app.py
├── config.yaml
└── README.md

Extracting Specific Files

You can extract specific items if you know their exact names.

tar xvf project.tar project/config.yaml

Important Note

Extracting does not remove the archive itself.

Before:

project.tar

After extraction:

project.tar
project/

The archive remains.


Table of Contents Mode

Before extracting, it is a good practice to inspect the archive.

tar tvf project.tar

Example:

project/
project/app.py
project/config.yaml
project/README.md

Why Check Contents First?

A badly structured archive may dump files directly into the current directory.

Good archive:

project/
project/file1
project/file2

Bad archive:

file1
file2
file3

Extracting the bad archive may clutter your current directory.

Recommendation

Create a temporary directory first:

mkdir temp
cd temp
tar xvf ../archive.tar

Preserving Permissions

Use the p option when extracting:

tar xvpf archive.tar

This preserves:

  • File permissions
  • Ownership information

Useful for:

  • Backups
  • System restores
  • Configuration archives

Important Root User Note

When extracting as root:

sudo tar xvpf archive.tar

Do not interrupt tar before it finishes.

Reason:

  • tar checks the archive completely
  • Permissions may be applied near the end
  • Interrupting can leave files with incorrect permissions or ownership

Always wait until the shell prompt returns:

$

before assuming extraction is complete.


Compressed Archives (.tar.gz)

Most Linux archives are distributed as:

file.tar.gz

Meaning:

tar archive
      +
gzip compression

Manual Extraction Process

Work from right to left.

Step 1: Remove .gz

gunzip file.tar.gz

Produces:

file.tar

Step 2: Extract .tar

tar xvf file.tar

Creating a .tar.gz Archive

Reverse the process.

Step 1: Create Archive

tar cvf file.tar project/

Step 2: Compress Archive

gzip file.tar

Result:

file.tar.gz

zcat

The previous approach creates a temporary .tar file.

This:

  • Uses extra disk space
  • Requires extra I/O operations

A more efficient method is:

zcat file.tar.gz | tar xvf -

What is zcat?

Equivalent to:

gunzip -dc file.tar.gz

Options:

-d = decompress
-c = write output to stdout

Understanding the Pipeline

zcat file.tar.gz | tar xvf -

Flow:

file.tar.gz
      │
      ▼
    zcat
      │
      ▼
 decompressed tar stream
      │
      ▼
     tar
      │
      ▼
 extracted files

No temporary .tar file is created.


Meaning of "-"

In:

tar xvf -

The dash means:

Read archive data from standard input (stdin)

instead of reading from a physical file.


tar's Built-in gzip Shortcut

Because .tar.gz files are extremely common, tar can call gzip automatically.

Instead of:

zcat file.tar.gz | tar xvf -

Use:

tar xzvf file.tar.gz

The z Option

z = use gzip automatically

Works with:

  • c (create)
  • x (extract)
  • t (list)

Common tar.gz Commands

Create

tar czvf backup.tar.gz backup/

Meaning:

c = create
z = gzip
v = verbose
f = filename

Extract

tar xzvf backup.tar.gz

Meaning:

x = extract
z = gunzip automatically
v = verbose
f = filename

View Contents

tar ztvf backup.tar.gz

Meaning:

t = table of contents
z = gunzip automatically
v = verbose
f = filename

.tgz Files

A:

file.tgz

is exactly the same as:

file.tar.gz

The .tgz extension exists mainly for compatibility with older FAT/MS-DOS filesystems.


Other Compression Utilities

xz

Compress:

xz file

Result:

file.xz

Decompress:

unxz file.xz

bzip2

Compress:

bzip2 file

Result:

file.bz2

Decompress:

bunzip2 file.bz2

Comparison

gzip   -> Fast compression/decompression
bzip2  -> Better compression, slower
xz     -> Best compression, usually slowest

ZIP Archives

Linux also supports Windows ZIP archives.

Create ZIP

zip archive.zip file1 file2

Extract ZIP

unzip archive.zip

Supports:

  • .zip
  • Self-extracting .exe archives

Legacy .Z Files

Files ending with:

file.Z

were created using the old Unix compress utility.

Decompress:

gunzip file.Z

Modern gzip can unpack .Z files but does not create them.


Commands to Memorize

gzip

gzip file
gunzip file.gz

tar

tar cvf archive.tar dir/
tar xvf archive.tar
tar tvf archive.tar

tar + gzip

tar czvf archive.tar.gz dir/
tar xzvf archive.tar.gz
tar ztvf archive.tar.gz

Streaming

zcat archive.tar.gz | tar xvf -

Interview One-Liners

What is gzip?

gzip compresses a file but does not combine multiple files into an archive.

What is tar?

tar combines multiple files/directories into a single archive without compressing them.

What is .tar.gz?

A .tar.gz file is a tar archive that has been compressed using gzip.

What does zcat do?

zcat decompresses a gzip file and writes the result to standard output.

Difference Between tar and gzip

tar gzip
Creates archives Compresses files
Combines multiple files Works on a single file
Produces .tar Produces .gz
No compression by default Compression only