Skip to the workspace
Pebellslearn by building
Working with Files  ›  Lesson

Archiving and Compressing — tar and gzip

Archiving and Compressing 12 mintar bundles many files into one archive and gzip compresses a file, and tar -czf, -tzf and -xzf create, list and extract compressed archives.

linux / Archiving and Compressing — tar and gzip

Explore each scene at your own pace.

01 · The rule

The rule

Archiving and Compressing — tar and gzip

At the end of the season, Asha wants to back up the whole garden folder as one small file she can email or copy to another machine. Two separate jobs are involved: bundling many files into one, and squeezing that one file so it takes less space. Linux has a tool for each, and one command that does both.

The rule

tar bundles, gzip squeezes; tar -czf does both.

tar packs files and folders into a single archive and keeps their names, folders and permissions. On its own it doesn't make anything smaller. gzip compresses a single file.

$ gzip notes.txt          # replaces notes.txt with notes.txt.gz
$ gunzip notes.txt.gz     # turns it back into notes.txt

Together, from Asha's home directory:

$ tar -czf garden.tar.gz garden/          # create, gzip, into this file
$ tar -tzf garden.tar.gz                  # list what's inside
$ tar -xzf garden.tar.gz                  # extract here
$ tar -xzf garden.tar.gz -C /tmp/restore  # extract into another folder

Each letter is a small instruction:

Letter Means
c create an archive
x extract from one
t list the contents ("table")
z use gzip (.tar.gz or .tgz)
j use bzip2 (.tar.bz2)
J use xz (.tar.xz), smaller but slower
v verbose: print each file name
f the next word is the archive's file name

When extracting, GNU tar can detect the compression type on its own, so tar -xf garden.tar.gz also works.

You'll also meet zip files, especially ones shared with Windows users. zip -r garden.zip garden/ bundles and compresses in one step, and unzip garden.zip unpacks it.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.