How-to

Create tar.gz Archives with Tar and Gzip

Tar bundles many files into one; gzip shrinks it. Together they make the archive format every system can open, and a few flags make it fast.

How do I create a tar.gz archive?

tar -czf site-2026-12-03.tar.gz -C /srv site

-c creates, -z compresses with gzip, -f names the output file. -C /srv changes into /srv first, so the archive holds site/... rather than srv/site/.... That small detail makes archives far nicer to extract elsewhere. Add -v to see each file as it is added; leave it off for big jobs, since printing thousands of names slows things down.

Why use tar and gzip together?

They do different jobs. Tar collects files, folders, permissions, owners and links into one stream without compressing anything. Gzip compresses a single stream without knowing about files. Combined, you get one compressed file that preserves the structure. That separation is why you can swap gzip for another compressor without changing how tar works.

How do I leave things out?

tar -czf app-src.tar.gz --exclude='node_modules' --exclude='*.log' --exclude-vcs -C /home/ana app

--exclude-vcs skips .git, .svn and similar folders in one go. Patterns work like shell wildcards and must be quoted. To keep exclude lists in a file, use --exclude-from=list.txt, the same idea as in Exclude files in rsync.

Which compression level should I pick?

gzip levels run from 1 (fast, larger) to 9 (slow, smaller); tar's -z uses the default, level 6. For text-heavy data such as logs, SQL dumps and source code, level 6 usually lands within a few percent of level 9 while running much faster. Set the level through the environment or a pipe:

tar -cf - -C /var/log nginx | gzip -1 > nginx-logs-fast.tar.gz
tar -cf - -C /var/backups db | gzip -9 > db-small.tar.gz

Already-compressed data such as JPEG, MP4 or zip files will not shrink much at any level. For media folders, plain tar -cf without compression saves time.

How do I use all my cores?

gzip uses one core. pigz produces the same format using every core and is a drop-in replacement:

apt install pigz          # dnf install pigz on Enterprise Linux
tar -I 'pigz -p 12' -cf media-index.tar.gz -C /srv media-index

On the S1 with 12 threads, or a dual-socket box like D2 with 48, pigz turns an hour-long compression into minutes. The result is a normal gzip file that any system opens.

How does gzip compare with zstd and xz?

Formattar flagSpeedSizeBest for
gzip-zFastMediumSharing, universal support
zstd--zstdVery fastSmaller than gzipBackups and internal transfers
xz-JSlowSmallestRelease files downloaded many times
bzip2-jSlowSmallOpening older archives

For your own backups, zstd is hard to beat: tar --zstd -cf backup.tar.zst -C /srv data, or tar -I 'zstd -T0 -10' -cf ... for multi-threaded higher compression. GNU tar on every current Linux image supports --zstd. Use gzip when the file goes to someone whose tools you do not control.

Can I archive straight to another machine?

Yes, without a temporary file. Pipe tar through SSH:

tar -czf - -C /srv uploads | ssh vault@198.51.100.12 'cat > /backup/uploads-$(date +%F).tar.gz'

The archive is built on the fly and written only on the far side, which helps when the source disk is nearly full. The data leaving the source machine counts toward its monthly outgoing allowance of 15 TB (5 TB in Singapore); the receiving box takes it in free.

How do I check an archive after creating it?

gzip -t site-2026-12-03.tar.gz && echo gzip-ok
tar -tzf site-2026-12-03.tar.gz > /dev/null && echo tar-ok
sha256sum site-2026-12-03.tar.gz > site-2026-12-03.tar.gz.sha256

The first command checks compression integrity, the second walks the whole archive structure, and the checksum file lets anyone confirm the copy later. Extracting is covered in Extract .tar.gz files.

Keep Reading

Extract .tar.gz files

Unpack tar.gz and .tgz archives from the command line: target folders, single files, stripped paths, ownership and safe handling of unknown archives.

Open

Extract .tar.bz2 files

Open .tar.bz2 and .tbz2 archives, list them first, extract single files, and speed up big ones with parallel bzip2 on many cores.

Open

The tar command

A working reference for tar: modes, compression flags, appending, comparing, incremental backups and the options people forget.

Open

Back up data with rsync

Build dated, space-saving snapshot backups with rsync and hard links, pull them from a second machine and test restores.

Open

FAQ

Quick Answers

Run tar -czf name.tar.gz followed by the folder, ideally with -C to set the base folder.

Use pigz through tar -I 'pigz -p N'; it creates standard gzip files using every core.

For backups usually yes: faster and smaller. Use gzip when the recipient may lack zstd.

Add --exclude='pattern' for each, or --exclude-vcs to skip version control folders.

Run gzip -t for the compression layer and tar -tzf for the archive structure.