Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
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.

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.

The shortcut

Build a tar command as one action letter, one squeeze letter, then f last: action (c, x or t) + compression (z, j or J) + f, followed straight away by the archive's name. The order of the other letters doesn't matter, but f must be right before the file name.

Asha backs up and restores her gardenStep 1 of 5
  1. Action

    She's making an archive, so the action is c.

The trap is putting f in the middle, as in tar -cfz garden.tar.gz garden/. Now z is taken as the archive's name, so tar writes an uncompressed archive called z. Keep f last among the letters.

Drills

Answer fast — trust the rule.

quick drill

Which command creates a gzip-compressed archive of the garden folder?

quick drill

How do you see what's inside garden.tar.gz without extracting it?

quick drill

What does gzip notes.txt leave in the folder?

quick drill

Asha types tar -cfz backup.tar.gz garden/. What goes wrong?

Remember

  • tar bundles, gzip squeezes, and tar -czf does both.
  • Build it as an action letter, a squeeze letter, then f last, followed by the archive name.
  • Check an archive with -t before relying on it, and extract into a separate folder with -C.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainBack up the garden

Write the commands to back up the garden folder into a single compressed file, check what is inside it, and restore it into /tmp/restore. Explain what each tar letter does and why f must come last.