EMZETT.
Login

Archives in the terminal: tar, gzip and zip

In short: tar bundles many files into one archive, gzip compresses it; together they make the typical .tar.gz file found everywhere on Linux.

In more detail: On Linux and macOS bundling (tar) and compressing (gzip, xz, zstd) are separate. The result project.tar.gz is also called a tarball.

In Depth

tar: creating and extracting archives

tar -czf project.tar.gz folder/     # create (c=create, z=gzip, f=file)
tar -xzf project.tar.gz            # extract (x=extract)
tar -tzf project.tar.gz            # show contents (t=list)
tar -xzf project.tar.gz -C /target # into another directory
tar -xzf project.tar.gz file.txt   # only one file

v (verbose) shows file names. Mnemonic: create, xtract, test/list, file. Modern tar often detects compression automatically when extracting.

Compression methods

  • gzip (.gz): fast, widespread.
  • bzip2 (.bz2), xz (.xz): smaller but slower. tar -cJf x.tar.xz folder/.
  • zstd (.zst): very fast with a good ratio, modern. Single files: gzip file (replaces it with file.gz), gunzip file.gz, xz file, zstd file. With -k the original stays.

zip and 7z

zip -r archive.zip folder/
unzip archive.zip
unzip -l archive.zip   # contents
7z a archive.7z folder/

zip is cross-platform (Windows, macOS, Linux); tar additionally stores file permissions and owners, hence better for Linux backups. 7z compresses strongly and can encrypt (-p).

Tips and caution

Look at archives before extracting (tar -tzf) so as not to dump uncontrolled numbers of files into a directory (“tar bomb”). Paths with .. or absolute paths in foreign archives are dangerous. Verify with checksums (sha256sum). For backups mind transfer and metadata; rsync or restic are often better suited.

See also: Extracting and creating ZIP files, Backups on Linux, Compression, 7-Zip