Skip to content

Archives versus compression

Module
Archives and Everyday Workflow
Difficulty
Easy
Updated
Tested on
Ubuntu 24.04.5 LTS (lab system), GNU Bash 5.2.21
On this page

You need to send a directory of notes as one file. A smaller file would help, but keeping the directory’s names and contents together is a separate job. By the end, you can choose a format for each job and explain what must happen to recover the files.

Finish the connectivity practice and its reset. Use ~/lab from the practice directory setup. Reserve ~/lab/b15-formats; it must not exist before you begin. The example uses only disposable text files.

Keep the files together, then reduce the bytes

Section titled “Keep the files together, then reduce the bytes”

An archive packages files and their names into one file. A tar archive can hold directory entries and metadata as well as file contents. Compression encodes data to take fewer bytes when the data allows it. It does not organize a directory for you.

Tar makes the archive first. Gzip can then compress that archive. In reverse, decompression restores the tar archive; extraction restores its members as separate files. GNU tar can combine these operations in one command, but they remain distinct operations.

Packaging and compression Tar packages several files into one archive, and gzip compresses that archive. To recover the files, decompress the archive, then extract it. Several files Tar archive Compressed archive Package Compress Decompress Extract
Packaging and compression Tar packages several files into one archive, and gzip compresses that archive. To recover the files, decompress the archive, then extract it. Several files Tar archive Compressed archive Package Compress Decompress Extract
Several files become a tar archive, then a compressed archive; decompress before extracting to recover the separate files.

Packaging may make a small project larger because the archive includes headers and padding. Compression is not guaranteed to save space, especially for data already compressed, such as JPEG images.

Suffix Meaning
.tar A tar archive, without a compression layer
.tar.gz or .tgz A tar archive compressed with gzip
.tar.xz A tar archive compressed with xz
.zip An archive format that supports compression of its members
.gz on one file Gzip-compressed bytes of that file; not a directory archive by itself
.tar.bz2 A tar archive compressed with bzip2

Zip and bzip2 are format references here; the exercise does not run their tools. A suffix is a naming convention, not proof of a file’s format. Use file to inspect a received file. Renaming an archive does not convert it.

Create and enter the exercise directory:

Terminal window
mkdir ~/lab/b15-formats
cd ~/lab/b15-formats
mkdir project

Use your editor to write project/notes.txt with this text, ending in a newline:

project/notes.txt
Archive practice

Write a second file in the editor:

project/settings.txt
status=ready

The recording uses printf to create the same files. Build the tar archive, then compress a copy:

Terminal window
tar -cf project.tar project
gzip -k project.tar
wc -c project.tar project.tar.gz
file project.tar project.tar.gz

-c creates a tar archive. -f takes the archive filename as its next argument; the final project is the source. Gzip’s -k keeps the original, so the two formats can be compared side by side. wc -c counts bytes.

Predict: after decompressing the gzip file, will the source files be extracted too?

Run it, then compare with your guess
Archive and compress
learner@rootvolt:~/lab$ mkdir ~/lab/b15-formatslearner@rootvolt:~/lab$ cd ~/lab/b15-formatslearner@rootvolt:~/lab/b15-formats$ mkdir projectlearner@rootvolt:~/lab/b15-formats$ printf 'Archive practice\n' > project/notes.txtlearner@rootvolt:~/lab/b15-formats$ printf 'status=ready\n' > project/settings.txtlearner@rootvolt:~/lab/b15-formats$ tar -cf project.tar projectlearner@rootvolt:~/lab/b15-formats$ gzip -k project.tarlearner@rootvolt:~/lab/b15-formats$ wc -c project.tar project.tar.gz10240 project.tar
  227 project.tar.gz
10467 totallearner@rootvolt:~/lab/b15-formats$ file project.tar project.tar.gzproject.tar:    POSIX tar archive (GNU)
project.tar.gz: gzip compressed data, was "project.tar", last modified: Sun Nov 15 17:05:33 2026, from Unix, original size modulo 2^32 10240learner@rootvolt:~/lab/b15-formats$ rm project.tarlearner@rootvolt:~/lab/b15-formats$ gunzip project.tar.gzlearner@rootvolt:~/lab/b15-formats$ tar -tf project.tarproject/
project/notes.txt
project/settings.txtlearner@rootvolt:~/lab/b15-formats$ rm project.tar project/notes.txt project/settings.txtlearner@rootvolt:~/lab/b15-formats$ rmdir projectlearner@rootvolt:~/lab/b15-formats$ cd ~/lablearner@rootvolt:~/lab$ rmdir ~/lab/b15-formatslearner@rootvolt:~/lab$ test ! -e ~/lab/b15-formats && printf 'Reset complete\n'Reset completelearner@rootvolt:~/lab$ 

Step-by-step replay of a verified session · Recorded on Ubuntu 24.04.5 LTS, GNU Bash 5.2.21. Create a tar archive, compress it, inspect both formats, decompress the gzip copy, and reset the disposable directory.
Steps (38)
  1. Type mkdir ~/lab/b15-formats
  2. Press Enter — Reserve a disposable directory for format comparisons
  3. Type cd ~/lab/b15-formats
  4. Press Enter — Work inside the reserved directory
  5. Type mkdir project
  6. Press Enter — Create the directory to package
  7. Type printf 'Archive practice\n' > project/notes.txt
  8. Press Enter — Write a short note for the archive
  9. Type printf 'status=ready\n' > project/settings.txt
  10. Press Enter — Add a second file so packaging has a purpose
  11. Type tar -cf project.tar project
  12. Press Enter — Create an uncompressed archive of both files
  13. Type gzip -k project.tar
  14. Press Enter — Compress a copy while keeping the tar archive
  15. Type wc -c project.tar project.tar.gz
  16. Press Enter — Compare byte counts for these particular inputs
  17. Output: 10240 project.tar 227 project.tar.gz 10467 total
  18. Type file project.tar project.tar.gz
  19. Press Enter — Inspect the formats rather than trusting their suffixes
  20. Output: project.tar: POSIX tar archive (GNU) project.tar.gz: gzip compressed data, was "project.tar", last modified: Sun Nov 15 17:05:33 2026, from Unix, original size modulo 2^32 10240
  21. Type rm project.tar
  22. Press Enter — Free the project.tar name so gunzip can restore it without an overwrite question
  23. Type gunzip project.tar.gz
  24. Press Enter — Decompress and replace the gzip file with its tar file
  25. Type tar -tf project.tar
  26. Press Enter — Confirm that decompression left an archive, not extracted files
  27. Output: project/ project/notes.txt project/settings.txt
  28. Type rm project.tar project/notes.txt project/settings.txt
  29. Press Enter — Remove only the files created for this exercise
  30. Type rmdir project
  31. Press Enter — Remove the empty source directory
  32. Type cd ~/lab
  33. Press Enter — Leave the directory before removing it
  34. Type rmdir ~/lab/b15-formats
  35. Press Enter — Remove the empty exercise directory
  36. Type test ! -e ~/lab/b15-formats && printf 'Reset complete\n'
  37. Press Enter — Confirm the exercise left no directory
  38. Output: Reset complete

No. The replay removes the retained tar first so gunzip can use that name. Gunzip restores project.tar and removes project.tar.gz. Tar’s listing still shows members inside an archive. No extraction has happened.

The date reported by file is from this recording; your date will differ.

File Captured byte count
project.tar 10240
project.tar.gz 227

The tar archive in the replay is larger than the gzip copy. The tar size stays the same for these inputs, but the gzip size can differ by a byte or two between runs, because the compressed data includes the tar archive’s timestamps. These byte counts describe these inputs, not a promise about other projects. File identifies one as a tar archive and the other as gzip-compressed data.

The replay ends with the following reset. These paths all belong to this disposable exercise:

Terminal window
rm project.tar project/notes.txt project/settings.txt
rmdir project
cd ~/lab
rmdir ~/lab/b15-formats
test ! -e ~/lab/b15-formats && printf 'Reset complete\n'

Renaming instead of converting. Giving a tar file a .zip suffix leaves its bytes in tar format. Inspect it with file.

Decompressing onto a retained original. Gzip’s keep option leaves project.tar in place. Remove only that disposable retained copy before gunzip, as the recording does. Do not overwrite an unrelated file.

Goal: Choose filenames for one uncompressed archive of a directory and one gzip-compressed copy. Explain the recovery order without running more commands.

Hint

The suffixes describe two layers.

Solution

Use notes.tar and notes.tar.gz. Decompress the gzip layer to recover notes.tar, then extract the archive to recover its members. Compressing notes.txt directly would produce one compressed file, without packaging neighboring files.

  • Tar packages names, contents, and metadata.
  • Compression aims to reduce byte counts.
  • A compressed tar archive has two layers to reverse.

Next, Creating and inspecting archives restores a project into a separate directory.