Back to Blog
Lesson 43 of the Linux: Linux Command Line for Developers course
LinuxAugust 31, 20263 min read

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.

linuxcommand-linetargzipbackupsfile-management
Neatly arranged blue office binders labeled with dates and names for organized storage.

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.

  1. Navigate to your project root (e.g., /home/user/web-project).
  2. Create a directory named backups.
  3. Use tar to create a compressed archive of your current configuration files into the backups directory.
  4. Verify the archive exists using ls -lh.

Common Pitfalls

  • Forgetting the -f flag: If you omit -f, tar will try to use the default tape device, which will lead to errors. Always put -f last, immediately followed by the filename.
  • Absolute vs. Relative Paths: By default, tar strips the leading / from absolute paths to prevent overwriting system files when extracted. If you want to keep the full path, use the --absolute-names flag, but be very careful when extracting those files later.
  • Compression Confusion: Don't confuse .tar.gz with other formats like .zip or .rar. While Linux can handle those with other tools (like unzip), tar and gzip are 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.

Similar Posts