Skip to content

Create and Extract Linux Archives with tar: A Beginner Guide

Admin
October 19, 2026
9 min read
Beginner
πŸ’» Target OS: Ubuntu 24.04+ / Debian 12+ / RHEL 9+

Before you begin

  • Applies to:Linux with Bash, GNU tar, gzip, and diff.
  • Where to start:Open one Bash terminal and keep it open throughout the exercise.
  • What you need:Use a regular account with access to its home directory. You do not need sudo.
Guide tools
Guide tools
0/0 Steps (0%)

An archive puts multiple files into one file, making them easier to copy or share. Compression reduces the space they take. A .tar.gz file combines both: tar gathers the files, and gzip compresses the result.

You will create two text files, pack them into an archive, extract a second copy, and compare the copies. You only need a Linux terminal and basic familiarity with copying commands.

Small practice folder β†’ One tar.gz file β†’ Fresh destination β†’ Verify the restore

Small practice folder β†’ One tar.gz file β†’ Fresh destination β†’ Verify the restore.

01

Make a New Practice Folder

Start Here

mkdir creates a folder. cd enters it, and pwd prints your current location. && runs the next command only when the previous one succeeds.

Terminal window
mkdir "$HOME/archive-practice" && cd "$HOME/archive-practice" && pwd
❯ What to expect
/home/sam/archive-practice
02

Create Two Small Files

Practice

mkdir notes creates a subfolder. printf writes text, \n adds a new line, and > puts the text into a file. > replaces an existing file, so use these names only in the new practice folder.

Terminal window
mkdir notes
printf 'My packing list.\n' > notes/list.txt
printf 'Ready to travel.\n' > notes/status.txt
cat notes/list.txt notes/status.txt
❯ What to expect
My packing list.
Ready to travel.
03

Create a Compressed Archive

Practice

tar gathers the notes folder into one file. In -czf, c means create, z means gzip compression, and f means the next argument is the archive filename. The source folder comes last. The archive sits beside notes, so it does not include itself.

Terminal window
tar -czf notes.tar.gz notes
ls -lh notes.tar.gz
❯ What to expect
A listing for notes.tar.gz.
Its exact size, date, and owner will vary.
04

Look Inside Before Extracting

Practice

-t lists archive contents without extracting them. Keep -z for gzip and -f for the filename. You should see only the practice folder and two files. Listing an archive does not modify the original files.

Terminal window
tar -tzf notes.tar.gz
❯ What to expect
notes/
notes/list.txt
notes/status.txt
The two file entries can appear in a different order.
05

Extract Into a Separate Folder

Practice

mkdir restored creates an empty destination. Stop if it already exists. In the extraction command, x means extract, and -C restored tells tar where to put the files. The original folder remains in place.

Terminal window
mkdir restored
tar -xzf notes.tar.gz -C restored
ls restored/notes
❯ What to expect
list.txt status.txt
06

Read and Compare the Restored Files

Practice

cat displays the restored text. diff -r compares the original folder with the restored folder, including files inside subfolders. A successful comparison prints nothing. The last command prints a success message only if the folders match.

Terminal window
cat restored/notes/list.txt restored/notes/status.txt
diff -r notes restored/notes && printf 'Restored files match.\n'
❯ What to expect
My packing list.
Ready to travel.
Restored files match.
07

Remove Only the Practice Files

Cleanup

First list the folder and confirm it contains only this exercise. rm removes the named files; rmdir removes only empty folders. The final command goes back home before removing the now-empty practice folder. Stop if a command reports an unexpected error.

Terminal window
ls -la "$HOME/archive-practice"
rm notes/list.txt notes/status.txt restored/notes/list.txt restored/notes/status.txt notes.tar.gz
rmdir notes restored/notes restored
cd "$HOME" && rmdir archive-practice && printf 'Practice folder removed.\n'
❯ What to expect
A listing of the practice folder, then:
Practice folder removed.

References and next steps

Comments