Archives & Compression: tar, gzip & zip
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:
| Letter | Meaning |
|---|---|
c | Create a new archive |
t | List (table of contents) |
x | Extract |
f | The next argument is the archive file name |
v | Verbose: show each file |
z | Compress or decompress with gzip (.tar.gz, .tgz) |
J | Use xz (.tar.xz), smaller but slower |
C dir | Change to dir first (for extracting somewhere else) |
So tar -czvf reads as "create, gzip, verbose, file". Here Priya backs up her shop website:
cd ~/projects
tar -czvf shop-site-2026-10-30.tar.gz shop-site
ls -lh shop-site-2026-10-30.tar.gzshop-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.
tar -tzvf shop-site-2026-10-30.tar.gz
tar -xzvf shop-site-2026-10-30.tar.gz -C /tmp/restoredrwxrwxr-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:
tar -czf shop-backup.tar.gz --exclude='*.log' --exclude='node_modules' shop-site
tar -czf etc-backup.tar.gz -C / etcThe 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:
ls -lh access.log
gzip -v access.log
ls -lh access.log.gz
zcat access.log.gz | grep -c " 404 "
gunzip access.log.gz-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:
| Tool | Extension | tar option | Character |
|---|---|---|---|
| gzip | .gz | -z | Fast, supported everywhere |
| xz | .xz | -J | Smallest files, slowest to create |
| zstd | .zst | --zstd | Very fast with good compression; increasingly the modern default |
| bzip2 | .bz2 | -j | Older; you will meet it in old downloads |
tar --zstd -cvf shop-site.tar.zst shop-site
tar -cJvf shop-site.tar.xz shop-sitezstd (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.
zip -r shop-site.zip shop-site
unzip -l shop-site.zip
unzip shop-site.zip -d /tmp/from-zipadding: 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.
fmust come last in the group, directly before the archive name:-czvf name.tar.gz, not-cfzv. - Extracting into the wrong place. List with
-tfirst and extract with-Cinto 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 foldercreates a gzip-compressed archive;-tzvflists it;-xzvf ... -C dirextracts it.fmust be followed directly by the archive name; list before you extract.gzipandgunzipcompress single files;zcatandzgrepread.gzfiles directly.- gzip is fast and universal, xz is smallest, zstd is fast with good compression.
- Use
zip -randunzipto 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
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/notesnotes/ 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?
c creates, z uses gzip, v is verbose and f names the archive.
2. How do you see what is inside backup.tar.gz without extracting it?
t lists the table of contents.
3. Which command searches a compressed log without unpacking it?
zgrep decompresses on the fly and searches the text.
4. Which compressor generally makes the smallest files but is the slowest?
xz trades speed for the best compression of the three.
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
