Learn › Linux Foundations › Editing and archives
Create, inspect and extract plain tar archives, then compare restored content.
This chapter covers the following topics:
This is Part 2 of 3. It covers Sections 19.5 and 19.6.
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.
>>> Member paths and the destination determine what gets restored. Section 19.5.
>>> Plain tar packages named members. Section 19.7 adds a compression format.
>>> Check the resulting paths and compare bytes. Sections 19.5 and 19.9.
>>> An archive stores named members. Section 19.5.
>>> Stored names select entries. Section 19.6.
An archive stores named files together. A member is an entry inside an archive. First inspect this prepared source tree. ls -R lists it recursively, including directories below the selected path. All following paths are rooted in your home directory.
ls -R ~/editing-archives/practice/source
prompt: admin@server1:~$ answer: ls -R ~/editing-archives/practice/source output: /home/admin/editing-archives/practice/source: docs site
/home/admin/editing-archives/practice/source/docs: notice.txt readme.txt
/home/admin/editing-archives/practice/source/site: about.html index.html hint: Section 19.5. Use the shown command form and the exact target ~/editing-archives/practice/source.
The site has an index and an about page. The docs directory is a separate tree. Choose the intended source before packaging it.
Plain tar packages files; it does not apply compression. -c creates, -f names the archive in the next argument, and -C changes the directory tar uses to find the following source name. The output path is outside the source tree. It is new among the prepared lab files: creating an archive can replace an existing archive at that path.
tar -cf ~/editing-archives/practice/out/site.tar -C ~/editing-archives/practice/source site
prompt: admin@server1:~$ answer: tar -cf ~/editing-archives/practice/out/site.tar -C ~/editing-archives/practice/source site output:
hint: Section 19.5. Use the shown command form and the exact target site.
The source stays in place. The archive contains relative member names beneath site/, not your complete home-directory path.
-t lists archive members; -f still identifies the archive. Listing lets you inspect the names without restoring files. Check that they belong to the intended site tree. Names alone are not proof that an arbitrary downloaded archive is trustworthy.
tar -tf ~/editing-archives/practice/out/site.tar
prompt: admin@server1:~$ answer: tar -tf ~/editing-archives/practice/out/site.tar output: site/ site/index.html site/about.html hint: Section 19.5. Use the shown command form and the exact target ~/editing-archives/practice/out/site.tar.
Compare these members with the source tree. Creating an archive and verifying its intended contents are separate actions.
Extraction can overwrite an occupied destination. Create this new directory first. mkdir makes one directory; its parent already exists among the prepared lab files. If the destination already exists, stop and inspect it instead of deleting unknown data.
mkdir ~/editing-archives/practice/restore/site-plain
prompt: admin@server1:~$ answer: mkdir ~/editing-archives/practice/restore/site-plain output:
hint: Section 19.5. Use the shown command form and the exact target ~/editing-archives/practice/restore/site-plain.
The destination now exists separately from the source. No files have been restored yet.
-x extracts. Here -C selects the existing destination directory, and the member names determine paths beneath it. Run as your ordinary account. No sudo is needed for this prepared archive.
tar -xf ~/editing-archives/practice/out/site.tar -C ~/editing-archives/practice/restore/site-plain
prompt: admin@server1:~$ answer: tar -xf ~/editing-archives/practice/out/site.tar -C ~/editing-archives/practice/restore/site-plain output:
hint: Section 19.5. Use the shown command form and the exact target ~/editing-archives/practice/restore/site-plain.
The site directory should now be beneath restore/site-plain. The source tree remains unchanged.
cmp compares two files byte by byte. The first path is the source index; the second is its restored copy. With an identical pair, cmp exits successfully without printing a difference. A difference or missing file is a reason to inspect the selected source and destination.
cmp ~/editing-archives/practice/source/site/index.html ~/editing-archives/practice/restore/site-plain/site/index.html
prompt: admin@server1:~$ answer: cmp ~/editing-archives/practice/source/site/index.html ~/editing-archives/practice/restore/site-plain/site/index.html output:
hint: Section 19.5. Use the shown command form and the exact target ~/editing-archives/practice/restore/site-plain/site/index.html.
Optional, unscored check: repeat the comparison for about.html. The existence of an archive alone does not prove a successful recovery.
You can name a file rather than a whole directory. After -C selects the source directory, site/index.html is the chosen relative member name. This separate output must contain only the index file.
tar -cf ~/editing-archives/practice/out/index.tar -C ~/editing-archives/practice/source site/index.html
prompt: admin@server1:~$ answer: tar -cf ~/editing-archives/practice/out/index.tar -C ~/editing-archives/practice/source site/index.html output:
hint: Section 19.6. Use the shown command form and the exact target site/index.html.
Use this form when only one named file belongs in the package. It does not include the rest of the site.
List the new archive with the same -t operation. Check the exact stored name; you will use a stored member name when extracting only part of a larger archive.
tar -tf ~/editing-archives/practice/out/index.tar
prompt: admin@server1:~$ answer: tar -tf ~/editing-archives/practice/out/index.tar output: site/index.html hint: Section 19.6. Use the shown command form and the exact target ~/editing-archives/practice/out/index.tar.
The file name is relative to the source directory selected by -C. It is not just index.html.
Create another fresh destination for the next extraction. This keeps the one-file result separate from the complete site you already restored.
mkdir ~/editing-archives/practice/restore/index-only
prompt: admin@server1:~$ answer: mkdir ~/editing-archives/practice/restore/index-only output:
hint: Section 19.6. Use the shown command form and the exact target ~/editing-archives/practice/restore/index-only.
The destination is empty before extraction.
Use the complete site.tar archive, but name site/index.html after the options. Tar extracts that selected member rather than every member. The name must match the archive listing; it is not a path to the current source file.
tar -xf ~/editing-archives/practice/out/site.tar -C ~/editing-archives/practice/restore/index-only site/index.html
prompt: admin@server1:~$ answer: tar -xf ~/editing-archives/practice/out/site.tar -C ~/editing-archives/practice/restore/index-only site/index.html output:
hint: Section 19.6. Use the shown command form and the exact target site/index.html.
Only the selected file should be restored beneath this destination. Inspect the tree and compare its bytes with the source.
Tar packages named members. Use -c to create, -t to list and -x to extract. -f names the archive; -C selects the directory used for the following source or destination operation. Inspect member names, extract into a fresh destination and compare recovered bytes.
| Key topic | What it is | Section |
|---|---|---|
| Archive | A file holding named members | 19.5 |
| Member | An entry inside an archive | 19.5 |
| Destination | Where extraction writes files | 19.5 |
| Selection | Use the stored name to extract one member | 19.6 |
| Term | Definition |
|---|---|
| Archive | A file holding named members |
| Member | An entry inside an archive |
| Destination | Where extraction writes files |
| Selection | Use the stored name to extract one member |
>>> Extraction into an occupied path can overwrite files. Section 19.5.
>>> Use the stored member name. Section 19.6.
>>> Create, list and extract are separate operations. Section 19.5.
>>> Keep output separate from the source. Section 19.5.
>>> Verify the recovery separately. Section 19.5.
Continue to Part 3: Compression and ZIP recovery. Keep the practice files; the next part uses the same dedicated tree.
Practice Chapter 19, Part 2. Tar archives and recovery in a real Linux terminal at The Linux Camp. Progress is verified automatically as you type commands on the machine.