H
10Corp Premium Hosting

How to Compress Files and Folders via SSH

Last Updated: 2025-01-01 3 min read

Overview

Compressing files and folders is essential for creating backups, preparing files for transfer, and saving disk space. Linux provides several compression tools, with tar + gzip and zip being the most commonly used.

tar + gzip (Most Common on Linux)

Create a compressed archive

tar -czf archive-name.tar.gz /path/to/directory/
FlagMeaning
-cCreate a new archive
-zCompress with gzip
-fSpecify the filename of the archive

Example — compress your entire site:

tar -czf site-backup.tar.gz /home/username/public_html/

Create with verbose output

tar -czvf site-backup.tar.gz /home/username/public_html/

The -v (verbose) flag lists each file as it’s added.

Extract a compressed archive

tar -xzf archive-name.tar.gz
FlagMeaning
-xExtract the archive
-zDecompress gzip
-fSpecify the filename

Extract to a specific directory

tar -xzf archive-name.tar.gz -C /target/directory/

List contents without extracting

tar -tzf archive-name.tar.gz

Extract a single file from an archive

tar -xzf archive-name.tar.gz path/to/specific/file.txt

tar + bzip2 (Better Compression)

bzip2 compresses more than gzip but is slower:

# Create
tar -cjf archive-name.tar.bz2 /path/to/directory/

# Extract
tar -xjf archive-name.tar.bz2

zip and unzip

The zip format is commonly used when sharing files with Windows users or when cPanel backups are in zip format.

Create a zip archive

zip archive-name.zip file1.txt file2.txt

Zip an entire directory recursively

zip -r archive-name.zip /path/to/directory/

Extract a zip archive

unzip archive-name.zip

Extract to a specific directory

unzip archive-name.zip -d /target/directory/

List contents of a zip file

unzip -l archive-name.zip

gzip and gunzip (Single File Compression)

For compressing individual files (not directories):

gzip largefile.sql          # Creates largefile.sql.gz (removes original)
gunzip largefile.sql.gz     # Decompresses back to largefile.sql

Keep the original file

gzip -k largefile.sql       # Creates .gz but keeps the original

Useful Flags Reference

tar flags

FlagDescription
-cCreate archive
-xExtract archive
-zUse gzip compression
-jUse bzip2 compression
-vVerbose (list files)
-fSpecify archive filename
-C DIRExtract to specific directory
-tList contents without extracting
--exclude=PATTERNExclude files matching pattern

zip flags

FlagDescription
-rRecursive (include subdirectories)
-eEncrypt with password
-9Maximum compression
-x PATTERNExclude files matching pattern

Practical Hosting Examples

Back up your entire site

tar -czf ~/backups/site-$(date +%Y%m%d).tar.gz /home/username/public_html/

This creates a timestamped backup like site-20250101.tar.gz.

Compress site but exclude cache and logs

tar -czf site-backup.tar.gz --exclude='*.log' --exclude='cache' /home/username/public_html/

Compress a database dump

gzip database-backup.sql
# Result: database-backup.sql.gz (70-90% smaller)

Extract a migration package

cd /home/username/public_html/
tar -xzf ~/migration-package.tar.gz

Create a password-protected zip

zip -re secure-backup.zip /home/username/sensitive-data/

Compression Format Comparison

FormatExtensionSpeedCompressionBest For
gzip.tar.gz / .tgzFastGoodGeneral use, most common
bzip2.tar.bz2SlowBetterWhen size matters more than speed
zip.zipFastGoodCross-platform compatibility

Tips

  • Use tar.gz for Linux-to-Linux transfers and backups — it’s the standard.
  • Use zip when files need to be opened on Windows or shared cross-platform.
  • Always compress database dumps with gzip — SQL files compress extremely well (often 80-90% reduction).
  • For very large archives, use screen to protect against SSH disconnections during the compression process.
  • Use tar -tzf or unzip -l to inspect an archive’s contents before extracting.
Tags: ssh linux compression tar zip backup

Still need help?

Our support team is available 24/7 to assist you.