Archives & Compression: tar, gzip & zip

Linux · Lesson 16 of 32 6 min read Updated

Backups, website releases, log collections and source code downloads are usually shipped as one compressed file such as site.tar.gz. Two separate ideas are at work. Archiving bundles many files and folders into one file, keeping names, folders and permissions. Compression makes a file smaller. On Linux, tar does the archiving and tools such as gzip, xz and zstd do the compression; tar can call them for you in one step.

In this lesson you will create, list and extract archives, compress single files, and handle .zip files, which you will receive from Windows users.

tar: create an archive

tar (originally tape archive) takes a few letters that work like a sentence. The most important ones:

LetterMeaning
cCreate a new archive
tList (table of contents)
xExtract
fThe next argument is the archive file name
vVerbose: show each file
zCompress or decompress with gzip (.tar.gz, .tgz)
JUse xz (.tar.xz), smaller but slower
C dirChange to dir first (for extracting somewhere else)

So tar -czvf reads as "create, gzip, verbose, file". Here Priya backs up her shop website:

Terminal
cd ~/projects
tar -czvf shop-site-2026-10-30.tar.gz shop-site
ls -lh shop-site-2026-10-30.tar.gz
Output
shop-site/
shop-site/images/
shop-site/images/logo.png
shop-site/index.html
shop-site/style.css
-rw-rw-r-- 1 priya priya 41K Oct 30 13:20 shop-site-2026-10-30.tar.gz

The archive name comes straight after f. Getting this wrong is the classic tar disaster: tar -cvf shop-site index.html would treat shop-site as the archive to write. Putting the date in the name, as here, keeps many backups apart.

tar: list and extract

Before extracting an archive from someone else, look inside it. A well-made archive has everything inside one top-level folder; a careless one spills dozens of files into your current directory.

Terminal
tar -tzvf shop-site-2026-10-30.tar.gz
tar -xzvf shop-site-2026-10-30.tar.gz -C /tmp/restore
Output
drwxrwxr-x priya/priya       0 2026-10-28 21:30 shop-site/
drwxrwxr-x priya/priya       0 2026-10-28 21:30 shop-site/images/
-rw-rw-r-- priya/priya   38214 2026-10-28 21:30 shop-site/images/logo.png
-rw-rw-r-- priya/priya    4210 2026-10-28 21:30 shop-site/index.html
-rw-rw-r-- priya/priya    1385 2026-10-28 21:30 shop-site/style.css
shop-site/
shop-site/images/
shop-site/images/logo.png
shop-site/index.html
shop-site/style.css

The listing shows permissions, owner, size and date for each file. -C /tmp/restore extracts into that folder (it must exist first) instead of the current one. Modern GNU tar detects the compression when extracting, so tar -xvf file.tar.gz also works without z. To extract just one file, name it at the end: tar -xzf backup.tar.gz shop-site/index.html.

Excluding files

Backups should skip things you can rebuild, such as caches and dependency folders:

Terminal
tar -czf shop-backup.tar.gz --exclude='*.log' --exclude='node_modules' shop-site
tar -czf etc-backup.tar.gz -C / etc

The second line shows a useful trick: -C / makes tar store the path as etc/... rather than /etc/..., so the archive can be extracted anywhere. GNU tar strips the leading / from stored paths anyway and prints a notice about it.

Compressing single files: gzip, xz and zstd

gzip compresses one file and replaces it with a .gz version. gunzip reverses it. Text such as logs compresses extremely well:

Terminal
ls -lh access.log
gzip -v access.log
ls -lh access.log.gz
zcat access.log.gz | grep -c " 404 "
gunzip access.log.gz
Output
-rw-r----- 1 priya priya 1.9M Oct 30 13:25 access.log
access.log:      94.1% -- replaced with access.log.gz
-rw-r----- 1 priya priya 113K Oct 30 13:25 access.log.gz
37

The log shrank by about 94%. zcat, zless and zgrep read .gz files without unpacking them, which is how you search old rotated logs in /var/log. Use gzip -k to keep the original file as well.

The three common compressors make different trade-offs:

ToolExtensiontar optionCharacter
gzip.gz-zFast, supported everywhere
xz.xz-JSmallest files, slowest to create
zstd.zst--zstdVery fast with good compression; increasingly the modern default
bzip2.bz2-jOlder; you will meet it in old downloads
Terminal
tar --zstd -cvf shop-site.tar.zst shop-site
tar -cJvf shop-site.tar.xz shop-site

zstd (install with sudo apt install zstd if it is missing) is now used by many distributions for their own packages because it decompresses very quickly.

zip and unzip

.zip is the format Windows and macOS users send. Ubuntu may not have the tools yet; install them with sudo apt install zip unzip.

Terminal
zip -r shop-site.zip shop-site
unzip -l shop-site.zip
unzip shop-site.zip -d /tmp/from-zip
Output
adding: shop-site/ (stored 0%)
adding: shop-site/images/ (stored 0%)
adding: shop-site/images/logo.png (deflated 2%)
adding: shop-site/index.html (deflated 64%)
adding: shop-site/style.css (deflated 58%)

-r includes folders recursively. unzip -l lists the contents and -d chooses where to extract. Note that already-compressed files such as PNG images barely shrink. Unlike tar, zip compresses each file separately and does not keep Linux owners, so prefer tar for Linux backups and zip for sharing with Windows users.

Common mistakes

  • Wrong order of tar letters. f must come last in the group, directly before the archive name: -czvf name.tar.gz, not -cfzv.
  • Extracting into the wrong place. List with -t first and extract with -C into an empty folder.
  • Archiving with absolute paths. Use -C / and a relative path so the archive can be restored anywhere.
  • Expecting compression to shrink photos or videos. They are already compressed; tar them without compression or accept little gain.

Key points

  • tar -czvf out.tar.gz folder creates a gzip-compressed archive; -tzvf lists it; -xzvf ... -C dir extracts it.
  • f must be followed directly by the archive name; list before you extract.
  • gzip and gunzip compress single files; zcat and zgrep read .gz files directly.
  • gzip is fast and universal, xz is smallest, zstd is fast with good compression.
  • Use zip -r and unzip to exchange files with Windows users.

Exercise

Create a compressed backup of your notes folder named with today's date (use $(date +%F) in the name), list it to check its contents, then extract it into a new folder ~/restore-test and confirm with ls that the files came back.

Show solution
Terminal
tar -czvf notes-$(date +%F).tar.gz notes
tar -tzvf notes-2026-10-30.tar.gz
mkdir -p ~/restore-test
tar -xzvf notes-2026-10-30.tar.gz -C ~/restore-test
ls ~/restore-test/notes
Output
notes/
notes/todo.txt
notes/shopping.txt
drwxrwxr-x priya/priya       0 2026-10-30 09:20 notes/
-rw-rw-r-- priya/priya      41 2026-10-30 08:05 notes/todo.txt
-rw-rw-r-- priya/priya       0 2026-10-30 09:20 notes/shopping.txt
notes/
notes/todo.txt
notes/shopping.txt
shopping.txt  todo.txt

$(date +%F) inserts today's date as YYYY-MM-DD, so each day's backup gets its own name. Because the archive contains the folder notes/, the files appear in ~/restore-test/notes.

Quick Quiz

1. Which command creates a gzip-compressed archive of the folder site?

2. How do you see what is inside backup.tar.gz without extracting it?

3. Which command searches a compressed log without unpacking it?

4. Which compressor generally makes the smallest files but is the slowest?

Learn Linux with a mentor

Free lessons get you started. Our Ethical Hacking & Cyber Security training adds real projects and code reviews from working developers.

  • Project-based learning
  • Mentor code reviews
  • Free counselling call