Learn › Linux Foundations › Editing and archives

Chapter 19, Part 2. Tar archives and recovery

Create, inspect and extract plain tar archives, then compare restored content.

Chapter 19, Part 2. Tar archives and recovery

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.

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.5 Inspect the source tree

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.

19.5 Create a plain tar archive

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.

19.5 List without extracting

-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.

19.5 Make a fresh destination

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.

19.5 Extract the archive

-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.

19.5 Compare restored bytes

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.

19.6 Package one chosen file

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.

19.6 Inspect the selected member

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.

19.6 Prepare a selected-member destination

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.

19.6 Extract one member from the complete archive

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.

Summary

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.

Review all key topics

Key topicWhat it isSection
ArchiveA file holding named members19.5
MemberAn entry inside an archive19.5
DestinationWhere extraction writes files19.5
SelectionUse the stored name to extract one member19.6

Define key terms

TermDefinition
ArchiveA file holding named members
MemberAn entry inside an archive
DestinationWhere extraction writes files
SelectionUse the stored name to extract one member

Review question 1 of 5

Review question 2 of 5

Review question 3 of 5

Review question 4 of 5

Review question 5 of 5

Next part

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.

More lessons in Editing and archives