Learn › Linux Foundations › Editing and archives
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.
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.
>>> Save writes the buffer to the selected file. Sections 19.1 and 19.2.
>>> Member paths and the destination determine what gets restored. Section 19.5.
>>> Plain tar packages named members. Section 19.7 adds a compression format.
>>> Choose a fresh output path for the compression exercises. Section 19.8.
>>> Check the resulting paths and compare bytes. Sections 19.5 and 19.9.
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.
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.
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.
-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.
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.
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.
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.
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.
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.
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.
-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.
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.
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.
-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.
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.
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.
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.
-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.
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.
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.
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.
| Key topic | What it is | Section |
|---|---|---|
| Buffer and save | Working copy versus file on disk | 19.1 to 19.4 |
| Tar operations | Create, list, extract and select a member | 19.5 to 19.6 |
| Compression | Gzip or bzip2 representation of bytes | 19.7 to 19.8 |
| ZIP recovery | List members, select destination, extract and compare | 19.9 |
| Term | Definition |
|---|---|
| Buffer | The editor working copy |
| Archive | A file holding named members |
| Member | One stored entry in an archive |
| Compression | Encoding data in another representation that may require fewer bytes |
| Destination | Where recovered files are written |
>>> Discard leaves the prior saved content. Section 19.4.
>>> Extraction into an occupied path can overwrite files. Section 19.5.
>>> Use the stored member name. Section 19.6.
>>> The shell sends those bytes to the selected file while the original remains. Section 19.8.
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.