Skip to content

The tar command

The tar command packs files and folders into one archive and unpacks it again…

The tar command packs files and folders into one archive and unpacks it again: tar -czf backup.tar.gz folder makes a compressed archive and tar -xzf backup.tar.gz unpacks it.

Taught in Intermediate, lesson 20; more in Advanced

A practice shell in your browser that behaves like bash on Linux for these commands. Nothing you type leaves the page.

Usage

tar -c [-z] [-v] -f ARCHIVE FILE...
tar -x [-z] [-v] -f ARCHIVE [-C DIR] [MEMBER]...
tar -t [-z] [-v] -f ARCHIVE

Pack files into an archive, or unpack one. Packs files and folders into one archive file (a "tarball"), lists what is in one, or unpacks it. Pick one of -c (create), -x (extract) or -t (list), and give the archive’s name with -f. Add -z to compress the archive with gzip as well, which is what a .tar.gz or .tgz file is. When unpacking, tar recognises a compressed archive by itself, so -z is optional there. The dash is optional too: tar czf works like tar -czf.

How to use tar

Make a compressed archive: tar -czf

c is create, z compresses with gzip and f names the archive. Then come the files and folders to pack.

tar -czf projects.tar.gz projects && ls -l projects.tar.gz
-rw-r--r-- 1 learner learner 389 Jan 12 09:01 projects.tar.gz

List what is in an archive: tar -tzf

Add v for details like ls -l.

tar -tzf website-2025.tar.gz
website-2025/
website-2025/index.html
website-2025/about.html
tar -tzvf website-2025.tar.gz
drwxr-xr-x learner/learner   0 2025-12-01 10:00 website-2025/
-rw-r--r-- learner/learner  17 2025-12-01 10:00 website-2025/index.html
-rw-r--r-- learner/learner  18 2025-12-01 10:00 website-2025/about.html

Unpack an archive: tar -xzf

tar unpacks into the current folder; -C picks another one.

tar -xzf website-2025.tar.gz && ls website-2025
about.html  index.html
tar -xzf website-2025.tar.gz -C archive && ls -R archive
archive:
website-2025

archive/website-2025:
about.html  index.html

Unpack one file only

Name it after the archive, exactly as tar -t lists it.

tar -xzvf website-2025.tar.gz website-2025/about.html
website-2025/about.html

An archive without compression

Without z, tar only packs. A small archive is 10240 bytes, because tar writes whole blocks.

tar -cf website.tar website && ls -l website.tar
-rw-r--r-- 1 learner learner 10240 Jan 12 09:01 website.tar

Options

-c, --createmake a new archive
-x, --extractunpack an archive
-t, --listlist what is in an archive
-f FILE, --file=FILEthe archive file (put it right after the f)
-z, --gzipcompress (or uncompress) with gzip: .tar.gz
-v, --verboseprint each file’s name as it goes; with -t, details like ls -l
-C DIR, --directory=DIRunpack into DIR instead of the current folder

More examples

tar -czf projects.tar.gz projects
Pack a folder into a compressed archive
tar -cf website.tar website
An uncompressed archive
tar -czvf backup.tar.gz notes.txt report.txt
Several files, listing each one
notes.txt
report.txt
tar -xzf website-2025.tar.gz -C archive
Unpack it into another folder

Questions about tar

What do the letters in tar -xzvf mean?

x extracts, z uncompresses with gzip, v lists each file as it goes and f says the next word is the archive’s file name. For creating, use c instead of x.

What is a .tar.gz or .tgz file?

A tar archive compressed with gzip. tar packs many files into one; gzip makes that one file smaller. tar -czf makes one and tar -xzf unpacks it.

Do I need the dash in tar -czf?

No. tar czf archive.tar.gz folder works too; the dash-less form is older and still common.

Learn it step by step

Every everyday command on one page, to print or keep open: the Linux commands cheat sheet.