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.gzUnderstanding 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:
-x(Extract): This tellstarto extract the contents from the archive.-v(Verbose): This optional flag lists the files in the terminal as they are being extracted, which is helpful for tracking progress.-f(File): This informs the utility that the next argument is the specific archive file you want to operate on. This flag must always come last in the group.
Note: Older documentation might include a
-zflag (e.g.,tar -zxvf). While-zexplicitly tellstarto decompress the file usinggzip, modern versions of Ubuntu automatically detect the compression format, making thezflag 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/destinationBefore running this command, ensure that the destination directory
already exists, as tar will not create a new folder for
you.