How To Extract - Unzip Tar - GZ Files From Linux Command Line

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

9/18/21, 4:42 PM How To Extract / Unzip tar.

gz Files From Linux Command Line

How to Extract or Unzip tar.gz Files from Linux


Command Line

November 14, 2019


LINUX

Home » DevOps and Development » How to Extract or Unzip tar.gz Files from Linux
Command Line

Introduction

A tar.gz file contains several compressed files to save storage space, as well as
bandwidth during the downloading process. The .tar file acts as a portable container for
other files and is sometimes called a tarball. The .gz part of the extension, stands for
gzip, a commonly-used compression utility.

In this guide you will learn how to extract or unzip files from tar.gz files using command-
line in Linux.

https://phoenixnap.com/kb/extract-tar-gz-files-linux-command-line 1/7
9/18/21, 4:42 PM How To Extract / Unzip tar.gz Files From Linux Command Line

Prerequisites

Access to a command-line/terminal window


The tar utility (included by default)
The gzip utility (included by default)

Extracting tar.gz Files in Linux


Using gzip Utility
Gzip by default, extracts the file in the current directory. In this example the file is located
in the Documents directory.

Below, we have used the file named test.txt. Use the name of the file you want to
compress instead.

to compress a single file with gzip enter the command in your terminal window:

gzip test.txt

After zipping the file, enter the command ls to confirm that the file has been compressed.
The output confirms that the file now has a .gz extension.

To decompress a file, use the gunzip command:

https://phoenixnap.com/kb/extract-tar-gz-files-linux-command-line 2/7
9/18/21, 4:42 PM How To Extract / Unzip tar.gz Files From Linux Command Line

gunzip test.txt

Again, use the ls command to confirm the extension of the file.

To compress all the .txt files in a particular directory, type in:

gzip *.txt

The * sign is a wildcard, which means “any number of any characters.” This command
would work on any (and all) filenames with the extension .txt.

This technique can be used on other file types including gzip.txt, .jpg, and .doc.

When you run gzip on multiple files at once, the system generates a compressed copy of
each file. This can clutter up a directory quickly! Fortunately, there’s another tool to
manage multiple files at once.

Using tar Utility


A tar.gz file is a combination of a .tar file and a .gz file. It is an archive file with several
other files inside it, which is then compressed.

You can unzip these files the same way you would unzip a regular zipped file:

tar –xvzf documents.tar.gz

https://phoenixnap.com/kb/extract-tar-gz-files-linux-command-line 3/7
9/18/21, 4:42 PM How To Extract / Unzip tar.gz Files From Linux Command Line

The basic command is tar, followed by four options:

x – instructs tar to extract the files from the zipped file


v – means verbose, or to list out the files it’s extracting
z – instructs tar to decompress the files – without this, you’d have a folder full of
compressed files
f – tells tar the filename you want it to work on

To list the contents of a .tar file before you extract it, enter:

tar –tzf documents.tar.gz

To instruct tar to put the extracted unzipped files into a specific directory, enter:

tar –xvzf documents.tar.gz –C /home/user/destination

https://phoenixnap.com/kb/extract-tar-gz-files-linux-command-line 4/7
9/18/21, 4:42 PM How To Extract / Unzip tar.gz Files From Linux Command Line

To create a tar.gz file, use the following command:

tar –cvzf documents.tar.gz ~/Documents

Similar to the tar command, it condenses all the content located in the
/home/user/Documents directory into a single file, called documents.tar.gz. The addition
of the –z option is what signals tar to compress the files.

To add multiple files to a tar file, use the command:

tar -cvf documents.tar ~/Documents

This copies the contents of your Documents folder into a single file, called documents.tar.
The options -cvf work as follows:

c – creates a new archive


v – verbose, meaning it lists the files it includes
f – specifies the name of the file

To extract the files from a .tar file, enter:

Dejan Tucakov
tar –xvf documents.tar

Dejan is the Head of Content at phoenixNAP with over 7 years of experience in Web
This command extracts and lists all files from the documents.tar file. The -x option tells
publishing and technical writing. Prior to joining PNAP, he was Chief Editor of several
tar to extract the files.
websites striving to advocate for emerging technologies. He is dedicated to simplifying
complex notions and providing meaningful insight into data center and cloud technology.

https://phoenixnap.com/kb/extract-tar-gz-files-linux-command-line 5/7
9/18/21, 4:42 PM How To Extract / Unzip tar.gz Files From Linux Command Line

You can also use xargs with tar to create a tar.gz archive and populate it with files from
Next you should read
the find command.

SysAdmin,
Web
Servers Note: Some graphical interfaces include a tool for managing tar.gz files
without the command-line. Simply right-click the item you want to

Linux Commands
compress, mouseover compress, and choose tar.gz. You can also right-
Cheat Sheet: With
click a tar.gz file, mouseover extract, and select an option to unpack the
Examples archive.
February 21, 2020

Conclusion
A list of all the
important Linux
commands in one
This tutorial explains how to use the tar tool, the gzip tool, and how to utilize them
place. Find the
together to work with tar.gz files. You are now ready to extract or unzip any tar.gz file.
command you need,
whenever you need it
Was this article helpful?
or... Yes No

READ MORE

SysAdmin
How to Unzip a
ZIP File in Ubuntu
/ Linux
December 2, 2019

Zip files are


ubiquitously used on
all operating system,
including Linux. This
simple guide explains
how to...
READ MORE

Security,
SysAdmin
19 Common SSH
Commands in
Linux With
Examples
https://phoenixnap.com/kb/extract-tar-gz-files-linux-command-line 6/7
9/18/21, 4:42 PM How To Extract / Unzip tar.gz Files From Linux Command Line

August 25, 2019

Secure Shell is an
important protocol for
anyone managing and
controlling remote
machines. This guide
covers...
READ MORE

Security,
Web Servers
How to Set up &
Configure
ModSecurity on
Apache
March 11, 2019

ModSecurity is an
Open-source firewall
application for Apache.
Learn how to Setup &
Configure ModSecurity
on...
READ MORE

 Live Chat
 Get a Quote
 Support | 1-855-330-1509
 Sales | 1-877-588-5918

Privacy Policy GDPR Sitemap © 2021 Copyright phoenixNAP | Global IT Services. All Rights Reserved.

https://phoenixnap.com/kb/extract-tar-gz-files-linux-command-line 7/7

You might also like