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.
targzipgunzipbzip2bunzip2xztar -ttar -xtar -C
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:
| Option | Compressor | Extension | Character |
|---|---|---|---|
-z | gzip | .tar.gz, .tgz | Fast, good enough, everywhere |
-j | bzip2 | .tar.bz2 | Slower, smaller |
-J | xz | .tar.xz | Slowest, 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.
- Warm-up
Create an archive and read it back
Create
~/lab/data/with three small text files. Archive the directory to~/lab/data.tarwithout compression, then list the archive's contents without extracting it.Check your result
tar -tf ~/lab/data.tarprintsdata/and the three files, with names startingdata/.Hint
-ccreate,-ffile name,-tlist.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 - Practice
Compare gzip, bzip2 and xz
Archive
/usr/share/docthree times into~/lab/doc.tar.gz,~/lab/doc.tar.bz2and~/lab/doc.tar.xz, using the matching tar option each time. Show the three sizes withls -lhand say which is smallest.Check your result
ls -lh ~/lab/doc.tar.*shows three files. The.xzfile is normally the smallest and the.gzthe largest.Hint
-zgzip,-jbzip2,-Jxz. Errors about unreadable files can be ignored: add2>/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.* - Practice
Extract into another directory
Extract
data.tarinto a new directory~/lab/restoreso thata.txt,b.txtandc.txtappear directly under~/lab/restore/data/. Then extract onlydata/b.txtinto/tmp.Check your result
ls ~/lab/restore/datalists the three files.cat /tmp/data/b.txtprintstwo.Hint
-C dirchanges 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 - Practice
Archive without the leading path
Create
etc-backup.tar.gzcontaining/etc/hostnameand/etc/hostsso that listing it showsetc/hostnameandetc/hosts(relative), not/etc/.... Explain why relative names are safer.Check your result
tar -tzf etc-backup.tar.gzshows names without a leading slash. tar printsRemoving leading '/' from member nameswhen it strips them for you.Hint
By default GNU tar removes the leading
/for you.-C / etc/hostname etc/hostsmakes the relative form explicit.Solution
cd ~/lab tar -czf etc-backup.tar.gz -C / etc/hostname etc/hosts tar -tzf etc-backup.tar.gz - Challenge
Back up, compare, restore
Back up your whole
~/labdirectory 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/nullexits with status 0 (echo $?prints0). After the restorels ~/lab/datashows 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