How to Extract tar.gz Files in Ubuntu Terminal?

Extracting a compressed .tar.gz file in Ubuntu is a fundamental skill for managing backups, installing software, and handling data archives. This article provides a quick, step-by-step guide on how to use the native terminal tools to unpack these files. You will learn the standard command-line syntax, the meaning behind the specific command flags, and how to extract files to a specific destination directory rather than your current location.

The Standard Extraction Command

The most common and efficient way to extract a .tar.gz file in Ubuntu is by using the tar command. Open your terminal (Ctrl + Alt + T) and run the following command:

tar -xvf filename.tar.gz

Understanding the Command Flags

The tar utility uses specific options (flags) to determine how it processes the file. Breaking down -xvf helps clarify exactly what the terminal is doing:

Note: Older documentation might include a -z flag (e.g., tar -zxvf). While -z explicitly tells tar to decompress the file using gzip, modern versions of Ubuntu automatically detect the compression format, making the z flag optional.

Extracting to a Specific Directory

By default, the tar command extracts all contents into your current working directory. If you want to unpack the files into a different location, use the -C (uppercase C) flag followed by the target path.

tar -xvf filename.tar.gz -C /path/to/destination

Before running this command, ensure that the destination directory already exists, as tar will not create a new folder for you.