Loading...

Lab 14: Archiving and Compressing Files

Package a project directory into portable archives and compare gzip vs xz compression workflows using tar. Validate results by listing and extracting archives, then confirm multiple correct ways to handle .tar.gz and .tar.xz.

core troubleshooting storage

Scenario

You need to archive a project directory and ship it to another system for review. You will create an uncompressed tar archive, compress it with gzip, extract it, then repeat the process with xz and verify that you can extract .tar.xz directly.

Operator context

Archiving and compression show up everywhere: backups, incident bundles, offline transfers, and collecting evidence for escalation. The goal is doing it correctly and being able to prove what is inside.

Objective

  • Inspect a project directory before packaging it.
  • Create an uncompressed tar archive from project/.
  • Compress the archive with gzip and extract it using tar.
  • Compress the archive with xz and restore the tar file.
  • Extract an xz-compressed tar directly using tar.

Concepts

  • tar archives files and directories into a single container. Compression is optional.
  • .tar.gz is tar + gzip. .tar.xz is tar + xz.
  • gzip is usually faster; xz usually compresses smaller. Choose based on time vs size constraints.
  • Verify archives before shipping: list contents, confirm size, and test extraction.

Walkthrough

Step 1: Inspect the directory you are about to package.
Command
ls project/

Confirm what you are packaging. This avoids shipping incomplete bundles or archiving the wrong path due to a typo or assumption.

README.md  src/  assets/  data/
Step 2: Create an uncompressed tar archive.
Command
tar -cvf project.tar project/

-c creates the archive, -v prints what is added, and -f names the output file. This produces a single tarball without compression.

project/
project/README.md
project/src/
project/assets/
project/data/
Step 3: List the contents of the tar archive.
Command
tar -tvf project.tar

Before compressing or shipping, confirm what is inside. Listing is a fast sanity check and a good habit for handoffs.

# Example output (truncated):
drwxr-xr-x vonds vonds 0 2026-02-08 10:00 project/
-rw-r--r-- vonds vonds 0 2026-02-08 10:00 project/README.md
Step 4: Compress the tar archive with gzip.
Command
gzip project.tar

gzip compresses the tar file and replaces it with project.tar.gz by default.

ls -lh project.tar.gz
Step 5: Extract the gzip-compressed tar archive.
Command
tar -xvzf project.tar.gz

-x extracts, -z enables gzip handling, -v prints extracted paths, and -f selects the archive.

project/
project/README.md
project/src/
project/assets/
project/data/
Step 6: Create an xz-compressed tar archive (single command).
Command
tar -cJvf project.tar.xz project/

This is the most direct workflow: tar creates the archive and handles xz compression in one step. -J tells tar to use xz.

ls -lh project.tar.xz
Step 7: Extract a .tar.xz directly.
Command
tar -xvJf project.tar.xz

Direct extraction is ideal when you receive a .tar.xz bundle and just need to unpack it immediately.

project/
project/README.md
project/src/
project/assets/
project/data/
Step 8: Alternative method (compress an existing tar with xz).
Command
# If you already have project.tar:
xz project.tar

# Restore the tar later:
unxz project.tar.xz
# OR
xz -d project.tar.xz

This workflow shows up in real environments when a tar already exists and you need to compress it after the fact.

Common breakpoints

Wrong directory archived

Always run ls first and consider using absolute paths if you are in a busy working directory.

Extracted into the wrong location

Use -C <dir> when extracting to control where files land, for example: tar -xvzf project.tar.gz -C /tmp.

tar cannot handle xz

On minimal systems, xz support may be missing. Install xz tooling or decompress first with unxz, then extract the tar.

Cleanup checklist

Remove archives and any extracted test directories if you are working in a shared or constrained environment.

Commands
rm -f project.tar project.tar.gz project.tar.xz
# Remove extracted test directory if needed:
rm -rf project/

Reference

  • tar -cvf <archive.tar> <dir>: Creates an uncompressed tar archive.
    • -c: Create archive.
    • -v: Verbose output.
    • -f: Archive filename.
  • tar -tvf <archive.tar>: Lists contents of a tar archive.
  • gzip <file>: Compresses a file with gzip and replaces it with .gz.
  • tar -xvzf <archive.tar.gz>: Extracts a gzip-compressed tar archive.
    • -x: Extract.
    • -z: Use gzip.
  • tar -cJvf <archive.tar.xz> <dir>: Creates an xz-compressed tar archive.
    • -J: Use xz.
  • tar -xvJf <archive.tar.xz>: Extracts an xz-compressed tar archive directly.
  • xz <file>: Compresses a file with xz and replaces it with .xz.
  • unxz <file.xz> / xz -d <file.xz>: Decompresses an xz-compressed file.