Updated: 9 November 2026 · Applies to: Debian 13 (GNU tar 1.35, gzip 1.13, Zip 3.0, UnZip 6.00) and Ubuntu 24.04 (tar 1.35, gzip 1.12); the same commands work on AlmaLinux and Rocky Linux
To pack a directory into a gzipped tarball, run tar -czf archive.tar.gz folder. To unpack it, run tar -xzf archive.tar.gz (add -C /path to choose where). gzip file compresses a single file to file.gz, and gunzip file.gz reverses it. gzip cannot compress a directory into one file: that is what tar is for.
Quick reference
- Create a .tar.gz (gzip):
tar -czf archive.tar.gz folder - Create a .tar.xz (smaller, slower):
tar -cJf archive.tar.xz folder - List the contents:
tar -tzf archive.tar.gz - Extract here:
tar -xzf archive.tar.gz - Extract into /dest:
tar -xzf archive.tar.gz -C /dest - Extract, compression detected automatically:
tar -xf archive.tar.gz - Compress a file to file.gz (original removed):
gzip file - Compress and keep the original:
gzip -k file - Decompress file.gz back to file:
gunzip file.gz - Read a .gz file without unpacking it:
zcat file.gz - Create a zip:
zip -r archive.zip folder - Extract a zip into /dest:
unzip archive.zip -d /dest
The tar letters: c create, x extract, t list, z gzip, J xz, v verbose (show each file), f the archive file name, which must come last before the name.
tar.gz, tgz and gz: what is the difference?
- .tar: many files and folders packed into one file, not compressed.
- .tar.gz or .tgz: a tar file compressed with gzip, also called a gzipped tarball. The two extensions mean exactly the same thing.
- .gz: one single file compressed with gzip (for example
access.log.gz). - .zip: archive and compression in one format, common on Windows.
Create a tar.gz archive of a directory
tar -czvf site.tgz site
Output (each file is listed because of v):
site/ site/css/ site/css/style.css site/page1.html site/page3.html site/page2.html site/img/ site/img/logo.png
Leave out a folder with --exclude (put it before the folder name):
tar -czf site-noimg.tar.gz --exclude='site/img' site
List and extract a tar.gz file
Look before you extract, so you know whether the archive has a top folder:
tar -tzf site.tar.gz
Extract into a chosen directory (it must exist):
mkdir -p /tmp/restore tar -xzf site.tar.gz -C /tmp/restore
Extract only one file by giving its path exactly as the listing shows it:
tar -xzf site.tar.gz site/css/style.css
GNU tar recognises the compression itself when extracting, so tar -xf site.tgz works for .tar.gz, .tgz and .tar.xz alike.
gunzip and tar: the two-step way
gunzip on a tarball removes only the gzip layer and leaves a plain .tar file, which you then unpack with tar:
gunzip backup.tar.gz tar -xf backup.tar
Or stream it without writing the .tar to disk:
gunzip -c backup.tar.gz | tar -xf -
tar -xzf does both steps at once, so the two-step way is only needed on systems where tar has no gzip support.
gzip command on Linux
gzip replaces the file with a compressed copy:
gzip report.log ls -l report.log*
Output:
-rw-r--r-- 1 root root 45015 Nov 8 20:03 report.log.gz
See the compression ratio:
gzip -l report.log.gz
Output:
compressed uncompressed ratio uncompressed_name
45015 108894 58.7% report.log
Other useful options:
gzip -k file: keep the original next to the .gz file.gzip -9 file: best compression (slower);-1is fastest.gzip -d file.gz: decompress, the same asgunzip.gzip -t file.gz: test the file for damage; no output means it is fine.zcat,zlessandzgrep: read or search a .gz file without unpacking it.
Can gzip compress a directory?
Not into one file. Pointing gzip at a directory gives:
gzip: site is a directory -- ignored
gzip -r site compresses every file inside it separately (page1.html.gz, style.css.gz and so on) and keeps the folder, which is rarely what you want. To get one compressed file of a directory, use tar -czf site.tar.gz site. gunzip -r site undoes gzip -r.
zip and unzip
zip -r site.zip site
unzip -l site.zip
unzip site.zip -d /tmp/z
If zip or unzip is missing, install it with apt install zip unzip (Debian, Ubuntu) or dnf install zip unzip (AlmaLinux, Rocky Linux). More examples, including password-protected zips, are in How to Zip and Unzip Files in Linux and How to Unzip a Password Protected ZIP File in Linux.
Which format is smallest?
Lab example from our test container (a small folder of text files and one random image, 550 KB in total; your results depend on the data):
-rw-r--r-- 1 root root 339378 Nov 8 20:03 site.tar.gz -rw-r--r-- 1 root root 207128 Nov 8 20:03 site.tar.xz -rw-r--r-- 1 root root 347342 Nov 8 20:03 site.zip
xz gave the smallest file but is slower. gzip is the usual choice for backups and downloads because every system can open it. Images, videos and files that are already compressed hardly shrink at all.
Related articles
- How to Download and Extract Tar Files with One Command
- Tar with Password: Encrypt and Decrypt tar Files with OpenSSL
- How to Use 7-Zip on Linux
- Guide to create and extract Tar and Zip Archives (in cPanel)
Official documentation: GNU tar manual and GNU gzip manual.