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
Prepare Sample Files in a Lab Folder
Practice DataCreate 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.
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
Pack the Lab Folder into a tar.gz File
Create ArchiveIn 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.
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.gzStep 3: List Archive Contents Before Extracting
Inspect the Archive File List
Review ContentsUse -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.
tar -tzf "$ARCHIVE"❯ View Expected Console Output
././reports/./reports/status.txt./README.txtStep 4: Extract into a New, Empty Directory
Restore the Archive to a Fresh Location
Extract SafelyUse 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.
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.txtStep 5: Compare the Restored Files with the Originals
Verify the Extracted Directory Tree
Check the RestoreA 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.
diff -r "$LAB" "$RESTORE_DIR"echo "diff exit status: $?"❯ View Expected Console Output
diff exit status: 0