Learn › Linux Foundations › Editing and archives

Chapter 19, Part 3. Compression and ZIP recovery

Final test: Chapter 19, Editing and archives. Twenty objectives on a real Debian 12 server covering all three parts: three edits with nano that keep the other lines, plain tar archives made and restored into fresh directories, one member restored alone, gzip and bzip2 archives and single files compressed and recovered, and ZIP files extracted with unzip. Graded from the files and archives you produce.

Chapter 19, Part 3. Compression and ZIP recovery

This chapter covers the following topics:

This is Part 3 of 3. It covers Sections 19.7 to 19.9.

What you already know: Chapters 2, 5 and 6 introduced paths, reading and copying files. Chapter 11 taught output redirection with >; Part 3 reuses it to keep a source file while writing compressed output to a named file. Every new option is explained before its command.

Objectives

These examples use a dedicated ~/editing-archives/practice tree. Card output is recorded; inspect your live terminal and files. Its prompt may look different. Type only the command, not a shown prompt. Tab can complete a path after you type its beginning. The assessment starts a separate fresh machine and checks its results automatically.

Pre-check 1 of 5

Pre-check 2 of 5

Pre-check 3 of 5

Pre-check 4 of 5

Pre-check 5 of 5

19.7 Create a gzip-compressed archive

Compression changes the representation of bytes and can reduce their size. It is not encryption, and some small or already compressed data gets larger. -z asks tar to use gzip. Keep -c to create, -f before the output pathname, and -C before the source directory.

tar -czf ~/editing-archives/practice/out/site.tar.gz -C ~/editing-archives/practice/source site

prompt: admin@server1:~$ answer: tar -czf ~/editing-archives/practice/out/site.tar.gz -C ~/editing-archives/practice/source site output:

hint: Section 19.7. Use the shown command form and the exact target site.

The .tar.gz name describes an archive compressed with gzip. Renaming a file to this extension would not compress it.

19.7 Inspect the compressed archive

Change the tar operation to -t to list. The -z option selects gzip handling and -f names the compressed archive.

tar -tzf ~/editing-archives/practice/out/site.tar.gz

prompt: admin@server1:~$ answer: tar -tzf ~/editing-archives/practice/out/site.tar.gz output: site/ site/index.html site/about.html hint: Section 19.7. Use the shown command form and the exact target ~/editing-archives/practice/out/site.tar.gz.

Its member paths should describe the same site tree as the plain archive. The compressed bytes do not need to match a fixed hash.

19.7 Prepare a gzip archive destination

Create a new extraction directory. Do not reuse the destination from the plain archive, since that would hide whether this recovery worked.

mkdir ~/editing-archives/practice/restore/site-gzip

prompt: admin@server1:~$ answer: mkdir ~/editing-archives/practice/restore/site-gzip output:

hint: Section 19.7. Use the shown command form and the exact target ~/editing-archives/practice/restore/site-gzip.

The next command extracts into this empty directory.

19.7 Restore the gzip archive

-x extracts and -z decompresses gzip. The remaining path arguments have the same meanings as the plain tar extraction.

tar -xzf ~/editing-archives/practice/out/site.tar.gz -C ~/editing-archives/practice/restore/site-gzip

prompt: admin@server1:~$ answer: tar -xzf ~/editing-archives/practice/out/site.tar.gz -C ~/editing-archives/practice/restore/site-gzip output:

hint: Section 19.7. Use the shown command form and the exact target ~/editing-archives/practice/restore/site-gzip.

Optional, unscored check: compare the restored site files with the source using the cmp pattern from Section 19.5. Compression changes storage, not the intended recovered file contents.

19.7 Use bzip2 for an archive

Bzip2 is a different compression format. Tar selects it with -j instead of -z. Create a compressed archive of the docs directory. Do not assume one format always yields the smaller file; that depends on data and options.

tar -cjf ~/editing-archives/practice/out/docs.tar.bz2 -C ~/editing-archives/practice/source docs

prompt: admin@server1:~$ answer: tar -cjf ~/editing-archives/practice/out/docs.tar.bz2 -C ~/editing-archives/practice/source docs output:

hint: Section 19.7. Use the shown command form and the exact target docs.

This contains named documentation members in a tar archive, compressed using bzip2.

19.7 List bzip2 archive members

Use -t for listing and keep -j for bzip2. Read the member paths before restoring them.

tar -tjf ~/editing-archives/practice/out/docs.tar.bz2

prompt: admin@server1:~$ answer: tar -tjf ~/editing-archives/practice/out/docs.tar.bz2 output: docs/ docs/notice.txt docs/readme.txt hint: Section 19.7. Use the shown command form and the exact target ~/editing-archives/practice/out/docs.tar.bz2.

The members belong to docs/, not site/. Choosing the right input matters more than memorizing the extension.

19.7 Prepare a bzip2 archive destination

Create a fresh destination for the documentation archive. Its parent is already prepared.

mkdir ~/editing-archives/practice/restore/docs-bzip2

prompt: admin@server1:~$ answer: mkdir ~/editing-archives/practice/restore/docs-bzip2 output:

hint: Section 19.7. Use the shown command form and the exact target ~/editing-archives/practice/restore/docs-bzip2.

Keep source and recovery locations separate.

19.7 Restore the bzip2 archive

Use -xj for extracting through bzip2 decompression. -f names the archive and -C names the existing destination.

tar -xjf ~/editing-archives/practice/out/docs.tar.bz2 -C ~/editing-archives/practice/restore/docs-bzip2

prompt: admin@server1:~$ answer: tar -xjf ~/editing-archives/practice/out/docs.tar.bz2 -C ~/editing-archives/practice/restore/docs-bzip2 output:

hint: Section 19.7. Use the shown command form and the exact target ~/editing-archives/practice/restore/docs-bzip2.

Optional, unscored check: compare the restored documentation with its source using the earlier tools.

19.8 Save command output to a file

Standard output is the normal result a command writes. It usually appears in the terminal. The shell can send it to a file instead: the greater-than sign, followed by a pathname, is output redirection. This sign is handled by the shell; it is not an option passed to gzip.

A new target is created. An existing target is emptied before the command starts, even if that command later fails. Read both paths first and keep the output different from the input. The next examples use new files in the prepared output or restore directory.

19.8 Compress one file while keeping it

Gzip compresses data; it does not by itself preserve a directory tree as a tar archive does. -c sends the compressed bytes to standard output while retaining the source. The shell redirects those bytes with > into the new output file. The shell opens the output before gzip runs; > can truncate an existing target. This assigned output must be absent.

gzip -c ~/editing-archives/practice/source/docs/readme.txt > ~/editing-archives/practice/out/readme.txt.gz

prompt: admin@server1:~$ answer: gzip -c ~/editing-archives/practice/source/docs/readme.txt > ~/editing-archives/practice/out/readme.txt.gz output:

hint: Section 19.8. Use the shown command form and the exact target ~/editing-archives/practice/out/readme.txt.gz.

Keep the redirection so compressed bytes are saved in the assigned output file. The original document must remain.

19.8 Decompress into another file

-d decompresses and -c writes the recovered bytes to standard output. The shell writes a different destination file. Never redirect back onto the compressed input or a source you need to keep.

gzip -dc ~/editing-archives/practice/out/readme.txt.gz > ~/editing-archives/practice/restore/readme-gzip.txt

prompt: admin@server1:~$ answer: gzip -dc ~/editing-archives/practice/out/readme.txt.gz > ~/editing-archives/practice/restore/readme-gzip.txt output:

hint: Section 19.8. Use the shown command form and the exact target ~/editing-archives/practice/restore/readme-gzip.txt.

The restored text belongs in restore/. The compressed file and original source are both retained.

19.8 Verify the gzip round trip

Compare the restored README against the original. This checks the recovered bytes rather than assuming that a .gz filename means success.

cmp ~/editing-archives/practice/source/docs/readme.txt ~/editing-archives/practice/restore/readme-gzip.txt

prompt: admin@server1:~$ answer: cmp ~/editing-archives/practice/source/docs/readme.txt ~/editing-archives/practice/restore/readme-gzip.txt output:

hint: Section 19.8. Use the shown command form and the exact target ~/editing-archives/practice/restore/readme-gzip.txt.

If cmp reports a difference, inspect the two selected paths.

19.8 Compress with bzip2

Bzip2 also accepts -c to write compressed bytes to standard output and preserve its input. Use the separate notice file and a new .bz2 output. The redirection has the same overwrite consequence as before.

bzip2 -c ~/editing-archives/practice/source/docs/notice.txt > ~/editing-archives/practice/out/notice.txt.bz2

prompt: admin@server1:~$ answer: bzip2 -c ~/editing-archives/practice/source/docs/notice.txt > ~/editing-archives/practice/out/notice.txt.bz2 output:

hint: Section 19.8. Use the shown command form and the exact target ~/editing-archives/practice/out/notice.txt.bz2.

Use a bzip2 decoder for this format. A filename alone does not convert formats.

19.8 Recover the bzip2 file

-d decompresses and -c writes the recovered data to standard output. Keep this restored file separate from the source and compressed file.

bzip2 -dc ~/editing-archives/practice/out/notice.txt.bz2 > ~/editing-archives/practice/restore/notice-bzip2.txt

prompt: admin@server1:~$ answer: bzip2 -dc ~/editing-archives/practice/out/notice.txt.bz2 > ~/editing-archives/practice/restore/notice-bzip2.txt output:

hint: Section 19.8. Use the shown command form and the exact target ~/editing-archives/practice/restore/notice-bzip2.txt.

The original notice should still be present.

19.8 Verify the bzip2 round trip

Compare the two text files. Gzip and bzip2 have different compressed representations, but each successful round trip returns the original bytes.

cmp ~/editing-archives/practice/source/docs/notice.txt ~/editing-archives/practice/restore/notice-bzip2.txt

prompt: admin@server1:~$ answer: cmp ~/editing-archives/practice/source/docs/notice.txt ~/editing-archives/practice/restore/notice-bzip2.txt output:

hint: Section 19.8. Use the shown command form and the exact target ~/editing-archives/practice/restore/notice-bzip2.txt.

The task is to recover the intended content, not to reproduce a demonstration compressed-file hash.

19.9 Inspect a ZIP before extracting

ZIP is another format for named members, with optional compression. Use unzip for this prepared ZIP file, rather than treating it as a .gz or .bz2 file. -l lists entries without extracting. The prepared lab files include this archive, built from known source files.

unzip -l ~/editing-archives/practice/incoming/site.zip

prompt: admin@server1:~$ answer: unzip -l ~/editing-archives/practice/incoming/site.zip output: Archive: /home/admin/editing-archives/practice/incoming/site.zip Length Date Time Name --------- ---------- ----- ---- 24 2026-01-01 00:00 site/about.html 23 2026-01-01 00:00 site/index.html --------- ------- 47 2 files hint: Section 19.9. Use the shown command form and the exact target ~/editing-archives/practice/incoming/site.zip.

Inspect the member names. ZIP metadata handling varies between tools and platforms; this lesson checks the required regular-file contents.

19.9 Prepare a ZIP destination

Create a new directory before extraction. Keeping it empty avoids unexpected overwrite questions or mixing old and newly recovered files.

mkdir ~/editing-archives/practice/restore/site-zip

prompt: admin@server1:~$ answer: mkdir ~/editing-archives/practice/restore/site-zip output:

hint: Section 19.9. Use the shown command form and the exact target ~/editing-archives/practice/restore/site-zip.

If the destination exists, inspect it. Do not delete unknown files to make the exercise pass.

19.9 Extract the ZIP

-d names the destination for unzip. The members retain their relative paths beneath it. Run without sudo and without an overwrite-all option.

unzip ~/editing-archives/practice/incoming/site.zip -d ~/editing-archives/practice/restore/site-zip

prompt: admin@server1:~$ answer: unzip ~/editing-archives/practice/incoming/site.zip -d ~/editing-archives/practice/restore/site-zip output: Archive: /home/admin/editing-archives/practice/incoming/site.zip inflating: /home/admin/editing-archives/practice/restore/site-zip/site/about.html inflating: /home/admin/editing-archives/practice/restore/site-zip/site/index.html hint: Section 19.9. Use the shown command form and the exact target ~/editing-archives/practice/restore/site-zip.

Optional, unscored check: compare the index and about page with the source using cmp in separate commands.

19.9 Prepare a one-member ZIP destination

Create a separate destination when you need only one file from the ZIP. This makes the intended result easy to inspect.

mkdir ~/editing-archives/practice/restore/about-only

prompt: admin@server1:~$ answer: mkdir ~/editing-archives/practice/restore/about-only output:

hint: Section 19.9. Use the shown command form and the exact target ~/editing-archives/practice/restore/about-only.

No complete site tree should be copied into this destination.

19.9 Extract one named ZIP member

Place the exact member name after the archive path. Here it selects only site/about.html; -d still supplies the destination. Use the listing to discover member names instead of guessing them.

unzip ~/editing-archives/practice/incoming/site.zip site/about.html -d ~/editing-archives/practice/restore/about-only

prompt: admin@server1:~$ answer: unzip ~/editing-archives/practice/incoming/site.zip site/about.html -d ~/editing-archives/practice/restore/about-only output: Archive: /home/admin/editing-archives/practice/incoming/site.zip inflating: /home/admin/editing-archives/practice/restore/about-only/site/about.html hint: Section 19.9. Use the shown command form and the exact target ~/editing-archives/practice/restore/about-only.

Inspect this destination and compare its one file with the source. A named member is a path inside the archive.

Summary

A buffer is a working copy; saving writes it to a selected file. Archive members have stored names. Plain tar packages files, while gzip and bzip2 compress bytes. Extracting into a fresh destination separates recovered files from working files. A comparison checks recovery.

Review all key topics

Key topicWhat it isSection
Buffer and saveWorking copy versus file on disk19.1 to 19.4
Tar operationsCreate, list, extract and select a member19.5 to 19.6
CompressionGzip or bzip2 representation of bytes19.7 to 19.8
ZIP recoveryList members, select destination, extract and compare19.9

Define key terms

TermDefinition
BufferThe editor working copy
ArchiveA file holding named members
MemberOne stored entry in an archive
CompressionEncoding data in another representation that may require fewer bytes
DestinationWhere recovered files are written

Review question 1 of 5

Review question 2 of 5

Review question 3 of 5

Review question 4 of 5

Review qu

Practice Chapter 19, Part 3. Compression and ZIP recovery in a real Linux terminal at The Linux Camp. Progress is verified automatically as you type commands on the machine.

More lessons in Editing and archives