Ravindra BagaleCourses & study guides मराठी Track your progress

AWS Cloud

Linux tar, gzip, zip and xz: Archive and Compress Files with Examples

Backups, log shipping, code releases and file transfers all need two things: put many files into one file (archive) and make it smaller (compress). tar does the first. gzip, bzip2, xz and zstd do the second. zip does both and opens on Windows. We learn each command and each key option, one by one, with a safe habit for every step.

In this chapter

  • Create, list and extract tar archives
  • Compress with -z, -j, -J and zstd
  • Use -C, --exclude and --strip-components
  • Use gzip, gunzip, zcat and -k
  • Use xz, bzip2 and zstd and pick the right one
  • Use zip and unzip, and verify archives

1. An everyday example: packing for a move

When a family moves from Pune to Bengaluru, they put many small things into one carton and tape it. Then they squeeze clothes in vacuum bags so the carton holds more. A list is stuck on top so they know what is inside without opening it.

Carton = tar archive. Vacuum bag = compression (gzip, xz). The list on top = tar -t. Unpacking at the new house in the right room = tar -x -C /target.

How tar.gz is made and openedtar walks the folder and writes each file with its name, owner, mode and timeThe stream goes through gzip (-z) which shrinks itResult: one .tar.gz fileTo open, gzip expands the streamtar reads each entry and recreates files under the target folderOwners are restored only when extracting as root
Always list (-t) before you extract an archive from someone else.

2. tar -c: create an archive

An archive keeps many files and their paths together. Compression makes the stored bytes smaller. A plain .tar can keep a folder tree while remaining uncompressed. In your lab, archive a folder with two short text files, then use tar -tf to predict the directory layout you’ll get back. The -f option names the archive file the command should work with. The -c, -t and -x options tell tar whether to create it, list it or extract it.

You need one file to copy, upload or keep as a backup instead of thousands.

tar -c creates. -f NAME gives the archive file name (must come right before the name). -v lists files as it goes. Give the folders or files to add. Use relative paths so it extracts cleanly.

  1. Pack a folder.
tar -cvf site.tar www/

Output:

www/
www/index.html
www/css/app.css

One file, not compressed yet.

  1. From the parent folder.
cd /var && sudo tar -cf /tmp/www.tar www && ls -lh /tmp/www.tar

Output:

-rw-r--r--. 1 root root 51M Oct 25 13:30 /tmp/www.tar

Paths start at www/.

3. tar -t: list without extracting

You want to check what is inside, and make sure it will not write to strange paths, before you unpack.

tar -tf FILE lists entries. Add -v for sizes, owners and dates. Works with compressed files too (tar detects the compression when reading).

  1. List.
tar -tf site.tar | head -3

Output:

www/
www/index.html
www/css/app.css

Safe, relative paths.

  1. Long list.
tar -tvf site.tar | head -2

Output:

drwxr-xr-x ravi/ravi    0 2026-10-25 13:20 www/
-rw-r--r-- ravi/ravi 2140 2026-10-25 13:20 www/index.html

Like ls -l.

4. tar -x and -C: extract, and choose where

Choose a separate empty restore folder. If the listing shows project/report.txt, extracting with -C restore should create restore/project/report.txt. After extraction, compare the restored file with your test original using cmp. An empty cmp result with status zero means the bytes match. This gives better evidence than checking only that the archive opens. Keep the original until you know the restored folder contains the files you expected.

Extracting in the wrong folder can mix files into your home or overwrite things.

tar -xf FILE extracts into the current folder. -C DIR extracts into DIR (it must exist). Name a path after the archive to extract only that file or folder. -k (--keep-old-files) refuses to overwrite.

  1. Extract into a new folder.
mkdir -p /tmp/restore && tar -xf site.tar -C /tmp/restore && ls /tmp/restore

Output:

www

Contained and tidy.

  1. Only one file.
tar -xf site.tar -C /tmp/restore www/index.html

Output:

(no output)

Quick restore of one page.

  1. Do not overwrite.
tar -xkf site.tar -C /tmp/restore

Output:

tar: www/index.html: Cannot open: File exists

Keeps your newer copy.

5. tar -z: gzip compression (.tar.gz, .tgz)

Logs and text compress 5 to 10 times. gzip is fast and opens everywhere.

Add -z to create or extract .tar.gz. The well-known short forms: tar -czf (create), tar -tzf (list), tar -xzf (extract). Modern tar detects compression when extracting, so -xf works too.

  1. Create .tar.gz.
tar -czf logs-2026-10-25.tar.gz /var/log/shop/ 2>/dev/null; ls -lh logs-2026-10-25.tar.gz

Output:

-rw-r--r--. 1 ravi ravi 6.2M Oct 25 13:35 logs-2026-10-25.tar.gz

From 48 MB to 6.2 MB.

  1. Extract it.
tar -xzf logs-2026-10-25.tar.gz -C /tmp/restore

Output:

(no output)

Leading / was removed when created.

6. tar -J and -j: xz and bzip2

For files kept a long time or downloaded many times, smaller matters more than speed.

-J uses xz (.tar.xz): smallest, slowest. -j uses bzip2 (.tar.bz2): older, in between. Set the level with XZ_OPT=-9 or use -I 'xz -T0' for all CPU cores.

  1. xz archive.
tar -cJf release.tar.xz build/ && ls -lh release.tar.xz

Output:

-rw-r--r--. 1 ravi ravi 3.9M Oct 25 13:40 release.tar.xz

Smaller than gzip's 6.2M.

  1. Use every core.
tar -I 'xz -T0' -cf release.tar.xz build/

Output:

(no output)

Much faster on big servers.

  1. bzip2.
tar -cjf old.tar.bz2 build/

Output:

(no output)

Only if someone asks for .bz2.

7. zstd with tar: fast and small

For backups and CI caches you want speed close to gzip and size close to xz. zstd gives both.

tar --zstd -cf FILE.tar.zst (GNU tar 1.31+). Or -I 'zstd -T0 -19' for level and threads. zstd -d FILE.zst to decompress a single file.

  1. Make a .tar.zst.
tar --zstd -cf cache.tar.zst node_modules/ && ls -lh cache.tar.zst

Output:

-rw-r--r--. 1 ravi ravi 41M Oct 25 13:45 cache.tar.zst

Quick to make and open.

  1. Extract.
tar --zstd -xf cache.tar.zst

Output:

(no output)

Same -x as always.

8. --exclude and --exclude-from

You do not want node_modules, .git, caches or secrets in a backup or release.

--exclude=PATTERN skips matches (put it before the folder names). Repeat it, or list patterns in a file with -X FILE (--exclude-from). --exclude-vcs skips .git and similar.

  1. Release without junk.
tar --exclude=node_modules --exclude='*.log' --exclude-vcs -czf app.tgz app/

Output:

(no output)

Small and clean.

  1. From a file.
printf '.env\n*.pem\ntmp/\n' > .tarignore && tar -X .tarignore -czf app.tgz app/

Output:

(no output)

Secrets never leave.

  1. Check nothing leaked.
tar -tzf app.tgz | grep -E '\.env|\.pem' || echo none

Output:

none

Always check.

9. --strip-components, -p and --same-owner

Use the file list to see which path components tar will remove. For project/docs/report.txt, stripping one component leaves docs/report.txt. Stripping two leaves report.txt. List the archive before choosing the count and try the extraction in the empty restore directory. Check that paths don’t unexpectedly collapse onto the same name. Decide whether you need to restore file owners and modes using your backup plan and identity, especially when you are practising with an archive from a different machine.

Downloaded releases often have a top folder like app-1.2.3/ that you do not want. Backups need permissions kept.

--strip-components=1 removes the first path part on extract. -p keeps permissions (default as root). --same-owner restores owners (default as root). --numeric-owner stores IDs, not names, for moving between systems.

  1. Drop the version folder.
tar -xzf node-v22.10.0-linux-x64.tar.gz -C /opt/node --strip-components=1 && ls /opt/node

Output:

bin  include  lib  share

bin is directly in /opt/node.

  1. Backup with owners.
sudo tar -cpzf etc-backup.tgz --numeric-owner /etc

Output:

tar: Removing leading `/' from member names

The warning is normal.

10. gzip and gunzip: one file

Old logs and SQL dumps are single files; you just want them smaller.

gzip FILE replaces FILE with FILE.gz. -k keeps the original. -d or gunzip decompresses. -1 fastest, -9 smallest (default 6). -l shows the ratio. -r does every file in a folder. gzip works on files, not folders (use tar for folders).

  1. Compress and keep the original.
gzip -k -9 dump.sql && ls -lh dump.sql*

Output:

-rw-r--r--. 1 ravi ravi 210M Oct 25 13:50 dump.sql
-rw-r--r--. 1 ravi ravi  31M Oct 25 13:50 dump.sql.gz

Both kept.

  1. Ratio.
gzip -l dump.sql.gz

Output:

  compressed        uncompressed  ratio uncompressed_name
    32505111           220200960  85.2% dump.sql

85% saved.

  1. Decompress.
gunzip -k dump.sql.gz

Output:

gzip: dump.sql already exists; do you wish to overwrite (y or n)?

-k and an existing file: it asks.

11. zcat, zless and zgrep: read without unpacking

Rotated logs are .gz. Unpacking them all wastes disk and time.

zcat FILE.gz prints the text. zless pages it. zgrep PATTERN FILE.gz searches. xzcat, bzcat, zstdcat do the same for other formats.

  1. Search old logs.
sudo zgrep -c ' 500 ' /var/log/nginx/access.log.2.gz

Output:

37

37 server errors that day.

  1. First lines.
zcat dump.sql.gz | head -n 2

Output:

-- PostgreSQL database dump
-- Dumped from database version 16.4

No file written.

12. xz, bzip2 and zstd on single files

Each tool has the same pattern, so once you know gzip you know them all.

xz FILE / unxz FILE.xz / xz -k -T0 -9. bzip2 FILE / bunzip2. zstd FILE (keeps original by default) / zstd -d / unzstd. All support -k and -d. -c writes to stdout.

ToolSpeedSizeUse for
gzipFastMediumLogs, web, everywhere
bzip2SlowSmallerOld systems
xzSlowestSmallestReleases, archives
zstdVery fastSmallBackups, CI, streaming
  1. xz with all cores.
xz -k -T0 dump.sql && ls -lh dump.sql.xz

Output:

-rw-r--r--. 1 ravi ravi 22M Oct 25 13:55 dump.sql.xz

22M vs gzip 31M.

  1. zstd.
zstd -T0 dump.sql

Output:

dump.sql : 13.10%   (210 MiB => 27.5 MiB, dump.sql.zst)

Fast, original kept.

  1. Stream a dump straight to a compressed file.
pg_dump shopdb | zstd -T0 > shopdb.sql.zst

Output:

(no output)

No big temp file.

13. zip and unzip

Windows and Mac users, emails and many web uploads expect .zip.

zip -r OUT.zip FOLDER adds a folder. -q quiet. -x 'pattern' excludes. -e asks for a password (weak, do not rely on it). unzip FILE.zip extracts; -d DIR into a folder; -l lists; -o overwrites without asking; -q quiet.

  1. Zip a report folder.
zip -rq reports-oct.zip reports/ -x '*.tmp' && ls -lh reports-oct.zip

Output:

-rw-r--r--. 1 ravi ravi 2.4M Oct 25 14:00 reports-oct.zip

Ready for email.

  1. List.
unzip -l reports-oct.zip | tail -n 3

Output:

     8120  2026-10-25 13:58   reports/gst.csv
---------                     -------
  9921040                     14 files

14 files inside.

  1. Extract into a folder.
unzip -q reports-oct.zip -d /tmp/rep && ls /tmp/rep

Output:

reports

Contained.

zip in depth: -r, -e, -u and -9

Windows and Mac users open .zip files with a double click. When you send files to a client, an accountant or a teacher, zip is the safest format to share.

zip packs many files into one .zip file and compresses them at the same time. First write the zip name, then the files or folders to add.

Common options

OptionWhat it does
-rRecursive: add a folder and everything inside it.
-eEncrypt: ask for a password and lock the zip.
-uUpdate: add only new or changed files to an existing zip.
-9Best (slowest) compression. -1 is fastest. Default is 6.
-qQuiet: do not print each file name.
-xExclude files that match a pattern.
  1. Zip a whole folder with -r.
zip -r gst-2026.zip gst-2026

Output:

  adding: gst-2026/ (stored 0%)
  adding: gst-2026/april.csv (deflated 71%)

Without -r you would get only the empty folder entry.

  1. Lock it with a password using -e.
zip -er salary-slips.zip slips

Output:

Enter password:
Verify password:
  adding: slips/ (stored 0%)

The zip cannot be opened without the password. The password is not shown while you type.

  1. Add a changed file with -u.
zip -u gst-2026.zip gst-2026/may.csv

Output:

updating: gst-2026/may.csv (deflated 70%)

Only the new or newer file goes in. The rest stay as they were.

  1. Squeeze harder with -9.
zip -9 -r photos-best.zip photos && ls -lh photos-best.zip

Output:

-rw-r--r-- 1 asha asha 41M Oct 25 10:12 photos-best.zip

-9 is a little smaller but slower. Photos are already compressed, so the gain is small.

Practise this in the tasks below: Practice tasks

unzip in depth: -l, -d, -o, -t and one file

People send you zips by email and download links. You must look inside before opening, open in the right folder and check the file is not broken.

unzip opens a .zip file. With no option it extracts everything into the current folder. You can also give file names after the zip to extract only those.

Common options

OptionWhat it does
-lList the files inside, do not extract.
-d DIRExtract into folder DIR (it is created if needed).
-oOverwrite existing files without asking.
-nNever overwrite existing files.
-tTest the zip for damage, do not extract.
-qQuiet output.
zip file1Extract only file1 from the zip.
  1. Look inside first with -l.
unzip -l vendor-kyc.zip

Output:

  Length      Date    Time    Name
---------  ---------- -----   ----
   182044  2026-10-20 09:14   pan.pdf
    90211  2026-10-20 09:15   aadhaar.pdf
---------                     -------
   272255                     2 files

Two PDFs are inside. Nothing was written to disk.

  1. Extract into a chosen folder with -d.
unzip vendor-kyc.zip -d /srv/kyc/v102

Output:

Archive:  vendor-kyc.zip
  inflating: /srv/kyc/v102/pan.pdf
  inflating: /srv/kyc/v102/aadhaar.pdf

Files go to /srv/kyc/v102, not the current folder.

  1. Replace old copies with -o.
unzip -o plugin-v2.zip -d wp-content/plugins

Output:

  inflating: wp-content/plugins/seo/seo.php

No "replace? [y]es, [n]o" question. Good for scripts.

  1. Test for damage with -t.
unzip -t backup-sept.zip

Output:

testing: db.sql   OK
No errors detected in compressed data of backup-sept.zip.

The zip is safe to use. A broken zip shows "bad CRC" or "End-of-central-directory signature not found".

  1. Take out one file only.
unzip vendor-kyc.zip pan.pdf

Output:

  inflating: pan.pdf

Only pan.pdf is extracted. Use quotes for patterns, like "*.pdf".

Practise this in the tasks below: Practice tasks

14. Verify archives: -t, -d, --test and checksums

A backup you cannot restore is not a backup. Corrupt archives are found only when you need them, unless you test.

gzip -t FILE.gz tests. xz -t, zstd -t, unzip -t do the same. tar -tzf FILE > /dev/null reads it all. tar -d (--compare) compares with the disk. sha256sum FILE > FILE.sha256, then sha256sum -c to check after copy.

  1. Test gzip.
gzip -t logs-2026-10-25.tar.gz && echo OK

Output:

OK

No output from gzip = good.

  1. Zip test.
unzip -tq reports-oct.zip

Output:

No errors detected in compressed data of reports-oct.zip.

Safe to send.

  1. Checksum after upload.
sha256sum app.tgz > app.tgz.sha256 && sha256sum -c app.tgz.sha256

Output:

app.tgz: OK

Same bytes on both ends.

15. Real examples from many industries

The same Linux skill shows up in every kind of company. Seeing it in many places helps you remember it.

Each row is a made-up team. It shows who uses this and how.

Team and cityHow they use itCommand
Amit, CA office, PuneZips monthly GST reports for clientszip -r gst-oct.zip gst/
Hannah, SaaS, BerlinShips releases as .tar.xztar -cJf app.tar.xz build/
Omar, bank, RiyadhNightly DB dump streamed to zstdpg_dump db | zstd > db.zst
Yuki, gaming, TokyoSearches old rotated logs with zgrepzgrep 'match_id=91' access.log.3.gz
Diego, media, LimaBacks up /etc with owners kepttar -cpzf etc.tgz /etc
Kavya, hospital, NashikVerifies every backup archivegzip -t backup.tgz

16. Common errors and how to fix them

Everyone hits these errors. Knowing the cause saves hours.

Read the exact message first. Then match it to a row below.

Error you seeWhy it happensHow to fix
tar: Cowardly refusing to create an empty archiveNo files named after -f NAMEGive the folder to pack.
tar -czf out.tgz folder/
tar: Removing leading `/' from member namesYou gave an absolute path; tar makes it safeNormal; or use -C / with relative path.
tar -czf etc.tgz -C / etc
gzip: stdin: not in gzip formatFile is not gzip (maybe plain tar or xz)Check with file, use the right flag.
file archive.tgz
tar: Error is not recoverable: exiting nowArchive truncated or corruptRe-download; verify checksum.
sha256sum -c app.tgz.sha256
tar: f: Cannot open: No such file or directoryOption order: -fcz puts c as the file nameKeep f last before the name.
tar -czf out.tgz dir/
tar: /target: Cannot open: No such file or directory (with -C)Target folder does not existCreate it first.
mkdir -p /target
gzip: dump.sql.gz already exists; do you wish to overwrite (y or n)?Output existsRemove it or use -f.
gzip -f dump.sql
zip warning: name not matched: reportsPath wrong or not in this folderCheck the path.
ls -d reports
unzip: cannot find zipfile directoryNot a zip, or incomplete downloadCheck with file and size.
file reports.zip
No space left on device during tarWriting the archive on the same full diskWrite to another disk or stream it away.
tar -czf - dir | ssh backup 'cat > dir.tgz'

17. Troubleshooting step by step

When an archive will not open, find its real type, test it, then extract into an empty folder.

Go top to bottom. Stop at the first step that shows the problem.

  1. What is it really?
file backup.tgz
  1. Is the size sane?
ls -lh backup.tgz
  1. Test the compression layer.
gzip -t backup.tgz
  1. List the tar layer.
tar -tzf backup.tgz | head
  1. Compare checksum with the source.
sha256sum backup.tgz
  1. Extract into an empty folder.
mkdir -p /tmp/x && tar -xzf backup.tgz -C /tmp/x

18. Lab 1: Backup and restore a project

Create a project, back it up without junk, verify it, delete and restore it.

A full hands-on lab. Run every step on a test Linux machine (an EC2 instance, a VM, or WSL).

  1. Make files.
mkdir -p proj/src proj/node_modules && echo app > proj/src/app.js && echo secret > proj/.env && echo dep > proj/node_modules/x.js

Output:

(no output)
  1. Backup with excludes.
tar --exclude=node_modules --exclude=.env -czf proj.tgz proj/

Output:

(no output)
  1. Check contents.
tar -tzf proj.tgz

Output:

proj/
proj/src/
proj/src/app.js
  1. Verify and checksum.
gzip -t proj.tgz && sha256sum proj.tgz > proj.tgz.sha256 && sha256sum -c proj.tgz.sha256

Output:

proj.tgz: OK
  1. Delete and restore.
rm -rf proj && tar -xzf proj.tgz && cat proj/src/app.js

Output:

app

19. Lab 2: Compare gzip, xz, zstd and zip

Compress the same file with each tool, compare size and time, and read one without unpacking.

A full hands-on lab. Run every step on a test Linux machine (an EC2 instance, a VM, or WSL).

  1. A big text file.
seq 1 3000000 > nums.txt && ls -lh nums.txt

Output:

-rw-r--r--. 1 ravi ravi 21M Oct 25 14:10 nums.txt
  1. gzip and xz.
time gzip -k nums.txt; time xz -k -T0 nums.txt

Output:

real 0m0.9s
real 0m2.8s
  1. zstd and zip.
zstd -q nums.txt; zip -q nums.zip nums.txt

Output:

(no output)
  1. Compare.
ls -lh nums.txt.* nums.zip | awk '{print $5, $9}'

Output:

6.3M nums.txt.gz
1.5M nums.txt.xz
2.9M nums.txt.zst
6.3M nums.zip
  1. Read without unpacking.
zcat nums.txt.gz | tail -n1

Output:

3000000

Ravindra Bagale's Tip

Remember tar as c-x-t: Create, eXtract, lisT, and keep f last because the name follows it. Before trusting any backup, test it (gzip -t) and restore it once into an empty folder.

20. Frequently asked questions

Q. Archive vs compress?

Archive = many files into one (tar). Compress = make smaller (gzip). .tar.gz does both.

Q. .tgz vs .tar.gz?

Same thing.

Q. Which format should I use?

gzip for compatibility, zstd for speed, xz for smallest, zip for Windows users.

Q. Does tar keep permissions?

Yes, it stores them; restoring owners needs root.

Q. Can gzip compress a folder?

No, tar it first, or gzip -r compresses each file separately.

Q. Why is my .gz not smaller?

Already compressed data (jpg, mp4, zip) does not shrink.

Q. Is zip -e safe?

Old zip encryption is weak. Use gpg or 7z AES for secrets.

Q. How to copy a folder to another server as an archive without temp files?

tar -czf - dir | ssh host 'tar -xzf - -C /dest'.

21. Interview questions and answers

Beginner level

Q. Create a .tar.gz of folder app?

tar -czf app.tgz app/.

Q. List a tar.gz without extracting?

tar -tzf app.tgz.

Q. Extract into /opt?

tar -xzf app.tgz -C /opt.

Q. Keep the original with gzip?

gzip -k FILE.

Mid level level

Q. Exclude node_modules from a tar?

--exclude=node_modules before the folder.

Q. Remove the top folder on extract?

--strip-components=1.

Q. Search inside a .gz log?

zgrep PATTERN FILE.gz.

Q. gzip vs xz vs zstd?

Speed vs size trade-off: gzip balanced, xz smallest, zstd fastest at good size.

Senior level

Q. Design a backup that you trust.

Consistent snapshot or dump, compress with zstd, checksum, copy offsite, test restore regularly, monitor.

Q. What is a tar bomb and how do you avoid it?

An archive without a top folder or with ../ paths; list first and extract into an empty folder.

Q. Move 2 TB between servers fast?

tar | zstd -T0 | ssh or mbuffer, or rsync for resumable copies.

Q. Why --numeric-owner for system backups?

User names may map to different IDs on the restore host.

Try at home

  1. Back up your home folder's Documents with tar and zstd, excluding *.iso. Test it and restore into /tmp/check.
  2. Take a log file, compress it with gzip, xz and zstd, and write a table of size and time.

Practise this chapter

Interview questions and answers

How do you prove that an archive is a usable backup?

List the archive members, extract them into a separate empty directory and compare important restored files with the originals. Verify meaningful content and the expected paths, plus metadata needed by the app. That demonstrates recovery rather than just archive creation.

Check your understanding

Choose an answer, then compare your reasoning with the explanation.

0 of 3 answered correctly
1. Which tar operation shows saved paths without writing the files back?

Answer and explanation

C. tar -tf archive.tar

The t operation lists members. The x operation extracts and c creates.

2. A member is project/docs/report.txt. What remains after stripping one leading component?

Answer and explanation

B. docs/report.txt

Strip-components counts path parts from the start. Removing project leaves two path parts.

3. What feature does an ordinary tar archive already supply before compression?

Answer and explanation

A. A collection of files and their paths in one archive

Archiving packages files together. Compression is a separate step that reduces stored size when the data permits it.

Practice Tasks

Practice checks run in your browser. They check the answer for this exercise; they do not create resources or connect to your AWS account.

About this scenario

Milan Furniture is a furniture business in Milan. Giulia Rossi looks after its IT and cloud work. They count money in EUR and run their AWS resources in the eu-south-1 Region. In the tasks below you do real jobs for them using what this lesson teaches. All names, account IDs, IP addresses and numbers are made up for practice.

0 / 22

Easy Create a tar.gz

Backups and transfers need archives.

Create site.tar.gz from the folder site.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy List an archive

Backups and transfers need archives.

List contents of site.tar.gz.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy Extract

Backups and transfers need archives.

Extract site.tar.gz here.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy Best gzip compression

Backups and transfers need archives.

Compress error.log with gzip at maximum level 9.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy Uncompress

Backups and transfers need archives.

Uncompress access.log.gz.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy Make a zip

Backups and transfers need archives.

Zip the folder report into report.zip.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy List zip

Backups and transfers need archives.

List contents of report.zip without extracting.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy Uncompress xz

Backups and transfers need archives.

Uncompress dump.sql.xz.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy Max zip compression

Backups and transfers need archives.

Zip the folder logs into logs.zip with the best compression level.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy Check a zip

Backups and transfers need archives.

Check invoices.zip for damage without extracting it.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Extract elsewhere

Backups and transfers need archives.

Extract site.tar.gz into /srv/www.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium xz archive

Backups and transfers need archives.

Create logs.tar.xz from folder logs.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Keep original

Backups and transfers need archives.

gzip access.log but keep the original.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Read without extracting

Backups and transfers need archives.

Search ERROR in app.log.gz without extracting.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Unzip elsewhere

Backups and transfers need archives.

Unzip report.zip into /tmp/r.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium xz a file

Backups and transfers need archives.

Compress dump.sql with xz using all CPU cores.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Password zip

Backups and transfers need archives.

Zip the folder payroll into payroll.zip with a password lock.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Update a zip

Backups and transfers need archives.

Put only the changed file index.html into the existing site.zip.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Unzip without asking

Backups and transfers need archives.

Extract theme.zip here and replace existing files without any question.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium One file from a zip

Backups and transfers need archives.

Extract only cover.jpg from photos.zip.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Hard Exclude files

Backups and transfers need archives.

Archive app as app.tar.gz but skip node_modules.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Hard Test integrity

Backups and transfers need archives.

Test backup.tar.gz for damage.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

In short

  1. tar -c create, -t list, -x extract, f before the name.
  2. -z gzip, -J xz, -j bzip2, --zstd zstd.
  3. -C, --exclude, --strip-components shape the result.
  4. gzip/gunzip -k, zcat and zgrep for single files.
  5. zip -r and unzip -d for Windows-friendly archives.
  6. Test with -t and checksums; restore once to be sure.

Revision, comparison and interview cards

Revise this lesson in a few minutes. Save the cards on your phone or download them as PNG files. The QR code on each card opens this lesson.

tar c t x. Pack a folder into one file. c create, t list, x extract. z gzip, J xz, f file. Make a backup.
Revision card: tar c t x · Download PNG · Open card page
gzip zip xz. Smaller files, faster copies. gzip fast, xz smallest. zip for Windows friends. Read without extracting.
Revision card: gzip zip xz · Download PNG · Open card page
gzip vs xz. Fast. / Slower.
Comparison: gzip vs xz · Download PNG · Open card page
tar.gz vs zip. Linux backups. / Sharing with Windows.
Comparison: tar.gz vs zip · Download PNG · Open card page
gzip vs xz?. gzip: fast, common. xz: smaller, slower. xz with all cores.
Interview card: gzip vs xz? · Download PNG · Open card page
tar.gz vs zip?. tar keeps Linux permissions. zip opens anywhere. Zip a folder.
Interview card: tar.gz vs zip? · Download PNG · Open card page
Skip folders in tar?. node_modules is huge. Use --exclude. Skip it.
Interview card: Skip folders in tar? · Download PNG · Open card page
Check a backup?. Corrupt archives fail restores. List or test it. Test gzip.
Interview card: Check a backup? · Download PNG · Open card page

More cards: Ravi Sir's Cards.