Creating and inspecting archives
On this page
You kept an archive of a project before moving its files. Before depending on that copy, you need to know what it contains and whether it restores correctly. By the end, you can inspect an archive and compare an isolated extraction with its source.
Before you start
Section titled “Before you start”Finish Archives versus compression and its reset. Use ~/lab from the practice directory setup. Reserve the absent directory ~/lab/b15-restore for this exercise.
Choose the archive’s starting directory
Section titled “Choose the archive’s starting directory”Prepare a small project:
mkdir ~/lab/b15-restorecd ~/lab/b15-restoremkdir projectUse your editor to create these two files, each with a final newline:
Sensor notessample=5The recording writes the same contents with printf. From the directory containing project, create the archive:
tar -czf project.tar.gz projectThe options combine three jobs: -c creates, -z compresses with gzip, and -f takes the archive filename. The last argument is the source directory. Keeping the archive outside project prevents it from being included in its own input.
The relative source path means member names begin with project/. That top directory becomes part of the extraction result. Choosing the working directory before archiving makes the stored paths predictable.
Read the members before extraction
Section titled “Read the members before extraction”List names first, then ask for details:
tar -tf project.tar.gztar -tvf project.tar.gz-t lists members without extracting them. Adding -v shows metadata, including modes, ownership, size, and modification date. GNU tar recognizes gzip compression while reading this archive, so these reading commands do not need -z.
The recording’s detailed listing is wide. These are its modes and names in a narrower form:
| Member name | Stored mode |
|---|---|
project/ |
drwxr-xr-x |
project/notes.txt |
-rw-r--r-- |
project/settings.txt |
-rw-r--r-- |
Your listing dates will differ. The member names and modes are the useful clues here: one directory holds two regular files. The dates describe when the source was written in this capture.
Restore into a separate directory
Section titled “Restore into a separate directory”Make a clean destination rather than extracting over the source:
mkdir restoredtar -xf project.tar.gz -C restoreddiff -r project restored/projectecho $?-x extracts. -C restored changes tar’s extraction directory; it does not change your shell’s working directory. The stored project/ prefix means the recovered files go under restored/project/.
Predict: will an equal extraction make diff print all the matching lines?
Run it, then compare with your guess
learner@rootvolt:~/lab$ mkdir ~/lab/b15-restorelearner@rootvolt:~/lab$ cd ~/lab/b15-restorelearner@rootvolt:~/lab/b15-restore$ mkdir projectlearner@rootvolt:~/lab/b15-restore$ printf 'Sensor notes\n' > project/notes.txtlearner@rootvolt:~/lab/b15-restore$ printf 'sample=5\n' > project/settings.txtlearner@rootvolt:~/lab/b15-restore$ tar -czf project.tar.gz projectlearner@rootvolt:~/lab/b15-restore$ tar -tf project.tar.gzproject/
project/notes.txt
project/settings.txtlearner@rootvolt:~/lab/b15-restore$ tar -tvf project.tar.gzdrwxr-xr-x learner/learner 0 2026-11-15 16:54 project/
-rw-r--r-- learner/learner 13 2026-11-15 16:54 project/notes.txt
-rw-r--r-- learner/learner 9 2026-11-15 16:54 project/settings.txtlearner@rootvolt:~/lab/b15-restore$ mkdir restoredlearner@rootvolt:~/lab/b15-restore$ tar -xf project.tar.gz -C restoredlearner@rootvolt:~/lab/b15-restore$ diff -r project restored/projectlearner@rootvolt:~/lab/b15-restore$ echo $?0learner@rootvolt:~/lab/b15-restore$ rm restored/project/notes.txt restored/project/settings.txtlearner@rootvolt:~/lab/b15-restore$ rmdir restored/project restoredlearner@rootvolt:~/lab/b15-restore$ rm project.tar.gz project/notes.txt project/settings.txtlearner@rootvolt:~/lab/b15-restore$ rmdir projectlearner@rootvolt:~/lab/b15-restore$ cd ~/lablearner@rootvolt:~/lab$ rmdir ~/lab/b15-restorelearner@rootvolt:~/lab$ test ! -e ~/lab/b15-restore && printf 'Reset complete\n'Reset completelearner@rootvolt:~/lab$ Files ~/lab
- ~
- lab/cwdno b15-restore here
- Documents/
- notes.txt-rw-r--r--
- report.txt-rw-r--r--
- projects/
- todo.txt-rw-r--r--
Steps (42)
- Type
mkdir ~/lab/b15-restore - Press Enter — Reserve an isolated archive exercise
- Type
cd ~/lab/b15-restore - Press Enter — Keep source and extraction paths under one directory
- Type
mkdir project - Press Enter — Prepare the source project
- Type
printf 'Sensor notes\n' > project/notes.txt - Press Enter — Write the project note
- Type
printf 'sample=5\n' > project/settings.txt - Press Enter — Write the companion settings file
- Type
tar -czf project.tar.gz project - Press Enter — Create one gzip-compressed project archive
- Type
tar -tf project.tar.gz - Press Enter — Inspect member names before writing any extracted files
- Output:
project/ project/notes.txt project/settings.txt - Type
tar -tvf project.tar.gz - Press Enter — Read the stored modes and member names
- Output:
drwxr-xr-x learner/learner 0 2026-11-15 16:54 project/ -rw-r--r-- learner/learner 13 2026-11-15 16:54 project/notes.txt -rw-r--r-- learner/learner 9 2026-11-15 16:54 project/settings.txt - Type
mkdir restored - Press Enter — Create a separate empty extraction destination
- Type
tar -xf project.tar.gz -C restored - Press Enter — Extract under restored instead of the current directory
- Type
diff -r project restored/project - Press Enter — Compare both directory trees recursively
- Type
echo $? - Press Enter — Check that the immediately preceding comparison returned success
- Output:
0 - Type
rm restored/project/notes.txt restored/project/settings.txt - Press Enter — Remove the two disposable extracted files
- Type
rmdir restored/project restored - Press Enter — Remove empty extraction directories from the inside out
- Type
rm project.tar.gz project/notes.txt project/settings.txt - Press Enter — Remove the archive and its disposable source files
- Type
rmdir project - Press Enter — Remove the empty original project
- Type
cd ~/lab - Press Enter — Return to the baseline working directory
- Type
rmdir ~/lab/b15-restore - Press Enter — Remove the empty exercise directory
- Type
test ! -e ~/lab/b15-restore && printf 'Reset complete\n' - Press Enter — Verify the source, archive, and restored copy are gone
- Output:
Reset complete
No. Diff prints no differences. Echo reads the immediately preceding command’s status, which is 0 here. With -r, diff visits the two directories recursively and compares corresponding files.
This checks the names and file contents in this small project. It is not a full test of ownership, permissions, or every metadata field. The Files panel also shows the preserved baseline lab files; only the reserved exercise directory changes.
Reset this lesson
Section titled “Reset this lesson”The replay finishes by removing the extracted copy, the archive, and the original disposable project. No recursive deletion is needed:
rm restored/project/notes.txt restored/project/settings.txtrmdir restored/project restoredrm project.tar.gz project/notes.txt project/settings.txtrmdir projectcd ~/labrmdir ~/lab/b15-restoretest ! -e ~/lab/b15-restore && printf 'Reset complete\n'Mistakes to recognize
Section titled “Mistakes to recognize”Looking for restored/notes.txt. The archive stores project/ as a member. The destination is restored/project/notes.txt.
Using the source as the destination. Extraction can overwrite existing files. Use the separate empty directory so the comparison still has an untouched original.
Security Angle
Section titled “Security Angle”An archive received from someone else can contain unexpected names or links. Inspect it before extraction, use a disposable destination, and do not extract it as root. A clean directory reduces accidental overwrites; it does not make an untrusted archive safe to execute.
Try it yourself
Section titled “Try it yourself”Goal: Identify the comparison paths if the extraction destination is unpacked instead of restored. Explain which name remains inside the archive. You do not need to create another copy.
Hint
The -C argument chooses the parent of the stored path.
Solution
Create an empty unpacked directory and extract with -C unpacked. Compare project with unpacked/project using diff -r. The archive’s project/ prefix remains unchanged. A different destination does not rename members.
- Create the archive outside the source directory.
- List names and metadata before extracting.
- Extract into an empty destination and compare against the original.
Next, Checksums and differences separates byte comparison from readable text changes.
Further reading
Section titled “Further reading”- On Ubuntu:
man tarandman diff. - GNU tar manual.
- GNU Diffutils manual.