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 methods for handling .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’s 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’s xz flag.

What You’ll Practice

  • Directory inspection with ls before archiving.
  • Creating archives with tar -c and -f.
  • gzip compression using gzip and extraction with tar -z.
  • xz compression using xz and decompression using unxz / xz -d.
  • Direct extraction of .tar.xz using tar -J.

Walkthrough

Step 1 : List the contents of the project directory.
Command
ls project/

Confirm what you’re packaging. This avoids shipping incomplete bundles or archiving the wrong directory due to a typo or assumption.

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

tar creates a single archive container without compression. -c creates the archive, -v shows what is added, and -f names the output file.

project/
project/README.md
project/src/
project/assets/
project/data/
Step 3 : Compress the tar archive with gzip.
Command
gzip project.tar

gzip compresses the tar file and replaces it with project.tar.gz by default. This is one of the most common archival formats for transfers.

ls -l project.tar.gz
# Confirm the file exists and has a non-zero size.
Step 4 : Extract the gzip-compressed archive.
Command
tar -xvzf project.tar.gz

-x extracts, -z tells tar to use gzip, -v prints extracted paths, and -f specifies the archive file.

project/
project/README.md
project/src/
project/assets/
project/data/
Step 5 : Recreate the uncompressed tar archive.
Command
tar -cvf project.tar project/

Rebuilding the tar file gives you a clean baseline for testing xz compression next. In real workflows, you might produce both formats depending on your transfer constraints.

project/
project/README.md
project/src/
project/assets/
project/data/
Step 6 : Compress the tar archive using xz.
Command
xz project.tar
# Optional (multithread):
xz -T0 project.tar

xz typically compresses smaller than gzip, but may take longer. By default, it replaces project.tar with project.tar.xz.

ls -l project.tar.xz
# Confirm the file exists and has a non-zero size.
Step 7 : Decompress the xz archive to restore the tar file.
Command
unxz project.tar.xz
# OR
xz -d project.tar.xz

This restores project.tar from the xz-compressed file. Knowing both variants helps when one tool is missing or when you are working on constrained environments.

ls -l project.tar
# Confirm project.tar exists again.
Step 8 : Extract a .tar.xz directly (no separate unxz step).
Command
tar -xvJf project.tar.xz

-J tells tar to use xz. This is the cleanest workflow when you receive a .tar.xz bundle and just need to extract it immediately.

project/
project/README.md
project/src/
project/assets/
project/data/

Reference

  • tar -cvf <archive.tar> <dir> : Creates an uncompressed tar archive.
    • -c : Create archive.
    • -v : Verbose output (lists files as they are added/extracted).
    • -f : Output archive filename.
  • gzip <file> : Compresses a file using gzip and typically replaces it with .gz .
  • tar -xvzf <archive.tar.gz> : Extracts a gzip-compressed tar archive.
    • -x : Extract archive.
    • -z : Use gzip compression/decompression.
  • xz <file> : Compresses a file using xz and typically replaces it with .xz .
  • unxz <file.xz> : Decompresses an xz-compressed file (same as xz -d ).
  • tar -xvJf <archive.tar.xz> : Extracts an xz-compressed tar archive directly.
    • -J : Use xz compression/decompression.