Overview
tar and gzip are the standard tools for file compression on Linux. tar bundles files and directories into a single archive. gzip compresses that archive to reduce its size. Most of the time you’ll use them together, which is why you’ll see the .tar.gz extension everywhere in Linux hosting environments.
You’ll need this if you’re downloading a site backup from your VPS, packaging up a WordPress installation before migrating it, compressing log files that are bloating your disk, or extracting software tarballs. These are all daily tasks in server management.
This article covers the most common tar and gzip operations on Linux — compression, extraction, listing archive contents, and a few combinations that save time. All commands below are tested on Ubuntu 22.04, Debian 12, AlmaLinux 8/9, and Rocky Linux 8/9, which cover the vast majority of VPS SSD Hosting environments at Host & Tech.
Prerequisites
- SSH access to your Linux server (root or a sudo-capable user)
tarinstalled — it’s included by default on virtually every Linux distributiongzipandbzip2installed (also included by default on most distros)- Enough free disk space to hold the compressed archive — check with
df -h - Basic familiarity with navigating directories using
cdandls
Step-by-Step Instructions
Step 1: Create a Compressed tar Archive (.tar.gz)
This is the command you’ll use most. It creates a .tar.gz file from a directory or set of files.
tar -czvf archive-name.tar.gz /path/to/directory
Here’s what each flag does:
-c— create a new archive-z— compress it using gzip-v— verbose output (lists files as they’re added — useful for verification)-f— specifies the archive filename (must come last, immediately before the filename)
Real example: Compressing a WordPress site directory before migration:
tar -czvf wordpress-backup-2026-01-15.tar.gz /var/www/html/wordpress
📝 Note: The -v flag is optional. Leave it out if you’re archiving thousands of files — printing every filename to the terminal slows things down noticeably on large directories.
Step 2: Extract a .tar.gz Archive
To extract the contents of a .tar.gz file into the current directory:
tar -xzvf archive-name.tar.gz
To extract into a specific directory instead:
tar -xzvf archive-name.tar.gz -C /path/to/destination/
⚠ Warning: tar will overwrite existing files without asking. If you’re extracting into a live web directory, make sure you’re not about to clobber files you need. Either extract to a temp directory first, or verify the archive contents with Step 3 before extracting.
Step 3: List Archive Contents Without Extracting
Before extracting, it’s good practice to see what’s inside the archive. This is especially true when extracting someone else’s archive — some tarballs dump everything into the current directory with no containing folder, which creates a mess.
tar -tzvf archive-name.tar.gz
If the output shows paths starting with ./ or a directory name, you’re fine. If you see individual files at the root with no wrapping directory, extract to a dedicated folder using -C as shown in Step 2.
Step 4: Compress a Single File with gzip
If you just need to compress a single file (not a directory), you can use gzip directly:
gzip filename.log
This replaces filename.log with filename.log.gz. The original is deleted automatically.
📝 Note: If you want to keep the original file, use gzip -k filename.log. The -k (keep) flag is available in gzip 1.6 and later, which is standard on all modern distros.
To decompress a .gz file:
gunzip filename.log.gz
Step 5: Create a .tar.bz2 Archive (Better Compression Ratio)
If disk space or transfer size matters more than speed, bzip2 compression typically produces smaller files than gzip, though it’s slower. Swap the -z flag for -j:
tar -cjvf archive-name.tar.bz2 /path/to/directory
To extract a .tar.bz2:
tar -xjvf archive-name.tar.bz2
In my experience, .tar.gz is the right default for most hosting tasks — it’s faster and universally supported. Use .tar.bz2 only when you’re storing archives long-term and the size difference is meaningful.
Step 6: Exclude Files or Directories from an Archive
This one comes up constantly when backing up a CMS installation. You often want to exclude the node_modules folder, cache directories, or large upload folders that don’t need to be in a code backup.
tar -czvf backup.tar.gz /var/www/html/mysite
--exclude='/var/www/html/mysite/wp-content/uploads'
--exclude='/var/www/html/mysite/node_modules'
⚠ Warning: The path in --exclude must match exactly how tar sees it internally. A common gotcha — if your source path has a trailing slash, the internal paths differ. Test with -t (list) first if you’re unsure the exclusion worked.
Step 7: Extract a Single File from an Archive
You don’t have to extract an entire archive to retrieve one file. Specify the internal path directly:
tar -xzvf archive-name.tar.gz var/www/html/mysite/wp-config.php
📝 Note: Don’t include a leading slash in the internal path when extracting individual files — tar strips the leading slash when archiving, so /var/www/html/... is stored as var/www/html/... internally. The path you specify here must match what tar -t shows.
Common Issues & Troubleshooting
“tar: Removing leading ‘/’ from member names”
Cause: This is informational, not an error. When you specify absolute paths (starting with /), tar strips the leading slash before storing the file. This is actually intentional safety behaviour — it prevents extraction from overwriting system files at arbitrary absolute paths.
Fix: Nothing to fix. Your archive is fine. If you want to suppress the message, add --warning=no-leading-slash to your command.
“gzip: stdin: not in gzip format” or “tar: This does not look like a tar archive”
Cause: The file extension doesn’t match the actual format. This happens with incomplete downloads, corrupted transfers, or files that were renamed. A .tar.gz file that’s actually just a .tar (uncompressed) will trigger this.
Fix: Run file archive-name.tar.gz to see what the file actually is. Then use the appropriate flags — drop -z if it’s an uncompressed tar, or use -j if it’s actually bzip2-compressed.
“No space left on device” During Compression
Cause: You’re trying to compress into the same filesystem that’s full, or the compressed output is going onto a partition without enough room.
Fix: Check available space first with df -h. If your /home partition is full, write the archive to a different mount point — for example, /tmp or a mounted backup volume. On a Host & Tech VPS you can also expand your storage or attach a block storage volume.
df -h
tar -czvf /tmp/backup.tar.gz /var/www/html/mysite
Extraction Is Extremely Slow
Cause: Usually one of two things — either the archive is very large and disk I/O is the bottleneck, or you’re running -v (verbose) on an archive with tens of thousands of small files. Printing each filename to the terminal has real overhead.
Fix: Drop the -v flag. If you need to verify integrity without full extraction, use tar -tzf archive.tar.gz | wc -l to count files, or tar -tzf archive.tar.gz | head -20 to spot-check the contents.
Permission Denied When Extracting
Cause: The archive was created by root and you’re extracting as a non-root user, or the destination directory has restrictive permissions.
Fix: Either use sudo tar -xzvf archive.tar.gz -C /destination/, or change ownership of the destination directory first. If you’re extracting a cPanel backup that was created as root, you’ll almost always need sudo.
FAQ
Frequently Asked Questions
What's the difference between .tar, .tar.gz, and .tar.bz2?
A .tar file is just a bundle — multiple files and folders packed into one, with no compression. A .tar.gz is that same bundle compressed with gzip, which is faster to create and widely supported. A .tar.bz2 uses bzip2 compression, which is slower but usually produces a smaller file. For most hosting tasks, .tar.gz is the right choice.
How do I compress a directory without including the full absolute path?
Change into the parent directory first, then reference the target by its relative name. For example, cd /var/www/html && tar -czvf ~/backup.tar.gz mysite/ — this archives mysite/ with a relative path, so extraction won’t try to recreate the full /var/www/html/ structure at the destination.
Can I compress files on Linux without using the command line?
If you’re on shared hosting or managed WordPress hosting with cPanel, yes — cPanel’s File Manager has a built-in Compress tool under the toolbar. Select your files or folder, click Compress, and choose the archive format. Extraction works the same way via the Extract button. For VPS or dedicated servers without a control panel, the command line is the practical option.
How do I see the size of a tar.gz file before and after compression?
Use du -sh /path/to/directory to check the original size, then ls -lh archive.tar.gz after compression to see the archive size. For a quick ratio, gzip prints compression statistics if you use the -v flag: gzip -v filename shows the original size, compressed size, and percentage saved.