Course outline · 0% complete

0/29 lessons0%

Course overview →

Bundles and downloads: tar and curl

lesson 10-3 · ~9 min · 28/29

tar: many files, one file

You can't email a directory, and you can't download one either — network transfer and most storage tools work on single files. tar exists to solve that: it packs a whole directory tree (structure and permissions included) into one file called an archive, and unpacks it again on the other side. Nearly everything you'll download as a developer — source-code releases, datasets, Linux software — arrives as a .tar.gz: a tar archive compressed with gzip, a compression program that shrinks the file for transfer.

The flags (always with -f archive-name, which says "the next word is the archive file"):

  • tar -cf out.tar dir: create an archive from dir
  • tar -tf out.tar: list what's inside without extracting
  • tar -xf out.tar: extract it
  • add -z to compress or decompress with gzip: tar -czf out.tar.gz dir, tar -xzf out.tar.gz

Listing before extracting is the professional reflex: -t shows exactly what's about to land in your working directory, before it does.

Code exercise · bash

Run it. A two-file site/ directory becomes site.tar, -t lists the contents, then we delete the original and resurrect it from the archive. (The | sort is there because tar lists entries in filesystem order.)

curl: fetch things from the network

curl URL makes a network request and prints the response — the same fetch your browser performs, minus the rendering. It's how scripts download anything: grab a release, tar -xzf it, done. The sandbox on this page has no internet access, so these are shown rather than run:

curl https://example.com                 # print a page's HTML
curl -O https://host/data.csv            # -O: save under its own name
curl -fsSL https://host/x.tar.gz -o x.tar.gz   # quiet, fail loudly on errors, save as x.tar.gz

One caution you'll meet in install instructions: the pattern curl ... | bash pipes a script straight from the internet into your shell. It works — that's unit 5's pipe doing its job — but you're executing code you never read. On machines you care about: download first, read it, then run it.

Quiz

You downloaded `release.tar.gz`. Which command shows what's inside WITHOUT extracting it?

Code exercise · bash

Your turn. Create a `logs/` directory holding `mon.log` (containing `day one`) and `tue.log` (containing `day two`). Pack the directory into a compressed `logs.tar.gz`, then list the archive's contents through sort. Expected output: ``` logs/ logs/mon.log logs/tue.log ```

Problem

A teammate sends you `backup.tar.gz`. What single command extracts it into the current directory?