Exercise

Creating and Extracting Archives Exercise

Overview

An archive is one file that holds many files, along with the directory structure, ownership, permissions, and timestamps that a plain copy can lose.

In this hands-on exercise you will bundle the entire /etc directory into a single uncompressed archive, read its contents without unpacking it, extract the whole archive into a directory of its own, and then extract just one file from it.

Host
servera
Run as
root
  1. Bundle all of /etc as root into one uncompressed archive under /tmp

    Reading every file under /etc requires administrative privileges, so connect to servera and become root first.

    ssh student@servera
    sudo -i

    -c creates, -v lists each member as it is added, and -f names the archive file.

    tar -cvf /tmp/etc-backup.tar /etc
  2. Check the size of the archive you produced

    -h prints the size in human-readable units.

    ls -lh /tmp/etc-backup.tar
  3. Read the archive's contents without unpacking it

    -t lists the members. Notice that every path is stored without its leading slash.

    tar -tf /tmp/etc-backup.tar | head -n 20
  4. Unpack the archive into an empty directory of its own

    tar unpacks into the current working directory, so change into a directory of its own first.

    mkdir -p /tmp/etc-extract
    cd /tmp/etc-extract
    tar -xf /tmp/etc-backup.tar
    ls
  5. Confirm the extracted tree looks like the original

    Compare one extracted file against the original. diff printing nothing means the two files are identical.

    ls -l /tmp/etc-extract/etc | head -n 10
    diff /tmp/etc-extract/etc/hosts /etc/hosts
  6. Extract a single file into a fresh directory

    Name the member you want exactly as -t printed it: etc/hosts, not /etc/hosts.

    mkdir -p /tmp/etc-one
    cd /tmp/etc-one
    tar -xf /tmp/etc-backup.tar etc/hosts
    ls -R /tmp/etc-one