File Archiving and Compression: A Linux Developer's Guide
Master file archiving and compression in Linux. Learn to use tar and gzip for efficient backups and file management in your server environments.

Previously in this course, we covered monitoring disk usage to identify space-hogging directories. Today, we turn our attention to managing that space by bundling and compressing files for backups and easier transfers.
Understanding Archiving vs. Compression
In Linux, "archiving" and "compression" are two distinct but often combined tasks.
- Archiving (
tar): This is the process of taking multiple files or directories and bundling them into a single file (called a tarball). Crucially, a standard tarball does not reduce the file size; it simply preserves the original structure, file permissions, and directory hierarchy. - Compression (
gzip): This is the process of applying a mathematical algorithm to reduce the size of a file.
We typically use these together. You bundle your project files with tar and then apply gzip to the resulting archive, creating a .tar.gz file.
Creating Archives with tar
The tar (Tape Archive) command is the standard utility for bundling files. To create an archive, you use the following common flags:
-c: Create an archive.-v: Verbose (shows you what files are being added).-f: Filename (specifies the name of the archive).
Worked Example: Bundling Web Logs
In our ongoing web server project, we have a /var/log/my-web-app/ directory. Let's create an archive of those logs:
Bash# Create an archive named logs_backup.tar tar -cvf logs_backup.tar /var/log/my-web-app/
Compressing Files with gzip
Once you have your tar file, you can shrink it using gzip. While you can run gzip as a separate command, tar includes a shortcut flag: -z.
When you add -z to your tar command, it tells tar to pass the stream through gzip automatically, resulting in a compressed archive in one step.
Worked Example: Creating a Compressed Archive Let's recreate the archive, but this time compressed:
Bash# Create a compressed archive named logs_backup.tar.gz tar -cvzf logs_backup.tar.gz /var/log/my-web-app/
The resulting file is significantly smaller, making it perfect for off-site backups.
Extracting Archives
To extract a compressed archive, use the -x (extract) flag instead of -c.
Bash# Extract the contents of a compressed archive tar -xvzf logs_backup.tar.gz
Hands-on Exercise: Project Backup
As part of our web server hardening project, let’s archive our configuration files.
- Navigate to your project root (e.g.,
/home/user/web-project). - Create a directory named
backups. - Use
tarto create a compressed archive of your current configuration files into thebackupsdirectory. - Verify the archive exists using
ls -lh.
Common Pitfalls
- Forgetting the
-fflag: If you omit-f,tarwill try to use the default tape device, which will lead to errors. Always put-flast, immediately followed by the filename. - Absolute vs. Relative Paths: By default,
tarstrips the leading/from absolute paths to prevent overwriting system files when extracted. If you want to keep the full path, use the--absolute-namesflag, but be very careful when extracting those files later. - Compression Confusion: Don't confuse
.tar.gzwith other formats like.zipor.rar. While Linux can handle those with other tools (likeunzip),tarandgzipare the native standard for POSIX-compliant systems.
FAQ
Q: Can I compress individual files with tar?
A: tar is for bundling. If you only have one file, just use gzip filename.
Q: How can I see what's in an archive without extracting it?
A: Use the -t flag: tar -tvf archive.tar.
Q: Is there a way to delete the original files after archiving?
A: Yes, use the --remove-files flag. Use it with caution, as it permanently deletes your source data once the archive is successfully created.
Recap
We’ve learned that tar bundles files while gzip compresses them. By combining these, we can efficiently manage data for backups. You now have the skills to bundle project directories, compress them for storage, and extract them when needed.
Up next: We will dive into remote file transfers to move these backups to secure storage locations.
Work with me

VPS Server Setup, Deployment & Hardening
Get your app live on a fast, secure server — properly configured, hardened, and deployment-ready. No more wrestling with the command line.

Custom WordPress Plugin Development
Custom WordPress & WooCommerce plugins built to standard — by the developer behind a plugin with 5,000+ active installs and a SaaS with 10,000+ users.


