Create and Extract Linux Archives with tar: A Beginner Guide
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
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.
Make a New Practice Folder
Start Heremkdir creates a folder. cd enters it, and pwd prints your current location. && runs the next command only when the previous one succeeds.
mkdir "$HOME/archive-practice" && cd "$HOME/archive-practice" && pwdβ― What to expect
/home/sam/archive-practiceCreate Two Small Files
Practicemkdir 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.
mkdir notesprintf 'My packing list.\n' > notes/list.txtprintf 'Ready to travel.\n' > notes/status.txtcat notes/list.txt notes/status.txtβ― What to expect
My packing list.Ready to travel.Create a Compressed Archive
Practicetar 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.
tar -czf notes.tar.gz notesls -lh notes.tar.gzβ― What to expect
A listing for notes.tar.gz.Its exact size, date, and owner will vary.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.
tar -tzf notes.tar.gzβ― What to expect
notes/notes/list.txtnotes/status.txtThe two file entries can appear in a different order.Extract Into a Separate Folder
Practicemkdir 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.
mkdir restoredtar -xzf notes.tar.gz -C restoredls restored/notesβ― What to expect
list.txt status.txtRead and Compare the Restored Files
Practicecat 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.
cat restored/notes/list.txt restored/notes/status.txtdiff -r notes restored/notes && printf 'Restored files match.\n'β― What to expect
My packing list.Ready to travel.Restored files match.Remove Only the Practice Files
CleanupFirst 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.
ls -la "$HOME/archive-practice"rm notes/list.txt notes/status.txt restored/notes/list.txt restored/notes/status.txt notes.tar.gzrmdir notes restored/notes restoredcd "$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
Your feedback keeps these production procedures accurate and battle-tested.