Skip to content
DevOps devops linux 5 min read

Archiving & Compression

On a Linux server you constantly need to bundle many files into one and shrink them down — to back up an app, move logs to another machine, or hand a folder to a teammate. Two separate ideas are at play here: archiving (packing many files and folders into one single file) and compression (making that file smaller so it takes less disk space and downloads faster). This page covers the three tools you will reach for every day on Ubuntu: tar, gzip, and zip.

Archiving vs compression — two different jobs

It helps to keep these straight, because beginners often think tar “zips” files. It does not.

  • An archive is one file that contains many files plus their folder structure, names, and permissions. A .tar file (“tape archive”, a name from the old days of backup tapes) is an archive. By itself it is not smaller than the originals.
  • Compression is the math that makes data smaller. gzip is the most common compressor on Linux. It works on one file at a time.

So the classic Linux pattern is: use tar to bundle a folder into one .tar file, then compress that file with gzip to get a .tar.gz. The good news is tar can do both in a single command, which is what you will almost always run.

FormatArchives many files?Compresses?When to use
.tarYesNoBundling when you do not need smaller size (rare)
.gzNo (single file only)YesCompressing one already-existing file, like a big log
.tar.gz (.tgz)YesYesThe default for backups and transfers on Linux
.zipYesYesSharing with Windows/Mac users, or per-file extraction

tar — the everyday archiver

tar is the Swiss-army knife. The flags look cryptic at first, so let us decode the ones you actually use:

  • c = create a new archive
  • x = extract from an archive
  • t = list (show) what is inside, without extracting
  • z = run it through gzip (compress/decompress)
  • v = verbose — print each file name as it is processed
  • f = file — the next word is the archive filename. This must come last among the grouped flags because the filename follows it.

A handy way to remember the three core operations: “create ze file” (-czf), “extract ze file” (-xzf), “list ze file” (-tzf).

Create a compressed archive

This is the command you will use most — back up a directory into a single .tar.gz file.

tar -czvf myapp-backup.tar.gz /var/www/myapp

Output:

tar: Removing leading `/' from member names
/var/www/myapp/
/var/www/myapp/index.html
/var/www/myapp/config.json
/var/www/myapp/assets/logo.png

The “Removing leading /” message is normal and good — tar stores paths relative to the root, so the archive stays portable and will not try to overwrite system files when extracted.

Gotcha: Never run tar with f not as the last flag. tar -cfz backup.tar.gz dir treats z as the filename and breaks. Always group as -czf so f sits right before the filename.

List the contents (look before you leap)

Before extracting an archive someone sent you, peek inside so you know where files will land.

tar -tzvf myapp-backup.tar.gz

Output:

drwxr-xr-x deploy/deploy   0 2026-06-15 10:22 var/www/myapp/
-rw-r--r-- deploy/deploy 412 2026-06-15 10:20 var/www/myapp/index.html
-rw-r--r-- deploy/deploy  89 2026-06-15 10:21 var/www/myapp/config.json

Extract an archive

tar -xzvf myapp-backup.tar.gz

That extracts into the current directory. To extract into a specific folder, use -C (change directory) — the target folder must already exist.

sudo mkdir -p /var/www/restored
sudo tar -xzvf myapp-backup.tar.gz -C /var/www/restored

gzip and gunzip — compress one file

Use gzip when you have a single existing file to shrink — a giant log is the classic case. Note that by default gzip replaces the original with the .gz version.

gzip /var/log/myapp/access.log
ls /var/log/myapp/

Output:

access.log.gz

To decompress, use gunzip (or gzip -d):

gunzip /var/log/myapp/access.log.gz

To keep the original while also producing a .gz, add -k (keep):

gzip -k bigfile.sql

You can also read a compressed file without permanently decompressing it using zcat, which is great for searching old logs:

zcat /var/log/myapp/access.log.gz | grep "500"

zip and unzip — for cross-platform sharing

zip bundles and compresses in one format that Windows and macOS open natively with no extra tools. On Ubuntu these are not always installed by default, so add them first:

sudo apt update
sudo apt install zip unzip

Create a .zip of a folder with -r (recursive, meaning include subfolders):

zip -r myapp.zip /var/www/myapp

Extract it with unzip:

unzip myapp.zip -d /tmp/restored

List contents without extracting using -l:

unzip -l myapp.zip

Output:

Archive:  myapp.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
      412  2026-06-15 10:20   var/www/myapp/index.html
       89  2026-06-15 10:21   var/www/myapp/config.json
---------                     -------
      501                     2 files

Tip: Prefer .tar.gz for server-to-server work — it preserves Linux file permissions and symbolic links exactly, which .zip can mangle. Reach for .zip only when a non-Linux person needs to open it.

A real backup, end to end

Here is a complete, runnable backup of a web app, stamped with today’s date so each backup is unique:

sudo tar -czvf /var/backups/myapp-$(date +%F).tar.gz -C /var/www myapp
ls -lh /var/backups/

Output:

-rw-r--r-- 1 root root 1.4M Jun 15 10:30 myapp-2026-06-15.tar.gz

Using -C /var/www myapp (instead of the full path) makes the archive store paths as myapp/..., so restoring later is clean and predictable.

Best practices

  • Default to .tar.gz for Linux backups — it keeps permissions, ownership, and symlinks intact.
  • Always list (-tzf) an unfamiliar archive before extracting so nothing lands where you do not expect.
  • Add a date with $(date +%F) to backup filenames so you never overwrite yesterday’s copy.
  • Use gzip -k and zip (which never deletes the source) when you must keep the original file.
  • Store backups outside the app directory — /var/backups is the conventional Ubuntu location.
  • For large transfers, verify with tar -tzf after copying to confirm the archive is not truncated.
  • Use zcat / zgrep to search compressed logs directly instead of decompressing them first.
Last updated June 15, 2026
Was this helpful?