Linux › Section B · Basic commands and the directory hierarchy › Topic B.12
Archiving and compressing files
Software, backups and data sets travel as single compressed files such as project.tar.gz. Knowing how tar bundles files, how gzip and xz shrink them, and how a checksum proves a download arrived intact lets you package, unpack and trust files safely.
Bundling and squeezing
Two different jobs are often confused. Archiving packs many files and directories, with their names, permissions and timestamps, into one file; on Linux the standard archiver is tar. Compressing makes a single stream of data smaller by removing redundancy; gzip, xz and bzip2 do this, and they are lossless, so the original comes back bit for bit (Data compression in the Computer Science course explains how). Linux tools usually do one job each and are combined: tar makes the archive, then a compressor shrinks it. The ZIP format, common on Windows, does both at once.
gzip and gunzip
gzip notes.txt compresses the file and replaces it with notes.txt.gz; gunzip notes.txt.gz restores the original and removes the .gz file. Option -k keeps the input file, -l lists the compressed and uncompressed sizes, -c writes to standard output instead of a file, and -9 compresses harder at the cost of speed. zcat file.gz, equivalent to gunzip -c, prints the uncompressed contents without changing anything on disk, which is handy in pipelines such as zcat old.log.gz | grep ERROR. gzip works on single files: given a directory with -r, it compresses each file inside separately rather than combining them.
Creating an archive with tar
tar cvf project.tar project creates an archive of the project directory and everything in it. The letters are options run together: c selects create mode, v (verbose) prints each name as it is added, and f says the next argument is the archive file. The f must be followed directly by the archive name, which is why it usually comes last in the group; if you forget it, tar may try to use a tape drive or standard output. The archive name can also be a dash, meaning standard input or output, so tar can sit in a pipeline. The .tar suffix is a convention, not a rule.
Listing and extracting
Before unpacking an archive from anyone else, list it with tar tf archive.tar, or tar tvf for permissions, owners and sizes. Check that every path starts with the same top-level directory. An archive whose files sit at the top level will scatter them across your current directory, a nuisance sometimes called a tarbomb. Extract with tar xvf archive.tar, or add -C dir to unpack into another directory. tar never deletes the archive after extracting. Option p keeps the exact permissions stored in the archive instead of applying your umask; it is the default when running as root.
Compressed archives
Most archives you download are compressed tar files, named .tar.gz or the shorter .tgz. Work from right to left: gunzip file.tar.gz, then tar xf file.tar. A pipeline avoids the intermediate file: zcat file.tar.gz | tar xf -. GNU tar can also call the compressor itself: z selects gzip, so tar czf backup.tar.gz dir creates a compressed archive and tar tzf backup.tar.gz lists one. When extracting, GNU tar recognises the compression on its own, so tar xf backup.tar.gz works without the z. Remember that two steps still happen, archiving and compression, even when one command does both.
Worked example: backing up a project
These steps were run with GNU tar 1.35 on a directory project holding docs/notes.txt and run.sh. 1. Create the compressed archive: tar czf project.tar.gz project. 2. List it before trusting it: tar tzf project.tar.gz prints project/, project/docs/, project/docs/notes.txt and project/run.sh, all under one top-level directory. 3. Test a restore without touching the original: mkdir restore, then tar xzf project.tar.gz -C restore. 4. Check that restore/project now contains docs and run.sh. 5. Notice the size: the archive was a 10,240-byte tar file before compression and 235 bytes after, because the notes file was highly repetitive.
xz, bzip2 and zstd
Other compressors use the same pattern. xz (files ending .xz) usually produces smaller files than gzip but takes noticeably longer to compress; unxz or xz -d decompresses and xzcat reads. bzip2 (.bz2) sits between them, with bunzip2 to decompress. tar has matching options: J for xz, as in tar cJf src.tar.xz src, and j for bzip2. zstd (.zst) is a newer compressor, designed to be fast at both compressing and decompressing, and is increasingly used by distributions; GNU tar handles it with --zstd. A file ending .Z is a relic of the old compress program; gunzip can still unpack it.
zip and unzip
For swapping files with Windows and macOS users, ZIP is usually the friendliest format, and most distributions package zip and unzip. zip -r site.zip site creates an archive of the site directory, compressing each file as it is added; -r is needed to include the contents of directories. unzip -l site.zip lists the contents, unzip site.zip extracts into the current directory, and unzip site.zip -d dest extracts into dest. Unlike tar, ZIP compresses each file separately, so one damaged entry does not ruin the rest, but similar files compress less well than in a single compressed tar stream.
Checksums: is the file intact?
A checksum is a short fingerprint calculated from a file's contents; change one byte and the fingerprint changes completely. sha256sum file prints a 64-character SHA-256 hash followed by the file name. Projects that distribute installers or ISO images publish these hashes, often in a file named SHA256SUMS. sha256sum -c SHA256SUMS recalculates each listed file's hash and prints OK or FAILED. A matching hash shows the download was not corrupted. It proves authenticity only if you obtained the hash itself from a trustworthy source, such as the project's HTTPS site or a signed file. MD5 and SHA-1 sums still appear but are no longer safe against deliberate tampering.
Worked example: verifying a download
You download distro.iso and the project's SHA256SUMS file into one directory. 1. Make sure SHA256SUMS came from the official HTTPS site, not from a mirror you do not trust. 2. Run sha256sum -c SHA256SUMS. If the list names files you did not download, add --ignore-missing. 3. Read the result: distro.iso: OK means the hash matches. 4. If you see distro.iso: FAILED and a warning that 1 computed checksum did NOT match, the file differs from the published one, so delete it and download again. 5. In a local test, appending a single line to a tested archive was enough to turn OK into FAILED, and the command's exit status changed from 0 to 1.
Common mistakes to avoid
First, expecting gzip to bundle a directory: it compresses single files, so use tar first. Second, forgetting that the name after f is the archive: tar cvf notes.txt todo.txt overwrites notes.txt with an archive of todo.txt. Third, extracting without listing first and scattering files around the current directory. Fourth, forgetting that gzip and gunzip replace the input file unless you add -k. Fifth, trusting a checksum taken from the same untrusted place as the download.
Key takeaways
tar archives; gzip, xz, bzip2 and zstd compress; zip does both. tar modes are c to create, t to list and x to extract, with v for verbose, f for the archive file and -C to choose a destination. z, J and j add gzip, xz and bzip2, and GNU tar detects compression when extracting. gzip replaces its input unless given -k, and zcat reads without unpacking. Always list an archive before extracting it. sha256sum computes a SHA-256 hash and sha256sum -c checks a list, printing OK or FAILED; trust it only if the hash came from a trusted source.
Flashcards
Flashcard
Bundle many files into one / make data smaller
Test your understanding
Question 1 of 5
Which command creates a gzip-compressed archive of the directory photos?
Question 2 of 5
Before extracting a downloaded archive, why is tar tf archive.tar a good idea?
Question 3 of 5
You run gzip report.csv. What is in the directory afterwards?
Question 4 of 5
sha256sum -c SHA256SUMS prints image.iso: OK, but both files came from an unofficial mirror. What does that prove?
Question 5 of 5
Which statement about tar and gzip is correct?
