How to create and extract archives

Overview

tar stores many files in one archive and can combine with gzip or xz compression. File extensions such as .tar.gz indicate the compression method.

Examples

tar -czf project.tar.gz project/
tar -tzf project.tar.gz
tar -xzf project.tar.gz
tar -xzf project.tar.gz -C ~/restore
tar -cJf project.tar.xz project/

Options

  • -c creates; -x extracts; -t lists
  • -f is followed by the archive filename
  • -z uses gzip; -J uses xz
  • -v lists names while working
  • -C directory changes extraction destination

Safety

List an unfamiliar archive before extraction and unpack it into a new empty directory. Archives can contain paths that overwrite existing files.

Keep the option order understandable and ensure the archive filename immediately follows -f.