Geavanceerd
Zoeken inHow-to-gidsen
Zo zoek je
  • Typ de taak in je eigen woorden (poort vinden, firewall openen, schijf vol) of plak een foutmelding tussen aanhalingstekens.
  • Kies "Snel antwoord" om alleen de commando's te zien, of een gebied om in één vakgebied te blijven.
  • "twee woorden" vindt precies die zin.
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken
Probeer:

How-to 14System · Linux

How to create and extract tar.gz archives

tar packs a folder into one file — for backups, for moving a site to another server, for release packages, and for logs you send to support. This how-to shows the four commands you need every day (create, list, extract, compress), how to keep owners and rights, exclude folders, copy a folder through SSH without a temp file, and how to check that an archive is OK before you need it.

  • 5 Lessen
  • 10 Commando's

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

14.1

The quick answer

tar -czf site-2026-11-17.tar.gz -C /var/www shop
tar -tzf site-2026-11-17.tar.gz | head
tar -xzf site-2026-11-17.tar.gz -C /tmp/restore
tar -xzf site-2026-11-17.tar.gz -C /tmp/restore shop/config/app.php

Remember the letters: c create, t list, x extract, z gzip, f file name follows, C change to this folder first.

14.2

Step by step

  1. Create an archive. Use -C so the paths inside are short and relative (not /var/www/...):
tar -czf /backup/etc-$(date +%F).tar.gz -C / etc
tar -czf /backup/shop.tar.gz -C /var/www --exclude='shop/cache' --exclude='*.log' shop
  1. Choose the compression:
OptionEndingUse
-z.tar.gzFast, works everywhere. The default choice.
-j.tar.bz2Smaller, slower. Old style.
-J.tar.xzMuch smaller, slow to create. Releases, long-term archives.
--zstd.tar.zstVery fast and small. Good for big backups (tar 1.31+).

When you extract, modern GNU tar finds the compression itself: tar -xf file.tar.xz works.

  1. List before you extract (see what is inside and the paths):
tar -tvzf shop.tar.gz | head -5
drwxr-xr-x nginx/nginx       0 2026-11-17 09:12 shop/
-rw-r--r-- nginx/nginx    4212 2026-11-17 09:10 shop/index.php
  1. Extract to a test folder first, never over live files by mistake:
mkdir -p /tmp/restore
tar -xzf /backup/shop.tar.gz -C /tmp/restore
tar -xzpf /backup/shop.tar.gz -C /var/www --same-owner

As root, tar keeps owners and modes by default (-p, --same-owner). Add --acls --xattrs --selinux when you create and extract system files on RHEL.

  1. Copy a folder to another server in one stream (no big temp file):
tar -czf - -C /var/www shop | ssh web2 'tar -xzf - -C /srv'
  1. Check the archive and save a checksum with it:
gzip -t shop.tar.gz && tar -tzf shop.tar.gz > /dev/null && echo OK
sha256sum shop.tar.gz > shop.tar.gz.sha256
sha256sum -c shop.tar.gz.sha256
shop.tar.gz: OK

14.3

Check that it worked

A backup is only good when the restore works. Extract into /tmp/restore, compare with the source, then delete the test folder:

diff -r /var/www/shop /tmp/restore/shop && echo "same"

Commando's in deze les

14.4

Common problems

ProblemCauseFix
Removing leading '/' from member namesYou gave absolute paths.Only a note — tar stores relative paths (safe). Use -C / to avoid it.
gzip: stdin: not in gzip formatThe file is not gzip (maybe xz, zstd, or plain tar).file archive; then tar -xf without -z.
Unexpected EOF in archiveThe file is cut (disk full, copy stopped).Check free space; copy again; compare sha256sum.
Files have the wrong owner after extractExtracted as a normal user.Extract as root, or chown -R after.
Cannot open: Permission deniedNo write right in the target folder.Use a folder you own, or root for system paths.
The archive is hugeIt took caches, logs or node_modules.--exclude=PATTERN, or --exclude-vcs for .git.

14.5

Go further

1Why use -C /var/www shop instead of /var/www/shop?

The archive then holds short relative paths (shop/...), so you can extract it anywhere.

2How do you know a backup archive is good?

Test it (gzip -t, tar -t), keep a sha256sum, and do a real test restore.

Alle commando's in dit hoofdstuk