Log inSign up

Pack and unpack folders with tar

Create an archive with tar -czf site.tar.gz /var/www/example.net, look inside with tar -tzf site.tar.gz and unpack it into a folder with tar -xzf site.tar.gz -C /restore. Swap z for J (xz) or use --zstd to choose a different compressor; GNU tar is installed on every Linux image.

Topic: File transfer and archives · Tested on: Ubuntu 24.04 and 22.04, Debian 12 and 13, Rocky Linux 9, AlmaLinux 9 · Updated 2026-12-03

What is a tar archive and when do I need one?

tar joins many files into a single stream while keeping names, folder structure, permissions, owners and timestamps. Compression is a separate layer added on top, which is why file names look like .tar.gz or .tar.zst. You need one whenever thousands of small files should travel as one piece: before an upload with scp, as a quick copy of a configuration folder before an upgrade, or to hand a release to another server.

Because tar keeps Unix metadata, it is the natural format between Linux machines. For sharing with Windows users who expect to double click a file, zip is friendlier.

Is tar installed, and which compressors can I use?

GNU tar ships with Ubuntu, Debian, Rocky Linux and AlmaLinux. gzip is always present; xz usually is; zstd may need a quick install.

# Ubuntu, Debian
sudo apt update
sudo apt install tar gzip xz-utils zstd

# Rocky Linux 9, AlmaLinux 9
sudo dnf install tar gzip xz zstd

tar --version | head -n 1
zstd --version

As a rule of thumb: gzip (z) is fast and understood everywhere, xz (J) makes the smallest files but is slow to create, and zstd (--zstd) compresses about as well as xz at a much higher speed. On a shared vCPU, zstd is usually the sensible default for large backups.

How do I create an archive?

The letters mean: c create, f file name follows, v verbose, plus one compression letter. Put -f last in the bundle so the name comes right after it.

tar -czf etc-$(date +%F).tar.gz /etc
tar -cJf logs.tar.xz /var/log/nginx
tar --zstd -cf www.tar.zst -C /var/www example.net
tar -cf - /srv/data | zstd -T0 -19 > data.tar.zst

The third line uses -C to change into /var/www first, so the archive contains example.net/... rather than the full absolute path. The last line pipes into zstd directly to use all cores and a high level. When you archive absolute paths, tar prints Removing leading '/' from member names; that is a safety feature, not an error, and it means extraction lands in the current folder instead of overwriting system files.

How do I leave things out of an archive?

Use --exclude with a pattern, once per pattern, or collect patterns in a file for -X. Exclude options must come before the paths being archived.

tar --exclude=node_modules --exclude='*.log' --exclude=.cache \
    -czf app.tar.gz -C /srv app

printf "%s\n" node_modules .git '*.tmp' > tar-exclude.txt
tar -X tar-exclude.txt -czf app.tar.gz -C /srv app

tar --exclude-vcs --exclude-caches-all -czf src.tar.gz project/

--exclude-vcs skips Git and similar metadata, and --exclude-caches-all skips any folder that contains a standard CACHEDIR.TAG file.

How do I list, test and extract an archive?

List with t, extract with x, and always choose the destination with -C. Modern GNU tar detects the compression on its own when reading, so the compression letter is optional for t and x.

tar -tvf www.tar.zst | less
mkdir -p /tmp/restore
tar -xf www.tar.zst -C /tmp/restore
tar -xf etc-2026-12-03.tar.gz -C /tmp/restore etc/nginx/nginx.conf

The last example pulls one file out of a big archive. Extracting into an empty scratch folder first and then copying what you need is much safer than unpacking straight over /. Archives from strangers deserve a listing before extraction, to spot absolute paths or ../ entries.

How do I keep owners, ACLs and SELinux labels?

Run tar as root and add the metadata flags on both creation and extraction.

sudo tar --acls --xattrs --xattrs-include='*' --numeric-owner \
     -cpzf srv-full.tar.gz -C / srv

sudo tar --acls --xattrs --xattrs-include='*' --numeric-owner \
     -xpzf srv-full.tar.gz -C /

-p restores exact permissions, and --numeric-owner keeps numeric user IDs so files stay with the right accounts even if names differ between machines. On Rocky and AlmaLinux, run sudo restorecon -Rv /srv after a restore so SELinux contexts match the policy. For whole-system copies, see full-server copies.

How do I stream an archive straight to another server?

Write to standard output with -f - and pipe it into SSH. Nothing touches the local disk, which helps when the source server is nearly full.

# push a folder to a new server
tar -czf - -C /var/www example.net | ssh deploy@198.51.100.24 "tar -xzf - -C /var/www"

# pull a remote folder into a local archive
ssh deploy@198.51.100.24 "tar --zstd -cf - -C /srv data" > data.tar.zst

For repeat copies of the same tree, rsync is better because it only sends changes.

Can tar make incremental archives?

Yes. GNU tar records file states in a snapshot file given with --listed-incremental. The first run is a full archive, and each following run with the same snapshot file contains only what changed.

tar --listed-incremental=/var/backups/www.snar -czf www-full.tar.gz -C /var/www .
tar --listed-incremental=/var/backups/www.snar -czf www-inc1.tar.gz -C /var/www .

# restore: full first, then each increment in order
tar --listed-incremental=/dev/null -xzf www-full.tar.gz -C /restore
tar --listed-incremental=/dev/null -xzf www-inc1.tar.gz -C /restore

This works, but managing chains by hand gets fiddly. BorgBackup handles deduplication, pruning and encryption for you.

What goes wrong with tar most often?

  • Cannot open: No such file or directory right after -f: the options are in the wrong order, as in -fcz. Keep f last.
  • This does not look like a tar archive: the file is incomplete, or it is a plain .gz or .zip. Check with file name.tar.gz.
  • zstd: Cannot exec: install the zstd package.
  • No space left on device: check free space first with df -h, or stream the archive to another machine.

Questions about tar archives

How do I check an archive is not corrupt?

Run tar -tf file.tar.gz > /dev/null. It reads the whole archive and reports errors. For compressed data, gzip -t, xz -t or zstd -t test the outer layer.

Can I split a large archive into parts?

Yes: tar -czf - data | split -b 2G - data.tar.gz.part-, and later cat data.tar.gz.part-* | tar -xzf -.

Does tar compress by itself?

No. Without z, J or --zstd, the result is an uncompressed .tar file about the size of the input.

How do I compare an archive to the files on disk?

Use tar -df archive.tar.gz -C /path. It lists every file whose content, size or time differs.

Should I put database folders into tar while the database runs?

No. The copy can be inconsistent. Dump the database first (for example with pg_dump or mysqldump) and archive the dump.