How to Create and Extract tar.gz files via the Command Line

Learn how to easily create and extract tar files via the command line.

Creating a tar.gz file

In order to create a tar.gz you will need to open up your terminal and make use of the following schema:

 tar -czvf compressed-file-name.tar.gz/path/to/uncompressed/file 

The flags stand for the following parameters:

  • -c
    create an archive of the inputted file(s)
  • -z
    compress the archive using (g)z(ip)
  • -v
    execute in verbose mode, meaning that the terminal will display its progress in executing the command (this is an optional parameter but can prove helpful in case of error)
  • -f
    implements the naming of the resultant file

Extracting a tar.gz file

If you are using a *nix based operating system, your tar.gz should extract automatically on opening. If not, you can use the following command which simply reverses the creation process:

tar -xzvf file.tar.gz 

Here the parameters have the same meaning as above. The additional -x flag means 'extract'.