What is a tar.gz File and How to Create (Compress) and Extract (Unzip) Tar Gz File

tar.gz File

A tar file is a collection of files enclosed in a single file usually compressed using one of the compression programs (gzip, lzip,...) but in most cases tar files are compressed using gzip(Gzip is a file format and a software application used for file compression and decompression) compression that's where the name tar.gz come from.

Extracting tar.gz File

If you are using a version of Linux or macOS, tar command is usually available and can be used to extract tar.gz files in one command.All what is needed is to use the tar command and the "-x" option to extract the file.

tar -xf archive.tar.gz
  • x: Extract
  • f: specify the file path
  • v: Enable verbose mode to show the names of the extracted files.

Extracting tar.gz into a specified directory

By default tar will extract the selected files into the current folder. In order to change the output location path use the "-C" option.

tar -xf archive.tar.gz -C /path/to/the/folder

Extracting specific files from a tar.gz file

To extarct multiple selected files from the tar file, add the file names into the extract command. you can find the file name contained in the tar by adding the "--list" option to the command.

#Extract files by name
tar -xf archive.tar.gz file file2 file3
#Extract files by regex
tar -xf archive.tar.gz --wildcards '*.xlsx'

Create a tar file

tar.gz file creation is really easy. All you should do is replacing the "x" command to extract with "c" wich stand for create.

tar -cf output.tar.gz /path/to/the/folder/to/compress