← All lessons
Files and permissions · Lesson 7 of 7

Archives and disk space

tar bundles files and folders into one archive, and z compresses it with gzip, giving a .tar.gz file (also written .tgz). The options read as a sentence: c create, x extract, t list, z gzip, v verbose (show each file), and f followed by the archive's name.

$ tar czf site-backup.tar.gz site/     # create, gzip, file
$ tar tzf site-backup.tar.gz           # list what's inside
$ tar xzf site-backup.tar.gz           # extract here
$ tar xzf site-backup.tar.gz -C /tmp   # extract into /tmp

f is followed by the archive's name, so keep it last in the bunch: tar -cfz notes.tar.gz notes makes an archive called z. .tar.xz and .tar.bz2 use other compressors; modern tar works out which when extracting, so tar xf alone is fine.

.zip files, common from Windows, use zip -r photos.zip photos and unzip photos.zip.

df -h shows each mounted disk's size, how much is used and how much is free. du -sh folder shows how much one folder takes, and du -sh * shows every item in the current folder, which is the quickest way to find what's filling a disk.

$ df -h /
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1        40G   31G  7.2G  82% /
$ du -sh /var/log
1.3G    /var/log

Practice

Make a gzipped tar archive called notes.tar.gz of the folder notes.

More info

Quick reference

tar czf a.tar.gz dirCreate a gzipped archive.
tar xf a.tar.gz [-C dir]Extract (into a folder).
tar tf a.tar.gzList the contents.
zip -r a.zip dir, unzip a.zipZip files.
df -hFree space on each disk.
du -sh dir, du -sh *Space one folder uses; each item here.

The other Linux courses