How to create gzipped tar archive command line.

The tar command in Linux is often used to create.Tar. gz or.Tgz archive files, also called “tables.” The most commonly used compression formats in Linux is tar. gz. Creating a tar archive does not compress your files, it just makes them easier to move around as one blob. For compression, you can have tar call Gzip or zip.

Compress an Entire Directory or a Single File

tar -czvf name-of-archive.tar.gz /path/to/directory-or-file
  • -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.

Exclude Directories and Files

For certain scenarios, we need to compress an entire directory, but not exclude certain files and directories. For excluding certain files and directories we can append a --exclude switch for each directory or file you want to exclude. For excluding certain files and directories use the below format.

tar -czvf archive.tar.gz /home/ubuntu --exclude=/home/ubuntu/Downloads --exclude=/home/ubuntu/.somfilename


Extract an Archive

The following command will extract the contents of archive.tar.gz to the current directory.

tar -xzvf archive.tar.gz