Matheion

MA-CLI1 · Module 1.12

Archives and compression

Topics: tar · gzip · Archives

Overview

Module 1.12 — Archives and compression

When Sam wants to send the whole tidewatch/ folder to a teammate, a folder of many files is awkward to attach or copy. An archive packs a tree into one file, and compression makes that file smaller. This module teaches the everyday tar commands for creating, listing, and extracting, plus the gzip variants you will meet as .tar.gz and .tgz.

By the end you should be able to:

  • explain why you would bundle a directory tree into one file;
  • create, list, and extract with tar -cf, tar -tf, and tar -xf;
  • add gzip compression with tar -czf and tar -xzf;
  • inspect an archive before extracting it, and extract into a folder of your choice;
  • recognise how extraction can overwrite existing files.

Scroll Core ideas → worked examples → Quiz. Plan on roughly 45–55 minutes.

Concepts

Core ideas — packing a folder into one file

Imagine Sam has finished a week of tidewatch work and wants to hand the whole project to a teammate. The tidewatch/ folder holds notes, scripts, raw data, and processed data: dozens of files across several subfolders. Attaching each one separately would be tedious, and it would be easy to miss something or to lose the folder layout along the way.

The everyday solution is to archive the folder. An archive is one single file that contains many files and the folder structure that holds them. You send or store that one file, and the person on the other end extracts it to get the original tree back.

There is a second, related idea: compression. Compression rewrites the bytes of a file into a more compact form so that it takes up less space. The two ideas are separate, even though they are very often used together:

  • Archiving answers: "How do I bundle many files into one?"
  • Compression answers: "How do I make a file smaller?"

A useful picture: archiving is putting your belongings into one suitcase; compression is sitting on the suitcase to squeeze out the air. You can do the first without the second, and in the terminal the two are usually combined in a single command.


The tar command

On Unix-like systems, the standard archiving tool is tar, whose name comes from "tape archive" (a historical reminder of what it was first used for). Today it works on ordinary files, and you will see it everywhere.

tar is controlled by short option letters. Each letter is a small instruction, and they combine. The ones you need in this module are:

| Letter | Meaning | Mnemonic | |--------|---------|----------| | c | create a new archive | create | | t | list the contents of an archive | table of contents | | x | extract files out of an archive | extract | | f | the file name of the archive follows | file | | z | also run the data through gzip compression | gzip | | v | verbose: print each file name as it is processed | verbose |

You choose exactly one of c, t, or x as the action, and you nearly always add f to say which archive file you mean. The hyphen at the start is customary: tar -cf and tar cf both work on most systems. This module writes the hyphen so the options read like other commands you have learned.

One important detail: the f option takes the next word as the archive name. In tar -cf backup.tar tidewatch/, the f consumes backup.tar as the archive name, and tidewatch/ is then what goes inside it. The safe habit is to put f last in the cluster (-cf, -czf, -xzf) and the archive name immediately after, so there is never any doubt about which word is which.


Creating an archive — tar -cf

cd ~/cli-practice
tar -cf tidewatch_week1.tar tidewatch/

Read it as a sentence: "create an archive in the file tidewatch_week1.tar containing the folder tidewatch/." The command is usually silent when it succeeds. If you want to watch what is going in, add v:

tar -cvf tidewatch_week1.tar tidewatch/
a tidewatch
a tidewatch/notes
a tidewatch/notes/field_log.txt
a tidewatch/data
a tidewatch/data/raw/gauge_a.csv

(The exact wording varies between systems; some print just the file names.)

Three important facts about creating:

  1. Your originals are untouched. tar -c reads the files and writes a new archive. It does not move or remove anything in tidewatch/. Archiving is a copy, not a relocation.
  2. The folder layout is recorded. Because you named tidewatch/ (a relative path), every file inside is stored with a path starting with tidewatch/. When someone extracts it, they get a tidewatch/ folder.
  3. Do not put the archive inside the folder you are archiving. If you ran the command from inside tidewatch/ and told it to create backup.tar there, tar could try to include its own growing output. Create archives next to the folder, in its parent.

The .tar ending is a convention, not a requirement, but follow it. It tells you and everyone else what kind of file this is.


Looking inside — tar -tf

Before you open a package someone sent you, it is wise to look at the label. For archives, the label is the table of contents:

tar -tf tidewatch_week1.tar
tidewatch/
tidewatch/notes/
tidewatch/notes/field_log.txt
tidewatch/scripts/
tidewatch/scripts/sync_readings.sh
tidewatch/data/
tidewatch/data/raw/
tidewatch/data/raw/gauge_a.csv

Nothing is extracted and nothing on disk changes; -t is read-only, like the viewing commands in module 1.6. This listing tells you where the files will land when extracted, which is the most important thing to know in advance. It answers two questions:

  • Is everything inside a single top-level folder (like tidewatch/)? Good. Extraction will create one tidy folder.
  • Or are there many loose files at the top level? Then extracting into your current folder would scatter them everywhere, mixed with whatever is already there.

This is why the rule of this module is inspect before you extract.


Extracting — tar -xf

tar -xf tidewatch_week1.tar

This reads the archive and recreates its files and folders in your current directory. By default tar puts them wherever you are standing, which is exactly why you should be deliberate about where that is.

The better habit is to extract into a folder made for the purpose, using the -C option (capital C, "change to this directory first"):

mkdir restored
tar -xf tidewatch_week1.tar -C restored

Now everything lands under restored/, as restored/tidewatch/.... Your working folder stays clean, and you can look around before deciding what to keep. Two cautions: the target folder must already exist (-C will not create it, so mkdir first), and -C goes after the archive name here, as shown.

Together, the three commands form a safe routine:

  1. tar -tf archive.tar — look at what is inside.
  2. mkdir somewhere — prepare an empty destination.
  3. tar -xf archive.tar -C somewhere — unpack there.

Adding gzip compression — .tar.gz and .tgz

A plain .tar file is usually about as large as all its pieces added together. Text-like data such as notes, scripts, and CSV readings compresses very well, often to a fraction of the original size. gzip is a widely used compression tool, and tar can run it for you through one extra letter, z:

tar -czf tidewatch_week1.tar.gz tidewatch/

That is the same create command with a z added, and the archive name now ends in .tar.gz. The name is meaningful: it records that the file is a tar archive that was then gzipped. Many people shorten that to .tgz, which means exactly the same thing. The two spellings are interchangeable; pick one and keep it consistent.

The matching extract and list commands also take the z:

tar -tzf tidewatch_week1.tar.gz
tar -xzf tidewatch_week1.tar.gz -C restored

Remember the pattern: action letter, then z if it is gzipped, then f last: -czf, -tzf, -xzf. On many modern systems tar figures out the compression on its own when extracting or listing, so tar -xf file.tar.gz also works. Writing the z anyway is harmless and keeps your intent clear.

Seeing the size difference

ls -lh tidewatch_week1.tar tidewatch_week1.tar.gz
-rw-r--r--  1 sam  crew   20K  Oct 14 09:30 tidewatch_week1.tar
-rw-r--r--  1 sam  crew  2.1K  Oct 14 09:31 tidewatch_week1.tar.gz

The -h option from module 1.6 prints human-readable sizes. The compressed file is much smaller here because the sample data is repetitive text. Results vary: files that are already compressed, such as many photos and videos, shrink very little.

A word on bare gzip

gzip is also a command of its own. gzip notes.txt replaces notes.txt with a compressed notes.txt.gz, and gunzip notes.txt.gz reverses it. Because it replaces the original rather than copying it, treat it with care. For bundling a folder, the tar -czf route is the cleaner choice because it leaves your originals alone.


Caution — extraction can overwrite

Extracting is not always a harmless step. If a file in the archive has the same path as a file that already exists where you are extracting, tar replaces the existing file with the archived version, usually without asking. Your newer edits can vanish in a moment.

Here is a concrete case. Sam edited tidewatch/notes/field_log.txt this afternoon. A teammate then sends last week's archive. If Sam extracts it in the same folder that holds tidewatch/, the old field_log.txt from the archive quietly overwrites today's work.

A few habits protect you:

  • Extract into an empty folder with -C first, then compare and copy over only what you want.
  • List first with -t so you know which paths are involved.
  • Keep a backup copy of anything valuable before extracting over it (module 1.4's cp is enough).
  • If you must extract in place and want to keep what is already there, many versions of tar accept -k ("keep old files"), which skips files that already exist rather than replacing them. Check man tar on your system before relying on it.

Names that look odd — a short glossary

| File ending | What it is | How to open | |-------------|-----------|-------------| | .tar | archive only, no compression | tar -xf name.tar | | .tar.gz or .tgz | tar archive, gzip-compressed | tar -xzf name.tar.gz | | .gz alone | one gzip-compressed file | gunzip name.gz |

Other formats such as .zip exist too and have their own tools (zip and unzip). The ideas are the same: bundle, optionally compress, inspect, then extract.


Summary

  • An archive bundles a whole folder tree into one file; compression makes a file smaller. They are separate ideas that are often combined.
  • tar -cf name.tar folder/ creates; tar -tf name.tar lists; tar -xf name.tar extracts. The f comes last and is followed by the archive name.
  • Add z for gzip: tar -czf name.tar.gz folder/, tar -xzf name.tar.gz. .tgz is a shorthand for .tar.gz.
  • Inspect before you extract with -t, and extract into a prepared folder using -C.
  • Creating an archive leaves your originals untouched; extracting can overwrite files with the same names, so be deliberate about the destination.

Examples

Worked examples — shipping the tidewatch folder

Sam wants to send the project to a teammate, then later receives an archive back. Each scene gives a situation, the commands, an interpretation, and a takeaway. Sizes and dates on your machine will differ; follow the shape.

First, a small setup you can paste in:

cd ~/cli-practice
mkdir -p tidewatch/notes tidewatch/scripts tidewatch/data/raw
printf 'gauge,time,level_cm\nA,06:00,112\nA,07:00,118\nA,08:00,131\nA,09:00,149\n' > tidewatch/data/raw/gauge_a.csv
printf 'Oct 12: calm morning\nOct 13: surge at gauge A\nOct 14: repaired cable\n' > tidewatch/notes/field_log.txt
printf '#!/bin/sh\necho syncing\n' > tidewatch/scripts/sync_readings.sh

Scene 1 — One folder becomes one file

Scene. Sam creates a plain archive and checks that it exists.

tar -cf tidewatch_week1.tar tidewatch/
ls -l tidewatch_week1.tar
ls tidewatch/
-rw-r--r--  1 sam  crew  10240 Oct 14 09:30 tidewatch_week1.tar
data  notes  scripts

Interpretation. The tar -cf command printed nothing, which is its normal behaviour on success. The new file tidewatch_week1.tar sits beside the folder, not inside it. The last command shows tidewatch/ is exactly as before: archiving copied its contents without moving or removing anything.

Takeaway. tar -cf name.tar folder/ makes a bundle and leaves the original alone. Run it from the parent of the folder.


Scene 2 — Look before you extract

Scene. A teammate sends back an archive called tidewatch_week1.tar. Sam wants to know what it contains before unpacking.

tar -tf tidewatch_week1.tar
tidewatch/
tidewatch/notes/
tidewatch/notes/field_log.txt
tidewatch/scripts/
tidewatch/scripts/sync_readings.sh
tidewatch/data/
tidewatch/data/raw/
tidewatch/data/raw/gauge_a.csv

Interpretation. Every path begins with tidewatch/, so the archive has a single top-level folder. Extracting it will create one tidewatch/ directory and will not scatter loose files across the current folder. Sam also notices that notes/field_log.txt is included, which reminds him that extracting in the current folder would replace his own copy of that file. The listing did not change anything on disk.

Takeaway. tar -tf tells you the destination paths in advance. Check for a single top-level folder, and spot any names that would collide with files you already have.


Scene 3 — Extract into a fresh folder

Scene. To stay safe, Sam unpacks into a dedicated folder instead of the project directory.

mkdir restored
tar -xf tidewatch_week1.tar -C restored
ls restored
ls -R restored/tidewatch
tidewatch
restored/tidewatch:
data  notes  scripts

restored/tidewatch/data:
raw

restored/tidewatch/data/raw:
gauge_a.csv

restored/tidewatch/notes:
field_log.txt

restored/tidewatch/scripts:
sync_readings.sh

Interpretation. Because -C restored told tar to unpack there, the whole tree appeared as restored/tidewatch/, and the working copy at ~/cli-practice/tidewatch/ is untouched. Sam can now compare the two copies, for example with cat and ls -l, and copy over only the files he wants.

Takeaway. The safe routine is list, make a folder, extract with -C. The destination folder must exist before you run tar.


Scene 4 — Adding gzip and comparing sizes

Scene. The teammate has a slow connection, so Sam compresses the archive with gzip.

tar -czf tidewatch_week1.tar.gz tidewatch/
ls -lh tidewatch_week1.tar tidewatch_week1.tar.gz
tar -tzf tidewatch_week1.tar.gz
-rw-r--r--  1 sam  crew   10K  Oct 14 09:30 tidewatch_week1.tar
-rw-r--r--  1 sam  crew  438B  Oct 14 09:34 tidewatch_week1.tar.gz
tidewatch/
tidewatch/notes/
tidewatch/notes/field_log.txt
...

Interpretation. The only change in the create command was the added z, plus the .tar.gz ending on the file name. The compressed archive is dramatically smaller because this sample data is short, repetitive text. The listing with -tzf shows the same contents as before, so nothing was lost; compression is reversible. Had Sam named it tidewatch_week1.tgz, it would be the same kind of file under a shorter name.

Takeaway. Add z for gzip, in all three actions: -czf to create, -tzf to list, -xzf to extract. Real-world savings depend on the data.


Scene 5 — A quiet overwrite, and how to avoid it

Scene. Sam appends a new note to his working log, then, thinking he is only "restoring" a lost file, extracts the old archive in the project's parent folder.

echo 'Oct 15: replaced gauge cable' >> tidewatch/notes/field_log.txt
cat tidewatch/notes/field_log.txt
tar -xzf tidewatch_week1.tar.gz
cat tidewatch/notes/field_log.txt
Oct 12: calm morning
Oct 13: surge at gauge A
Oct 14: repaired cable
Oct 15: replaced gauge cable
Oct 12: calm morning
Oct 13: surge at gauge A
Oct 14: repaired cable

Interpretation. The first cat shows four lines. After the extraction, the same file shows only three: the Oct 15 line is gone. tar found a file with the same path already in place and replaced it with the archived version, without any warning. This is the risk described in the concept section.

Had Sam used the safe routine, nothing would have been lost:

mkdir check_restore
tar -xzf tidewatch_week1.tar.gz -C check_restore
diff check_restore/tidewatch/notes/field_log.txt tidewatch/notes/field_log.txt

The archived copy would have landed in its own folder, and Sam could compare before choosing what to keep. (diff prints the lines that differ; it is a small bonus tool you can explore with man diff.)

Takeaway. Extraction in place can overwrite newer work. List first, extract into an empty folder with -C, and copy over only what you intend to replace.

Practice check — archives and compression

Multiple-choice check from the Matheion app lesson. Pick an answer for each question, then submit.

  1. 1.What is the main reason to put the `tidewatch/` folder into a single archive file?

  2. 2.Which command creates an uncompressed archive named `week1.tar` from the folder `tidewatch/`?

  3. 3.Sam receives `incoming.tar` and wants to see what it holds without unpacking anything. Which command does that?

  4. 4.Which pair of commands matches a gzip-compressed archive being created and then extracted into the folder `restored/`?

  5. 5.What is the relationship between the endings `.tar.gz` and `.tgz`?

  6. 6.Why is it wise to run `tar -tf` and extract with `-C` into an empty folder instead of unpacking straight into your project directory?

  7. 7.Sam creates `tidewatch_week1.tar.gz` with `tar -czf tidewatch_week1.tar.gz tidewatch/`. Afterwards, what is true of the original `tidewatch/` folder?