Archives versus compression
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.
Before you start
Section titled “Before you start”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 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.
Recognize the format you receive
Section titled “Recognize the format you receive”| 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.
Compare an archive with its gzip copy
Section titled “Compare an archive with its gzip copy”Create and enter the exercise directory:
mkdir ~/lab/b15-formatscd ~/lab/b15-formatsmkdir projectUse your editor to write project/notes.txt with this text, ending in a newline:
Archive practiceWrite a second file in the editor:
status=readyThe recording uses printf to create the same files. Build the tar archive, then compress a copy:
tar -cf project.tar projectgzip -k project.tarwc -c project.tar project.tar.gzfile 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
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$ Steps (38)
- Type
mkdir ~/lab/b15-formats - Press Enter — Reserve a disposable directory for format comparisons
- Type
cd ~/lab/b15-formats - Press Enter — Work inside the reserved directory
- Type
mkdir project - Press Enter — Create the directory to package
- Type
printf 'Archive practice\n' > project/notes.txt - Press Enter — Write a short note for the archive
- Type
printf 'status=ready\n' > project/settings.txt - Press Enter — Add a second file so packaging has a purpose
- Type
tar -cf project.tar project - Press Enter — Create an uncompressed archive of both files
- Type
gzip -k project.tar - Press Enter — Compress a copy while keeping the tar archive
- Type
wc -c project.tar project.tar.gz - Press Enter — Compare byte counts for these particular inputs
- Output:
10240 project.tar 227 project.tar.gz 10467 total - Type
file project.tar project.tar.gz - Press Enter — Inspect the formats rather than trusting their suffixes
- 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 - Type
rm project.tar - Press Enter — Free the project.tar name so gunzip can restore it without an overwrite question
- Type
gunzip project.tar.gz - Press Enter — Decompress and replace the gzip file with its tar file
- Type
tar -tf project.tar - Press Enter — Confirm that decompression left an archive, not extracted files
- Output:
project/ project/notes.txt project/settings.txt - Type
rm project.tar project/notes.txt project/settings.txt - Press Enter — Remove only the files created for this exercise
- Type
rmdir project - Press Enter — Remove the empty source directory
- Type
cd ~/lab - Press Enter — Leave the directory before removing it
- Type
rmdir ~/lab/b15-formats - Press Enter — Remove the empty exercise directory
- Type
test ! -e ~/lab/b15-formats && printf 'Reset complete\n' - Press Enter — Confirm the exercise left no directory
- 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.
Reset this lesson
Section titled “Reset this lesson”The replay ends with the following reset. These paths all belong to this disposable exercise:
rm project.tar project/notes.txt project/settings.txtrmdir projectcd ~/labrmdir ~/lab/b15-formatstest ! -e ~/lab/b15-formats && printf 'Reset complete\n'Mistakes to recognize
Section titled “Mistakes to recognize”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.
Try it yourself
Section titled “Try it yourself”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.
Further reading
Section titled “Further reading”- On Ubuntu:
man tar,man gzip, andman file. - GNU tar manual.
- GNU gzip manual.