Skip to content

Create and Extract Linux Archives with tar

The tar command combines files and directories into one archive. With gzip compression, a .tar.gz or .tgz archive is easier to transfer and store. An archive is useful for packaging files, but by itself it is not a backup: keep important data in a separate, tested backup location.

The steps below create sample files under your home directory, store the archive beside the lab folder, inspect it, and extract it to a new temporary directory. Keep one shell open so the variables remain available.


Step 1: Create a Small Directory to Archive

01

Prepare Sample Files in a Lab Folder

Practice Data

Create two text files in a dedicated folder. The commands replace these sample files if you repeat the exercise. The archive will be created outside the lab directory so it cannot accidentally include itself.

Terminal window
LAB="$HOME/tar-practice"
ARCHIVE="$HOME/tar-practice.tar.gz"
mkdir -p "$LAB/reports"
printf 'Project notes\n' > "$LAB/README.txt"
printf 'Status: ready\n' > "$LAB/reports/status.txt"
❯ View Expected Console Output
The sample files are stored under ~/tar-practice.

Step 2: Create a Gzip-Compressed Archive

02

Pack the Lab Folder into a tar.gz File

Create Archive

In tar -czf, -c creates an archive, -z uses gzip compression, and -f names the archive file. -C changes the directory tar reads before it adds the following path. Using . includes the contents of the lab folder.

Terminal window
tar -czf "$ARCHIVE" -C "$LAB" .
ls -lh "$ARCHIVE"
❯ View Expected Console Output
-rw-r--r-- 1 sam sam 218 Oct 5 09:30 /home/sam/tar-practice.tar.gz

Step 3: List Archive Contents Before Extracting

03

Inspect the Archive File List

Review Contents

Use -t to list the archive without extracting it. Reviewing names first helps you spot unexpected absolute paths or parent-directory components. Do not unpack an archive from an untrusted source into a system directory.

Terminal window
tar -tzf "$ARCHIVE"
❯ View Expected Console Output
./
./reports/
./reports/status.txt
./README.txt

Step 4: Extract into a New, Empty Directory

04

Restore the Archive to a Fresh Location

Extract Safely

Use a newly created temporary directory as the extraction target so the files do not overwrite your original lab. The command uses -x to extract, -z for gzip, and -f for the archive path. Keep the printed restore path for the verification step.

Terminal window
RESTORE_DIR=$(mktemp -d "$HOME/tar-restore.XXXXXX")
tar -xzf "$ARCHIVE" -C "$RESTORE_DIR"
printf 'Restored to: %s\n' "$RESTORE_DIR"
find "$RESTORE_DIR" -type f -print
❯ View Expected Console Output
Restored to: /home/sam/tar-restore.a1b2c3
/home/sam/tar-restore.a1b2c3/reports/status.txt
/home/sam/tar-restore.a1b2c3/README.txt

Step 5: Compare the Restored Files with the Originals

05

Verify the Extracted Directory Tree

Check the Restore

A recursive diff with no output means it found no content differences. It returns an exit status of zero when the trees match. This verifies this practice restore; for real backups, follow your organization’s recovery test and retention process.

Terminal window
diff -r "$LAB" "$RESTORE_DIR"
echo "diff exit status: $?"
❯ View Expected Console Output
diff exit status: 0

References

Comments