Create and extract archives

The tar (short for tape archive) command is used to create an archive by combining files and directories into a single file. Tarballs (archive files created with tar and usually compressed with gzip or bzip2) are often used to distribute software packages in the Linux world, so you might encounter them while working with Raspbian. These software packages need to be decompressed and extracted before they can be installed.

Create an archive

To archive and compress files textfile1.txt and textfile2.txt into a tarball file called tar-example.tgz, use the tar cvfz tar-example.tgz textfile1.txt textfile2.txt command:

tar command create archive

Let’s break down the command:

  • tar – invokes the command
  • c – creates an archive file
  • v – displays detailed information
  • f – specifies the archive’s file name
  • z – filters the archive through gzip for compression
  • tar-example.tgz – the name of the archive
  • textfile1.txt, textfile2.txt – files that will be included in the archive

Extract an archive

To extract the archive created above, use the tar xvfz tar-example.tgz command:

tar command extract archive

The x option extract the content of the archive.

Geek University 2021