Skip to content

Creating and inspecting archives

Module
Archives and Everyday Workflow
Difficulty
Easy
Updated
Tested on
Ubuntu 24.04.5 LTS (lab system), GNU Bash 5.2.21
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.

Finish Archives versus compression and its reset. Use ~/lab from the practice directory setup. Reserve the absent directory ~/lab/b15-restore for this exercise.

Prepare a small project:

Terminal window
mkdir ~/lab/b15-restore
cd ~/lab/b15-restore
mkdir project

Use your editor to create these two files, each with a final newline:

project/notes.txt
Sensor notes
project/settings.txt
sample=5

The recording writes the same contents with printf. From the directory containing project, create the archive:

Terminal window
tar -czf project.tar.gz project

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

List names first, then ask for details:

Terminal window
tar -tf project.tar.gz
tar -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.

Make a clean destination rather than extracting over the source:

Terminal window
mkdir restored
tar -xf project.tar.gz -C restored
diff -r project restored/project
echo $?

-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
Create inspect extract
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--

Step-by-step replay of a verified session · Recorded on Ubuntu 24.04.5 LTS, GNU Bash 5.2.21. Create and inspect the archive, restore it under a clean destination, compare the trees, and remove the disposable copies.
Steps (42)
  1. Type mkdir ~/lab/b15-restore
  2. Press Enter — Reserve an isolated archive exercise
  3. Type cd ~/lab/b15-restore
  4. Press Enter — Keep source and extraction paths under one directory
  5. Type mkdir project
  6. Press Enter — Prepare the source project
  7. Type printf 'Sensor notes\n' > project/notes.txt
  8. Press Enter — Write the project note
  9. Type printf 'sample=5\n' > project/settings.txt
  10. Press Enter — Write the companion settings file
  11. Type tar -czf project.tar.gz project
  12. Press Enter — Create one gzip-compressed project archive
  13. Type tar -tf project.tar.gz
  14. Press Enter — Inspect member names before writing any extracted files
  15. Output: project/ project/notes.txt project/settings.txt
  16. Type tar -tvf project.tar.gz
  17. Press Enter — Read the stored modes and member names
  18. 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
  19. Type mkdir restored
  20. Press Enter — Create a separate empty extraction destination
  21. Type tar -xf project.tar.gz -C restored
  22. Press Enter — Extract under restored instead of the current directory
  23. Type diff -r project restored/project
  24. Press Enter — Compare both directory trees recursively
  25. Type echo $?
  26. Press Enter — Check that the immediately preceding comparison returned success
  27. Output: 0
  28. Type rm restored/project/notes.txt restored/project/settings.txt
  29. Press Enter — Remove the two disposable extracted files
  30. Type rmdir restored/project restored
  31. Press Enter — Remove empty extraction directories from the inside out
  32. Type rm project.tar.gz project/notes.txt project/settings.txt
  33. Press Enter — Remove the archive and its disposable source files
  34. Type rmdir project
  35. Press Enter — Remove the empty original project
  36. Type cd ~/lab
  37. Press Enter — Return to the baseline working directory
  38. Type rmdir ~/lab/b15-restore
  39. Press Enter — Remove the empty exercise directory
  40. Type test ! -e ~/lab/b15-restore && printf 'Reset complete\n'
  41. Press Enter — Verify the source, archive, and restored copy are gone
  42. 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.

The replay finishes by removing the extracted copy, the archive, and the original disposable project. No recursive deletion is needed:

Terminal window
rm restored/project/notes.txt restored/project/settings.txt
rmdir restored/project restored
rm project.tar.gz project/notes.txt project/settings.txt
rmdir project
cd ~/lab
rmdir ~/lab/b15-restore
test ! -e ~/lab/b15-restore && printf 'Reset complete\n'

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.

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.

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.