Track 2 · Lesson 5 of 6
Archives and compression
Pack many files into one tar or zip archive, compress single files with gzip, and always list an archive before you extract it.
Sooner or later you need to move a whole folder as one file. Other times you only want a smaller download. That is what archives and compression are for. tar packs many files into one. gzip shrinks a single file. zip does both, in a format other devices understand. You will use all three.
What you'll be able to do
- Pack a folder into a compressed
.tar.gzarchive. - List an archive's contents without unpacking it.
- Extract an archive into the directory you choose.
- Compress and decompress a single file with
gzip. - Make and unpack a
.ziparchive that other apps can read.
Step 1: Create an archive with tar
tar collects files into one stream. In -czf, the c creates the archive and z compresses it with gzip. The f names the file to write. Build a small folder first.
mkdir -p project/notes
echo alpha > project/a.txt
echo beta > project/b.txt
echo gamma > project/notes/c.txt
tar -czf project.tar.gz project
A successful tar prints nothing. Check that the file exists.
ls -l project.tar.gz
-rw-r--r-- 1 u0_a123 u0_a123 214 Jan 30 10:12 project.tar.gz
The name ends in .tar.gz because it is a tar archive, then gzipped. You will also see .tgz, which means the same thing.
Run tar -czf project.tar.gz project and the archive stores the folder itself, so extraction rebuilds project/. Run it on project/* instead and you get a loose pile of files. Naming the folder keeps things tidy.
Step 2: List the contents before extracting
The -t flag lists an archive without unpacking it. Keep the -zf tail, so tar knows it is gzipped and where to read from.
tar -tzf project.tar.gz
project/
project/notes/
project/notes/c.txt
project/a.txt
project/b.txt
Look at those paths. Every entry is under project/. Nothing is loose at the top level. Extracting here would not scatter files across your current directory.
Step 3: Extract with tar
The -x flag extracts. Point the archive at a target directory with -C. Then it does not unpack into wherever you happen to be.
mkdir -p outdir
tar -xzf project.tar.gz -C outdir
find outdir -type f | sort
outdir/project/a.txt
outdir/project/b.txt
outdir/project/notes/c.txt
Everything landed inside outdir/project, exactly as it was packed.
An archive can contain any path, including ones that overwrite files you already have. Before you extract anything you did not create, run tar -tzf on it and read the list. If you see absolute paths or entries starting with ../, stop and do not extract it. Extract into a fresh empty folder so a surprise cannot reach your real files.
Step 4: Compress a single file with gzip
gzip is for one file at a time. It replaces the file with a compressed copy that ends in .gz.
echo alpha > a.txt
gzip a.txt
ls a.txt.gz
a.txt.gz
The original a.txt is gone. In its place is a.txt.gz. To bring it back, use gunzip, which reverses the change.
gunzip a.txt.gz
cat a.txt
alpha
gzip is fast and widely used, but it works on single files. To compress a whole folder, pack it with tar first. That is why the two are so often paired.
Step 5: Use zip and unzip
The zip format is what other phones, computers and websites expect. The -r flag makes zip descend into folders.
zip -r project.zip project
adding: project/ (stored 0%)
adding: project/notes/ (stored 0%)
adding: project/notes/c.txt (stored 0%)
adding: project/a.txt (stored 0%)
adding: project/b.txt (stored 0%)
Unlike tar, zip talks as it works. It lists each file it adds. To read the contents without unpacking, use unzip -l.
unzip -l project.zip
Archive: project.zip
Length Date Time Name
--------- ---------- ----- ----
0 2026-01-30 10:12 project/
0 2026-01-30 10:12 project/notes/
6 2026-01-30 10:12 project/notes/c.txt
6 2026-01-30 10:12 project/a.txt
5 2026-01-30 10:12 project/b.txt
--------- -------
17 5 files
To unpack, point unzip at a folder with -d. The -q flag keeps it quiet.
mkdir -p unz
unzip -q project.zip -d unz
find unz -type f | sort
unz/project/a.txt
unz/project/b.txt
unz/project/notes/c.txt
The same warning from Step 3 applies here. List a zip before you open it.
Quick reference
| Command | What it does |
|---|---|
tar -czf out.tar.gz dir | Create a gzipped tar archive |
tar -tzf out.tar.gz | List the contents without extracting |
tar -xzf out.tar.gz -C dir | Extract into a chosen directory |
gzip file | Compress one file, replacing it with file.gz |
gunzip file.gz | Decompress a .gz file back to the original |
zip -r out.zip dir | Create a zip archive of a folder |
unzip -l out.zip | List a zip's contents |
unzip -q out.zip -d dir | Extract a zip into a chosen directory |
Next steps
- Text processing with sed and awk to inspect the files inside your archives.
- Scheduling work with cron to make a backup archive run on a timer.
- Pipe Dreams to chain listing and filtering commands.
Test yourself on this
4 questions and 1 task where you run a real command, with the reasoning explained either way.