Skip to lesson

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.

Beginner to intermediate·13 min·Archives, packages and remote machines·Updated 2026-11-24

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. gzip compresses one file. bzip2, xz and zstd do 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:

  • c creates a new archive.
  • z compresses it with gzip.
  • v is verbose and lists files as it works (optional).
  • f is followed by the archive's file name. It must be the last flag in the group.
bash
cd ~
tar --sort=name -czvf project.tar.gz project
result
project/
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.css

The 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:

bash
tar -tzf project.tar.gz | head -n 6
result
project/
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:

bash
mkdir restore
tar -xzf project.tar.gz -C restore
ls restore/project
result
README.md
data
docs
logs
src

tar 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:

bash
cd ~/project
cp logs/app.log copy.log
gzip -k copy.log
ls copy.log*
gunzip -c copy.log.gz | head -n 2
result
copy.log
copy.log.gz
2026-01-15 10:00:01 INFO server started
2026-01-15 10:00:05 INFO user asha logged in

gunzip (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:

bash
cd ~/project
zip -rq docs.zip docs
unzip -l docs.zip
result
Archive:  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 ... GNU tar strips 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, not tar -czf backup.tar.gz folder/*.
  • Use -C to control paths. tar -czf site.tar.gz -C /var/www html stores html/... without the long /var/www prefix.
  • 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.

0 / 4 understood
01 What does tar -czf backup.tar.gz project do?
02 Which command lists the contents of an archive without extracting it?
03 What is the difference between tar and gzip?
04 Why list an archive before extracting it?

Ready for the next small win?

Mark the lesson complete when you are ready.

Course navigation

Tools and Workflow

Linux Command Line tutorial

Learn to work confidently in the terminal, from your first command to pipes, permissions, scripts and a real log-analysis project.

Your progress 0/43
Learning something new?Explore all courses