On this page

There are two separate ideas in this chapter: archiving (bundling many files into one) and compressing (making a file smaller). tar archives, while gzip, xz and zip compress. They are often combined.

tar — the archiver

tar bundles files and directories into a single archive file.

Flag Meaning
-c create an archive
-x extract
-t list contents
-f FILE the archive file to use
-v verbose: show what is happening
-z compress with gzip
-J compress with xz

Create a gzip-compressed archive of a directory:

$ tar -czf proj.tar.gz proj

List what is inside without extracting:

$ tar -tzf proj.tar.gz
proj/
proj/data.txt
proj/main.c

Extract it:

$ tar -xzf proj.tar.gz

To extract into a specific directory, use -C:

$ tar -xzf proj.tar.gz -C /tmp/out

For xz-compressed archives, swap -z for -J:

$ tar -cJf proj.tar.xz proj
$ tar -xJf proj.tar.xz

gzip — compress a single file

gzip compresses one file at a time, appending .gz:

$ gzip main.c
$ ls main.c*
main.c.gz

The original file is removed. Keep it with -k:

$ gzip -k main.c

Decompress with -d (or gunzip):

$ gzip -d main.c.gz

Check the compression ratio with -l:

$ gzip -l main.c.gz
         compressed        uncompressed  ratio uncompressed_name
                118                  79 -49.4% main.c

Graphical alternative: most file managers offer “Compress” from the right-click menu — it uses tar + gzip under the hood.

zip / unzip — archives that compress

zip bundles and compresses in one step. It is the format most compatible with other operating systems.

$ zip -r proj.zip proj

List contents with -l:

$ unzip -l proj.zip
Archive:  proj.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  2026-08-05 22:20   proj/
        5  2026-08-05 22:20   proj/data.txt

Extract:

$ unzip proj.zip

Extract to a specific directory with -d:

$ unzip -q proj.zip -d /tmp/out

-q (quiet) suppresses the per-file listing.

Graphical alternative: right-click → Extract Here, or double-click the zip in the file manager.

xz — stronger compression

xz compresses like gzip but achieves smaller files at the cost of speed. The flags mirror gzip.

$ xz data.txt          # creates data.txt.xz, removes original
$ xz -k data.txt       # keep the original
$ xz -d data.txt.xz    # decompress
$ xz -kf data.txt      # -f overwrites an existing .xz

xz is the format Fedora uses for its packages and Arch uses for its repositories, so you will meet it even if you never run it yourself.