Creating and extracting tar archives
Quick answer
Use tar to package a directory or selected files, inspect the member list, then restore into a fresh directory. This three-step workflow helps prevent accidental overwrites and makes it possible to verify the result.
Prepare the practice environment
Run python3 lab-fixtures/archives/setup.py as an ordinary user from the extracted package root in a disposable Linux environment. Enter the printed directory, check pwd, then run export LC_ALL=C; umask 077; set -o pipefail. Use a fresh fixture for each example and stop if a command fails unexpectedly. All archive and output paths must be new. Outputs below are not commands.
How it works
The operation flags c, t and x mean create, list and extract; choose one per invocation. The f option names the archive file; -C DIRECTORY changes tar’s working location at that point in the command. During creation, relative member names are useful because they can be restored under another directory. During extraction, put -C before any member operands and name an existing empty destination. tar stores metadata as well as file contents, but restored ownership, modes and timestamps depend on format, options, privileges and filesystem. GNU tar --no-same-owner avoids requesting stored ownership, and --no-same-permissions applies normal permission masking. They are not security sandboxes. A listing is inspection, not proof of safety; use only the trusted synthetic archives in this lesson.
In the project example, -C project . makes the archive contain the contents relative to project, including the ./ directory entry. To select a single member later, use its actual stored name from the listing; ./data.csv and an external path such as project/data.csv are not the same archive name. A successful list is not a successful restore. In a trusted fixture, verify both the extraction status and the restored contents; if preserving metadata is part of the task, inspect it separately.
Try the examples
tar -cf project.tar -C project .tar -tf project.tar | sortThe archive contains relative paths beginning at the project directory, not the outer path. sort makes the listing deterministic.
Expected stdout:
././data.csv./readme.txttar -cf notes.tar notes.txt report.txttar -tf notes.tarmkdir restoredtar -xf notes.tar --no-same-owner --no-same-permissions -C restoredcmp notes.txt restored/notes.txt && cmp report.txt restored/report.txt && printf "restored bytes match\n"cmp compares bytes and is silent on equality. Both originals must match their restored copies.
Expected stdout:
notes.txtreport.txtrestored bytes matchtar -czf notes.tar.gz notes.txt report.txttar -tzf notes.tar.gzmkdir selectedtar -xzf notes.tar.gz -C selected report.txtls -1 selectedThe first two lines are the archive listing; the final line is the destination listing. Only report.txt was selected.
Expected stdout:
notes.txtreport.txtreport.txtCommon mistakes
Do not extract over an existing directory of important files. Do not use absolute-name or overwrite options to silence warnings. Create a fresh private destination; never extract an unknown archive with sudo.
Practice
Bundle project using -C, list its contents, then restore into a fresh directory. Compare both restored files with cmp and explain why the relative paths are useful.
Related references
Study: tar. These are reference names, not deployed links.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Creating and extracting tar archives — Knowledge Check
Apply the lesson concepts to the controlled archive fixture.
Which operation lists tar members?
What does -f specify?
Where should a trusted archive be extracted in this lab?
What does cmp success establish?