Linux Command Line · Lesson 32 of 43
Archives and compression with tar, gzip and zip
Bundle folders into archives with tar, compress with gzip, list and extract safely, and use zip and unzip when sharing with other systems.
UNDERSTAND IT. THEN TRY IT.
What you can do after this lesson
You will pack a folder into one file for backup or sharing, shrink files to save space, and unpack archives you download. Almost every piece of Linux software and every backup script uses tar and gzip, so these commands are worth knowing by heart.
Archiving versus compressing
Two different jobs often get mixed up:
- An archive combines many files and folders into one file, keeping names, folders, permissions and times. It does not make anything smaller.
tar(tape archive) does this. - Compression makes data smaller.
gzipcompresses one file.bzip2,xzandzstddo the same with different trade-offs between speed and size.
Put both together and you get the familiar .tar.gz (also written .tgz): a tar archive that was compressed with gzip.
tar: make an archive
The flags of tar look strange, but each letter has one job. For making a compressed archive:
ccreates a new archive.zcompresses it with gzip.vis verbose and lists files as it works (optional).fis followed by the archive's file name. It must be the last flag in the group.
cd ~
tar --sort=name -czvf project.tar.gz projectproject/
project/README.md
project/data/
project/data/fruit.txt
project/data/scores.csv
project/docs/
project/docs/notes.txt
project/docs/tasks.txt
project/logs/
project/logs/app.log
project/src/
project/src/app.py
project/src/hello.sh
project/src/style.cssThe extra option --sort=name (GNU tar) stores the files in alphabetical order, which makes the archive and its listing the same on every machine; it is optional. The folder project is now stored in project.tar.gz, and the original is untouched. GNU tar accepts flags with or without the leading dash, and many guides write tar czvf; both forms work.
Look before you extract
-t lists the contents without extracting anything. Make it a habit, because an archive might contain hundreds of files at its top level, or paths you do not expect:
tar -tzf project.tar.gz | head -n 6project/
project/README.md
project/data/
project/data/fruit.txt
project/data/scores.csv
project/docs/Notice that every path starts with project/. That is the sign of a well-made archive: it has one top-level folder. Archives that spill many files into the current folder when extracted are known as "tarbombs", and they are a pain to clean up.
tar: extract
x extracts. By default, it unpacks into the current folder, so move into an empty folder first, or choose one with -C:
mkdir restore
tar -xzf project.tar.gz -C restore
ls restore/projectREADME.md
data
docs
logs
srctar restores the contents with their original permissions and names. To extract only one file, name it after the archive: tar -xzf project.tar.gz project/README.md. GNU tar recognises compression automatically when extracting, so tar -xf file.tar.gz also works, and that is the form most people use in practice.
Other compression formats
Change the letter to change the compression: -j for bzip2 (.tar.bz2), -J for xz (.tar.xz), and --zstd for zstd. xz compresses best and is slower, gzip is fast and universal, and zstd is both fast and strong on modern systems. Without any compression letter, tar -cf file.tar folder makes a plain archive.
gzip and gunzip: one file
gzip compresses a single file, and replaces it with a .gz version unless you keep the original with -k:
cd ~/project
cp logs/app.log copy.log
gzip -k copy.log
ls copy.log*
gunzip -c copy.log.gz | head -n 2copy.log
copy.log.gz
2026-01-15 10:00:01 INFO server started
2026-01-15 10:00:05 INFO user asha logged ingunzip (or gzip -d) reverses it, and -c writes the result to the screen and keeps the compressed file. Handy tools read compressed files directly: zcat file.gz, zgrep pattern file.gz and zless file.gz, which are very useful for rotated log files such as app.log.2.gz.
zip and unzip: sharing with everyone
The .zip format is understood by Windows and macOS without extra tools. It archives and compresses at once. Use -r to include folders:
cd ~/project
zip -rq docs.zip docs
unzip -l docs.zipArchive: docs.zip
Length Date Time Name
--------- ---------- ----- ----
0 01-15-2026 10:00 docs/
65 01-15-2026 10:00 docs/tasks.txt
165 01-15-2026 10:00 docs/notes.txt
--------- -------
230 3 files-q makes zip quiet, and unzip -l lists the contents. The order of the entries follows the filesystem, so your listing may be ordered differently. Extract with unzip docs.zip -d somewhere, which puts the files in the folder somewhere. If zip or unzip are missing, install them with your package manager.
Safety and good habits
- Extract into a new, empty folder, never straight into your home or a system folder.
- List first with
tar -tf, and look for absolute paths or... GNUtarstrips a leading/and warns, which protects you, but you should still check archives from untrusted sources. - Archive a folder, not its contents, so it unpacks into one folder:
tar -czf backup.tar.gz folder, nottar -czf backup.tar.gz folder/*. - Use
-Cto control paths.tar -czf site.tar.gz -C /var/www htmlstoreshtml/...without the long/var/wwwprefix. - Exclude what you do not need:
--exclude='*.log'or--exclude=node_modules. - Check the archive after creating an important backup, by listing it or testing it with
gzip -t file.gz.
Common mistakes
Putting f in the wrong place. tar -cfz archive.tgz dir makes a file named z, because f takes the next word. Use -czf.
Overwriting an existing archive. tar -czf name.tar.gz folder replaces name.tar.gz without asking.
Extracting into the current folder by accident. Always check with pwd and list first.
Believing compression always helps. JPEG images, videos and zip files are already compressed and hardly shrink.
Exercise
Create a .tar.gz of your project folder, list it, and extract it into a new folder named check. Compare the two with diff -r project check/project. Then make a zip of the src folder, list its contents with unzip -l, and compress a copy of logs/app.log with gzip -k. Finally, use zcat to read the first line of the compressed copy.
KNOWLEDGE CHECK
A quick check. You’ve got this.
Try each question. Every answer comes with an explanation.
Ready for the next small win?
Mark the lesson complete when you are ready.