The tar command on Linux is often used to create or extract .tar.gz or .tgz archive files, also called "tarballs". Command has integrated compression, It can create a .tar archive and then compress it with gzip (.tar.gz) or bzip2 (.tar.bz2) compression in a single command.

Compress an Entire Directory or a Single File

tar -czvf <name of archive>.tar.gz <path to directory or file>
tar -czvf banks.tar.gz /var/data/banks

Below are the details of the options used:

  • -c: Create an archive.
  • -z: Compress the archive with gzip.
  • -v: Display progress in the terminal while creating the archive, also known as “verbose” mode. The v is always optional in these commands, but it’s helpful.
  • -f: Allows you to specify the filename of the archive.

Compress Multiple Directories or Files at Once

tar -czvf <name of archive>.tar.gz <path to directory or file seperated by space>
tar -czvf banks.tar.gz /var/data/banks /var/data/bank_codes.csv