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/
| Flag | Meaning |
|---|---|
-c | Create a new archive |
-z | Compress with gzip |
-f | Specify 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
| Flag | Meaning |
|---|---|
-x | Extract the archive |
-z | Decompress gzip |
-f | Specify 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
| Flag | Description |
|---|---|
-c | Create archive |
-x | Extract archive |
-z | Use gzip compression |
-j | Use bzip2 compression |
-v | Verbose (list files) |
-f | Specify archive filename |
-C DIR | Extract to specific directory |
-t | List contents without extracting |
--exclude=PATTERN | Exclude files matching pattern |
zip flags
| Flag | Description |
|---|---|
-r | Recursive (include subdirectories) |
-e | Encrypt with password |
-9 | Maximum compression |
-x PATTERN | Exclude 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
| Format | Extension | Speed | Compression | Best For |
|---|---|---|---|---|
| gzip | .tar.gz / .tgz | Fast | Good | General use, most common |
| bzip2 | .tar.bz2 | Slow | Better | When size matters more than speed |
| zip | .zip | Fast | Good | Cross-platform compatibility |
Tips
- Use
tar.gzfor Linux-to-Linux transfers and backups — it’s the standard. - Use
zipwhen 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
screento protect against SSH disconnections during the compression process. - Use
tar -tzforunzip -lto inspect an archive’s contents before extracting.
Tags:
ssh
linux
compression
tar
zip
backup