Comprehensive Guide to Using tar for Compression and Decompression (with gzip and zstd)
The tar (Tape ARchive) command in Linux is the standard tool for archiving files and directories. While tar itself does not compress data, it can seamlessly use external compression algorithms (like gzip, zstd, bzip2, xz) to create compressed archives. This tutorial focuses on the two most popular compression methods: gzip (.tar.gz or .tgz) and zstd (.tar.zst), covering compression, decompression, and common options.
1. Basic Usage
The general syntax of tar is:
tar [options] [archive-file] [files/directories to archive]
-c: Create an archive-x: Extract an archive-v: Verbose – list processed files-f: Specify the archive file name (must be the last option before the file name)-t: List contents of an archive
2. Creating a tar archive (without compression)
To create an uncompressed tar archive:
tar -cvf archive.tar /path/to/directory
-ccreate,-vverbose,-ffilename followed byarchive.tar.
3. Compression with gzip
Gzip is the most common compression method. It produces files with extension .tar.gz or .tgz.
3.1 Create a .tar.gz archive
Use the -z flag:
tar -czvf archive.tar.gz /path/to/directory
Or use the shorter .tgz extension:
tar -czvf archive.tgz /path/to/directory
3.2 Decompress a .tar.gz archive
tar -xzvf archive.tar.gz
Options: -x extract, -z gunzip, -v verbose, -f file.
To extract to a specific directory:
tar -xzvf archive.tar.gz -C /target/directory
(Note: -C changes to the target directory before extraction.)
3.3 List contents of a tar.gz file
tar -tzvf archive.tar.gz
(-t list; -v shows permissions, size, etc.)
4. Compression with zstd (Zstandard)
Zstd offers high compression speed and good ratios. The extension is .tar.zst. You need zstd installed (sudo apt install zstd on Debian/Ubuntu, sudo dnf install zstd on Fedora).
4.1 Create a .tar.zst archive
Use the --zstd flag (introduced in tar 1.31):
tar -cvf archive.tar.zst --zstd /path/to/directory
If your tar does not support --zstd, you can pipe manually (see section 5).
Alternatively, some systems allow
-I zstdor--use-compress-program=zstd. Example:tar -cvf archive.tar.zst -I zstd /path/to/directory
4.2 Decompress a .tar.zst archive
tar -xvf archive.tar.zst --zstd
Or with pipe (see section 5).
4.3 List contents of a .tar.zst file
tar -tvf archive.tar.zst --zstd
5. Using pipes for compression (when tar lacks built‑in support)
If your tar version does not have the --zstd flag, you can combine tar and the compression tool using pipes.
5.1 Create .tar.zst with pipe
tar -cvf - /path/to/directory | zstd > archive.tar.zst
-f -means output to stdout (pipe).zstd > archive.tar.zstcompresses the stream.
5.2 Decompress .tar.zst with pipe
zstd -dc archive.tar.zst | tar -xvf -
-ddecompress,-coutput to stdout, then pipe totar -xvf -.
5.3 List contents via pipe
zstd -dc archive.tar.zst | tar -tvf -
6. Advanced options
| Option | Description |
|---|---|
--exclude=PATTERN |
Exclude files matching pattern (e.g., --exclude='*.log') |
--exclude-vcs |
Exclude version control system directories (.git, .svn, etc.) |
--one-file-system |
Stay on the same filesystem (useful for backups) |
-p or --preserve-permissions |
Preserve original file permissions on extraction |
--same-owner |
Preserve original ownership (requires root) |
-j |
Use bzip2 compression (.tar.bz2) |
-J |
Use xz compression (.tar.xz) |
Example: archive with gzip, excluding .git and verbose
tar -czvf project-backup.tar.gz --exclude-vcs --exclude='*.tmp' my_project/
7. Comparison of compression ratios (typical)
| Method | Extension | Speed (relative) | Size (relative) |
|---|---|---|---|
| None | .tar |
fastest | largest |
| gzip | .tar.gz |
fast | medium |
| zstd (level 3) | .tar.zst |
very fast | small |
| zstd (level 19) | .tar.zst |
slow | very small |
| bzip2 | .tar.bz2 |
slow | smaller |
| xz | .tar.xz |
slower | smallest |
To set zstd compression level (default 3):
tar -cvf archive.tar.zst --zstd -I 'zstd -19' /path/to/directory
8. Quick reference cheat sheet
# Create .tar.gz
tar -czvf archive.tar.gz directory/
# Extract .tar.gz
tar -xzvf archive.tar.gz
# Create .tar.zst (modern tar)
tar -cvf archive.tar.zst --zstd directory/
# Extract .tar.zst (modern tar)
tar -xvf archive.tar.zst --zstd
# Create .tar.zst via pipe (legacy)
tar -cvf - directory/ | zstd > archive.tar.zst
# Extract .tar.zst via pipe (legacy)
zstd -dc archive.tar.zst | tar -xvf -
# List contents of any compressed tar
tar -tvf archive.tar.gz
tar -tvf archive.tar.zst --zstd
9. Troubleshooting
tar: Unrecognized option '--zstd'– Yourtarversion is too old. Upgrade or use the pipe method.zstd: command not found– Install zstd (apt install zstd/yum install zstd).- Permission denied on extraction – Use
sudoif the archive contains files owned by other users, or use--no-same-owner. - Archive corrupted – Check the file with
gzip -t archive.tar.gzorzstd -t archive.tar.zst.
Verify
apt update
apt install curl -y
curl -L 'https://filegator.kafana.dev/?r=/download&path=L3dwLWluc3RhbGwvd3AtMjAyNC0xMS0xNC50YXIuZ3o%3D' -o wp.tar.gz
tar -xzvf wp.tar.gz
Proven
This tutorial covers the essential tar commands with gzip and zstd. With these patterns you can efficiently archive and compress almost any Linux directory or file set.