Archive versus compression
Quick answer
An archive bundles files into one container; compression encodes data in fewer bytes when possible. A tar archive can contain many files without compression. gzip compresses a byte stream, not a directory hierarchy. Combining them creates a portable bundle such as report.tar.gz.
Prepare the practice environment
Run python3 lab-fixtures/archives/setup.py as an ordinary user from the extracted package root in a disposable Linux environment. Enter the printed directory, check pwd, then run export LC_ALL=C; umask 077; set -o pipefail. Use a fresh fixture for each example and stop if a command fails unexpectedly. All archive and output paths must be new. Outputs below are not commands.
How it works
Think of tar as packing documents into a box and gzip as shrinking the box. tar -cf ARCHIVE FILE... creates a bundle, tar -tf ARCHIVE lists its members. gzip -c FILE > FILE.gz writes compressed bytes to a new file without removing the input. gunzip -c FILE.gz writes the decoded stream. A suffix is a naming convention, not proof of format. Compression is lossless here: decoding reproduces the original bytes. Small or already compressed inputs can grow because of format overhead. The examples use GNU tar and gzip; check other implementations separately. An archive alone is not a backup strategy: independent copies and tested restoration matter.
Follow the data through two layers: notes.txt and report.txt become members of notes.tar; gzip can then encode the entire tar stream as one compressed stream. Decoding that stream recovers the tar bytes, not two separate files on disk. Extraction is a separate step. If the recipient only needs one text file, a single gzip stream may suffice; for a directory tree, an archive records the member names. Verify recovery by comparing the restored bytes, not just by checking the filename suffix.
Try the examples
The listing names two members; tar has not been asked to compress.
Expected stdout:
notes.txtreport.txtThe decoded bytes appear on stdout. Both original and compressed file remain.
Expected stdout:
hello archivetar -czf notes.tar.gz notes.txt report.txttar -tzf notes.tar.gzThe z option selects gzip in GNU tar. Listing does not restore files.
Expected stdout:
notes.txtreport.txtCommon mistakes
Do not run plain gzip on the only copy of a file: by default successful compression can replace its input. Use -c and a distinct new output name. Shell redirection can truncate an existing target before a command runs.
Practice
Create a tar bundle of both fixture documents. Compress the bundle to a new file with gzip -c, list it with tar -tzf and explain which step bundles files and which compresses bytes.
Related references
Study: tar, gzip, gunzip. These are reference names, not deployed links.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Archive versus compression — Knowledge Check
Apply the lesson concepts to the controlled archive fixture.
Which operation bundles multiple files?
What does gzip -c preserve?
What is guaranteed by successful lossless decoding?
What does the z flag in tar -czf select?