Home / Linux / How to Compress and Extract Files on Linux (tar, zip, gzip)

How to Compress and Extract Files on Linux (tar, zip, gzip)

Learn the exact commands to compress/extract files using tar, zip, and gzip, with clear explanations and expected results.

Views: 18 Unique: 15 Updated: 2026-03-20

What this is

This guide is a practical reference for compressing and extracting files on Linux using tar, zip, and gzip.

What it is for

  • Create backups
  • Bundle folders to transfer to another server
  • Reduce file size for storage

Prerequisites

  • SSH access to your VPS
  • Basic file permissions to read the source files

Step-by-step: tar (most common for folders)

Create a .tar archive (no compression)

tar -cvf backup.tar /path/to/folder

What it does: Creates a tar archive.

Expected output: Lists files as they are added.

Create a .tar.gz (tar + gzip compression)

tar -czvf backup.tar.gz /path/to/folder

What it does: Creates a compressed archive.

Extract a .tar.gz

tar -xzvf backup.tar.gz

Expected output: Files extracted into the current directory.

Extract into a specific folder

mkdir -p /tmp/restore
tar -xzvf backup.tar.gz -C /tmp/restore

Step-by-step: zip (common for compatibility)

Install zip/unzip (if needed)

Ubuntu/Debian:

sudo apt update
sudo apt install -y zip unzip

RHEL-based:

sudo dnf install -y zip unzip

Zip a folder

zip -r site.zip /var/www/html

Unzip

unzip site.zip

Step-by-step: gzip (single file)

Compress a file:

gzip -k file.log

What it does: Creates file.log.gz and keeps the original (-k).

Decompress:

gunzip file.log.gz

Warnings & useful notes

  • Extraction writes files to disk; ensure you have enough space.
  • Use a dedicated restore folder to avoid overwriting existing files.

Final verification

ls -la
file backup.tar.gz

Conclusion

Use tar.gz for Linux backups, zip for compatibility, and gzip for compressing single files (like logs).

Back to category