Archives and Compression

Welcome back.
In the previous lesson, you learned how to check network connections from the terminal.
Now we return to files.
But this time, we are going to pack them.
Because sometimes you do not want to send ten files separately like a confused pigeon with documents.
You want one archive.
Clean. Portable. Organized.
Very adult.
What You’ll Learn
In this lesson, you’ll learn how to:
- create archives with
tar; - extract
.tarfiles; - create compressed
.tar.gzarchives; - extract
.tar.gzarchives; - create
.zipfiles; - extract
.zipfiles; - understand the difference between archiving and compression.
The Mission
Your mission is simple:
Create a small folder with files, pack it into archives, compress it, extract it again, and understand what is happening.
No magic.
Just files wearing a suitcase.
Archive vs Compression
An archive collects multiple files into one file.
Compression makes files smaller.
They are not exactly the same thing.
Example:
.tar archive only
.tar.gz archive + compression
.zip archive + compression
A .tar file is like putting many documents into one folder.
A .tar.gz file is like putting that folder into a vacuum bag.
Linux loves this.
Probably too much.
Create a Practice Folder
Go to your practice folder:
cd ~/terminal-practice
If it does not exist, create it:
mkdir -p ~/terminal-practice
cd ~/terminal-practice
Create a folder for this lesson:
mkdir lesson9-archive
cd lesson9-archive
Create some files:
echo "Linux archive practice" > notes.txt
echo "Remember to extract carefully" > reminder.txt
echo "Compression is useful" > compression.txt
Check:
ls
You should see:
compression.txt notes.txt reminder.txt
Good.
Now we have something to pack.
Create a tar Archive
Go one level up:
cd ..
Create a .tar archive:
tar -cf lesson9-archive.tar lesson9-archive
Now list files:
ls
You should see:
lesson9-archive
lesson9-archive.tar
The archive was created.
The options mean:
-c = create
-f = file
So this command means:
Create an archive file called
lesson9-archive.tarfrom the folderlesson9-archive.
Very logical.
Very Linux.
See What Is Inside a tar Archive
To list the contents of a .tar archive:
tar -tf lesson9-archive.tar
The options mean:
-t = list archive contents
-f = file
You should see files inside the archive.
This is useful before extracting.
Because extracting unknown archives blindly is how your clean folder becomes a digital soup.
Extract a tar Archive
Create a folder for extraction:
mkdir extracted-tar
Extract the archive there:
tar -xf lesson9-archive.tar -C extracted-tar
The options mean:
-x = extract
-f = file
-C = extract into this directory
Now check:
tree extracted-tar
If tree is not installed, use:
ls -R extracted-tar
You should see your files again.
The archive worked.
Nothing exploded.
A beautiful day.
Create a Compressed tar.gz Archive
Now create a compressed archive:
tar -czf lesson9-archive.tar.gz lesson9-archive
The options mean:
-c = create
-z = gzip compression
-f = file
.tar.gz is very common on Linux.
You may also see .tgz.
That is basically the same idea, just shorter.
Because apparently even file extensions need nicknames.
Extract a tar.gz Archive
Create another extraction folder:
mkdir extracted-targz
Extract:
tar -xzf lesson9-archive.tar.gz -C extracted-targz
Options:
-x = extract
-z = gzip
-f = file
-C = target directory
Check:
ls -R extracted-targz
You should see the extracted folder and files.
Create a zip Archive
zip is common when sharing files with people on Windows, macOS, or anyone who thinks .tar.gz looks like a medical diagnosis.
Create a zip archive:
zip -r lesson9-archive.zip lesson9-archive
The -r option means recursive.
It includes files inside the folder.
If zip is not installed, install it.
Arch Linux
sudo pacman -S zip unzip
Ubuntu or Debian
sudo apt install zip unzip
Fedora
sudo dnf install zip unzip
Extract a zip Archive
Create a folder:
mkdir extracted-zip
Extract the zip:
unzip lesson9-archive.zip -d extracted-zip
The -d option means destination directory.
Check:
ls -R extracted-zip
Your files should be there.
Common Archive Commands
Here are the commands you will use often:
tar -cf archive.tar folder
tar -tf archive.tar
tar -xf archive.tar
tar -czf archive.tar.gz folder
tar -xzf archive.tar.gz
zip -r archive.zip folder
unzip archive.zip
These are enough for most everyday tasks.
You do not need to memorize everything immediately.
Your brain deserves mercy.
Common Mistakes
Extracting in the wrong folder
Before extracting, check where you are:
pwd
Then list files:
ls
Extracting archives in random places is how your home folder becomes a garage sale.
Forgetting -r with zip
This may not include folder contents correctly:
zip archive.zip folder
Use:
zip -r archive.zip folder
The -r makes zip include everything inside.
Confusing create and extract
Create:
tar -czf archive.tar.gz folder
Extract:
tar -xzf archive.tar.gz
Small difference.
Big result.
One packs your files.
The other unpacks them.
Do not mix them up unless you enjoy confusion as a lifestyle.
Practice
Try this:
cd ~/terminal-practice
mkdir archive-practice
cd archive-practice
echo "File one" > one.txt
echo "File two" > two.txt
echo "File three" > three.txt
cd ..
tar -czf archive-practice.tar.gz archive-practice
mkdir test-extract
tar -xzf archive-practice.tar.gz -C test-extract
ls -R test-extract
Then answer:
- What does
tar -cfdo? - What does
tar -xfdo? - What does
-zmean? - What does
zip -rdo? - Why should you check where you are before extracting?
Mini Challenge
Create this folder:
backup-demo/
├── notes.txt
├── tasks.txt
└── config/
└── app.conf
Then:
- Create
backup-demo.tar. - List its contents.
- Create
backup-demo.tar.gz. - Create
backup-demo.zip. - Extract each archive into a separate folder.
- Check that all files are there.
No mouse.
The mouse is now just a decorative object.
Summary
Today you learned:
- archives collect files into one file;
- compression makes data smaller;
.taris usually archive only;.tar.gzis archive plus gzip compression;.zipis common for sharing;tar -cfcreates a tar archive;tar -xfextracts a tar archive;tar -czfcreates a compressed tar.gz archive;tar -xzfextracts a compressed tar.gz archive;zip -rcreates a zip archive;unzipextracts zip files.
Archives are everywhere in Linux.
Now when you see .tar.gz, you do not need to panic.
It is not a monster.
It is just a suitcase with a belt around it.
Next Lesson
In the next lesson, we’ll learn about environment variables, aliases, and shell basics.
That is where the terminal starts remembering things for you.
Like a helpful assistant.
But with fewer opinions.