1 of 3

Archives and Remote LoginLesson 1 of 3

Archives and compression with tar

Bundle a directory into one file, compress it with gzip, bzip2 or xz, and restore it to the right place.

  • tar
  • gzip
  • gunzip
  • bzip2
  • bunzip2
  • xz
  • tar -t
  • tar -x
  • tar -C

40 min5 exercises

After this lesson you can

  • Create, list and extract tar archives with and without compression.
  • Pick gzip, bzip2 or xz and know the trade-off between speed and size.
  • Extract into a chosen directory, extract one file, and avoid archives that unpack into a mess.

The idea

tar (“tape archive”) bundles many files and their permissions, owners and timestamps into one file. It does not compress by itself; compression is a second step that tar can run for you. The three common compressors trade time for size:

OptionCompressorExtensionCharacter
-zgzip.tar.gz, .tgzFast, good enough, everywhere
-jbzip2.tar.bz2Slower, smaller
-Jxz.tar.xzSlowest, usually smallest

The three verbs to learn are create, table of contents (list) and extract. Add f and the archive’s file name, and v for a file list as it works. On the exam the usual mistake is the order of arguments after -f: the archive name must come immediately after it.

Try it

mkdir -p ~/lab/data && cd ~/lab
echo one > data/a.txt; echo two > data/b.txt; echo three > data/c.txt
tar -cvf data.tar data
tar -tf data.tar
data/
data/b.txt
data/c.txt
data/a.txt
data/
data/b.txt
data/c.txt
data/a.txt

The first four lines come from -v (verbose, while creating) and the last four from listing. tar stores files in the order the file system returns them, so yours may differ.

tar -czf data.tar.gz data
tar -cJf data.tar.xz data
ls -l data.tar*
-rw-r--r--. 1 alex alex 10240 Nov  3 10:15 data.tar
-rw-r--r--. 1 alex alex   186 Nov  3 10:15 data.tar.gz
-rw-r--r--. 1 alex alex   228 Nov  3 10:15 data.tar.xz

Plain tar pads to blocks of 10 KiB, so even three tiny files take 10240 bytes. For a tiny input xz’s fixed overhead makes it larger than gzip. Try the three compressors on /usr/share/doc in the exercises to see the real difference on bigger data.

mkdir -p ~/lab/restore
tar -xf data.tar -C ~/lab/restore
tar -xf data.tar -C /tmp data/b.txt

Compressing single files

gzip file replaces file with file.gz; gunzip file.gz reverses it. The same pattern applies to bzip2/bunzip2 and xz/unxz. Use -k to keep the original, and zcat, bzcat, xzcat to read a compressed file without unpacking it.

seq 1 100000 > ~/lab/numbers.txt
gzip -k ~/lab/numbers.txt
zcat ~/lab/numbers.txt.gz | head -2
ls -l ~/lab/numbers.txt*
1
2
-rw-r--r--. 1 alex alex 588895 Nov  3 10:20 /home/alex/lab/numbers.txt
-rw-r--r--. 1 alex alex 215169 Nov  3 10:20 /home/alex/lab/numbers.txt.gz

Going further

tar detects the compression when extracting, so tar -xf archive.tar.xz works without -J. --exclude='*.log' skips files, -p preserves permissions when you extract as root, and --selinux --xattrs keep SELinux labels and extended attributes, which you want when you back up system directories. A .zip file needs zip and unzip instead.

Practice

0 of 5 done

Do these on your own system. They get harder. Check each result, then tick it off.

  1. Warm-up

    Create an archive and read it back

    Create ~/lab/data/ with three small text files. Archive the directory to ~/lab/data.tar without compression, then list the archive's contents without extracting it.

    Check your result

    tar -tf ~/lab/data.tar prints data/ and the three files, with names starting data/.

    Hint

    -c create, -f file name, -t list.

    Solution
    mkdir -p ~/lab/data && cd ~/lab
    echo one > data/a.txt; echo two > data/b.txt; echo three > data/c.txt
    tar -cf data.tar data
    tar -tf data.tar
  2. Practice

    Compare gzip, bzip2 and xz

    Archive /usr/share/doc three times into ~/lab/doc.tar.gz, ~/lab/doc.tar.bz2 and ~/lab/doc.tar.xz, using the matching tar option each time. Show the three sizes with ls -lh and say which is smallest.

    Check your result

    ls -lh ~/lab/doc.tar.* shows three files. The .xz file is normally the smallest and the .gz the largest.

    Hint

    -z gzip, -j bzip2, -J xz. Errors about unreadable files can be ignored: add 2>/dev/null.

    Solution
    cd ~/lab
    tar -czf doc.tar.gz  /usr/share/doc 2>/dev/null
    tar -cjf doc.tar.bz2 /usr/share/doc 2>/dev/null
    tar -cJf doc.tar.xz  /usr/share/doc 2>/dev/null
    ls -lh doc.tar.*
  3. Practice

    Extract into another directory

    Extract data.tar into a new directory ~/lab/restore so that a.txt, b.txt and c.txt appear directly under ~/lab/restore/data/. Then extract only data/b.txt into /tmp.

    Check your result

    ls ~/lab/restore/data lists the three files. cat /tmp/data/b.txt prints two.

    Hint

    -C dir changes to that directory before extracting. Name the member to extract only it.

    Solution
    mkdir -p ~/lab/restore
    tar -xf ~/lab/data.tar -C ~/lab/restore
    ls ~/lab/restore/data
    tar -xf ~/lab/data.tar -C /tmp data/b.txt
    cat /tmp/data/b.txt
  4. Practice

    Archive without the leading path

    Create etc-backup.tar.gz containing /etc/hostname and /etc/hosts so that listing it shows etc/hostname and etc/hosts (relative), not /etc/.... Explain why relative names are safer.

    Check your result

    tar -tzf etc-backup.tar.gz shows names without a leading slash. tar prints Removing leading '/' from member names when it strips them for you.

    Hint

    By default GNU tar removes the leading / for you. -C / etc/hostname etc/hosts makes the relative form explicit.

    Solution
    cd ~/lab
    tar -czf etc-backup.tar.gz -C / etc/hostname etc/hosts
    tar -tzf etc-backup.tar.gz
  5. Challenge

    Back up, compare, restore

    Back up your whole ~/lab directory to /tmp/lab-backup.tar.xz. Verify the archive reads without errors, delete ~/lab/data, then restore only that directory from the archive and prove the files are back.

    Check your result

    tar -tJf /tmp/lab-backup.tar.xz > /dev/null exits with status 0 (echo $? prints 0). After the restore ls ~/lab/data shows the three files again.

    Hint

    Archive from your home so names start with lab/. To restore one directory name it exactly as it appears in the listing.

    Solution
    cd ~
    tar -cJf /tmp/lab-backup.tar.xz lab
    tar -tJf /tmp/lab-backup.tar.xz > /dev/null; echo $?
    rm -r ~/lab/data
    tar -xJf /tmp/lab-backup.tar.xz -C ~ lab/data
    ls ~/lab/data