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

A directory cannot be emailed, and it cannot be downloaded either, because network transfer and most storage tools operate on single files. tar exists to solve exactly 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 a developer downloads, whether source-code releases, datasets, or Linux software, arrives as a .tar.gz: a tar archive compressed with gzip, a compression program that shrinks the file for transfer.

Every form takes -f archive-name, which declares that the next word is the archive file:

CommandAction
tar -cf out.tar dircreate an archive from dir
tar -tf out.tarlist the contents without extracting
tar -xf out.tarextract it
tar -czf out.tar.gz dircreate and compress with gzip
tar -xzf out.tar.gzdecompress and extract

Listing before extracting is the professional reflex. -t shows exactly what is about to land in the working directory, while there is still time to change your mind.

tar -czf logs.tar.gz logs/ logs/ mon.log tue.log a tree of files c pack logs.tar one file, structure kept z gzip logs.tar.gz smaller, sendable tar -xzf rebuilds the tree exactly -t lists the archive instead, so you can see the contents before extracting
How tar and gzip combine. A directory tree containing mon.log and tue.log is packed by the c flag into a single tar file that preserves the structure, then the z flag compresses that into a smaller logs.tar.gz suitable for sending. A dashed return arrow shows tar -xzf rebuilding the original tree, and -t lists the contents without extracting.

Pack, inspect, delete, restore

A two-file site/ directory becomes site.tar, -t lists what went in, and then the original is deleted and rebuilt from the archive. The | sort is there because tar lists entries in filesystem order rather than alphabetically.

mkdir site
echo "<h1>home</h1>" > site/index.html
echo "body { margin: 0 }" > site/style.css
tar -cf site.tar site
tar -tf site.tar | sort
rm -r site
tar -xf site.tar
ls site

Output

site/
site/index.html
site/style.css
index.html
style.css

The output is two reports stacked together. The first three lines are the archive's table of contents, which includes the site/ directory entry itself, and the last two lines are ls proving the extraction rebuilt the directory and both files after rm -r had removed them.

curl: fetch things from the network

curl URL makes a network request and prints the response, which is the same fetch a browser performs minus the rendering. It is how scripts download anything: grab a release, extract it with tar -xzf, and carry on. These commands need internet access, so they are shown here 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 comes up constantly in install instructions. The pattern curl ... | bash pipes a script straight from the internet into your shell. It works, since that is unit 5's pipe doing its ordinary job, but it executes code that nobody has read. On any machine you care about, download first, read it, then run it.

To see what is inside a downloaded release.tar.gz without extracting it, the command is tar -tzf release.tar.gz. The -t lists the archive's contents, -z handles the gzip compression layer, and -f names the file to read.

The near misses are worth knowing, because two of them are destructive. -x would extract immediately, scattering files into the working directory sight unseen, and -c would try to create an archive at that name, overwriting the download. curl is unrelated here, since it fetches from URLs rather than reading local files.

Creating a compressed archive and then inspecting it uses the same flags with one letter changed. Here a logs/ directory with two files is packed into logs.tar.gz, and the listing is piped through sort for a predictable order.

mkdir logs
echo "day one" > logs/mon.log
echo "day two" > logs/tue.log
tar -czf logs.tar.gz logs
tar -tzf logs.tar.gz | sort

Output

logs/
logs/mon.log
logs/tue.log

Reading the flag groups

  • tar -czf logs.tar.gz logs combines three jobs: c creates, z compresses with gzip, and f names the archive that follows.
  • tar -tzf logs.tar.gz swaps c for t, so the same archive is listed rather than written.
  • The logs/ line in the output is the directory entry itself, which is what allows the extraction to recreate the folder structure and not just the two files.

A backup.tar.gz from a teammate extracts into the current directory with tar -xzf backup.tar.gz.

Each letter carries one job: x extracts, z handles the gzip layer, and f says the next word is the archive file. Modern versions of tar also detect compression automatically, so tar -xf backup.tar.gz works just as well, though spelling out the z costs nothing and makes the intent obvious to anyone reading the script later.