Learn how to easily extract the content of any tar.gz file with the command line in Ubuntu or Linux.

TAR files are created by tar, the famous Unix-based utility to package files in a single file to distribute it or just for backup. The TAR file contains multiple files that are stored in an uncompressed format as well with the metadata of the archive. Once it has been packaged inside the TAR file, it's usually compressed using some algorithm, in this case the GNU Zip compression (that's why the file includes the .gz extension).

You will find distributables of many projects, utilities in this format, so after downloading it in your server or local environment, you may be wondering right now how to easily extract its content into the same directory or just store its content into another directory, that's why I will explain to you how to easily extract the content of this type of files easily.

Extract content of tar.gz

If you want to extract the content of the file to the same directory where the tar.gz file is located, you may use the following command:

tar -xvzf file.tar.gz

The -xvzf instruction can be broken down like this:

  • -x : Specifies the tar utility to extract the content from an archive.
  • -v : Specify to verbosely list all the files that are being extracted (output in the terminal).
  • -z : Specify that the content should be decompressed using the GZIP algorithm.
  • -f : This argument specifies the filename of the tar to work with. This must be specified as the last argument and the path should appear immediately after it.

And that's it! Alternatively, if you want to specify a custom directory where the content should be extracted, you can do it with the -C argument to specify tar to change the directory (so the package content will be unpacked there):

tar -xvzf file.tar.gz -C /var/www/some-directory

Happy coding ❤️!

Senior Software Engineer at Software Medico. Interested in programming since he was 14 years old, Carlos is a self-taught programmer and founder and author of most of the articles at Our Code World.