- How to create a .tar.gz file on Linux
- 1. Create, list and extract a .tar.gz file
- 2. What is tarball or .tar.gz?
- 3. Create a .tar.gz file
- 3.1 Tar and compress a file
- 3.2 Tar and compress multiple files
- 3.3 Tar and compress multiple files and folders.
- 4. Extract the .tar.gz file
- 4.1 Extract the .tar.gz file to current directory
- 4.2 Extract the .tar.gz file to a specified directory
- 5. References
- How to Extract or Unzip tar.gz Files from Linux Command Line
- Extracting tar.gz Files in Linux
- Using gzip Utility
- Using tar Utility
How to create a .tar.gz file on Linux
A few examples to show the use of tar command to create a .tar.gz file on Linux.
1. Create, list and extract a .tar.gz file
We use -c to create a .tar.gz file.
We use -tf to list the content of a .tar.gz file.
We use -x to extract a .tar.gz file.
$ tar -xvzf example.tar.gz $ tar -xvzf example.tar.gz -C
2. What is tarball or .tar.gz?
On Linux or macOS, there is a tar command to group files and folders into one archive file, aka tarball or a .tar file, and later, we can compress the tarball to reduce the file size, the standard compress algorithm is gzipping or .gz file.
For example, below are two text files.
We can group the above files into one .tar file or tarball.
a.txt b.txt --> tar --> example.tar (100MB)
And we compress the above tar file to reduce the file size.
example.tar --> gzip --> example.tar.gz (50MB)
3. Create a .tar.gz file
Below are some tar options to create a .tar.gz file.
- -c to tar a few files or folders
- -z to gzip compress the tar file
- -f to give a file name
- -v to verbose output (optional)
The final command is tar -cvzf filename.tar.gz .
Below are two text files for testing.
ls -lsa total 16 0 drwxr-xr-x 4 yongmookkim staff 128 May 29 17:41 . 0 drwxr-xr-x 12 yongmookkim staff 384 May 29 17:35 .. 8 -rw-r--r-- 1 yongmookkim staff 6 May 29 17:35 a.txt 8 -rw-r--r-- 1 yongmookkim staff 12 May 29 17:36 b.txt
3.1 Tar and compress a file
Below example tar a single file a.txt and compress it using gzip and output it to example.tar.gz .
$ tar -czf example.tar.gz a.txt $ ls -lsah total 24 0 drwxr-xr-x 5 yongmookkim staff 160B May 29 17:43 . 0 drwxr-xr-x 12 yongmookkim staff 384B May 29 17:35 .. 8 -rw-r--r-- 1 yongmookkim staff 6B May 29 17:35 a.txt 8 -rw-r--r-- 1 yongmookkim staff 12B May 29 17:36 b.txt 8 -rw-r--r-- 1 yongmookkim staff 123B May 29 17:43 example.tar.gz # list the content of the tar.gz file $ tar -tf example.tar.gz a.txt
3.2 Tar and compress multiple files
Below tar command tar multiple files a.txt and b.txt and compress it using gzip and output it to example2.tar.gz .
$ tar -czf example2.tar.gz a.txt b.txt $ ls -lsa total 32 0 drwxr-xr-x 6 yongmookkim staff 192 May 29 17:46 . 0 drwxr-xr-x 12 yongmookkim staff 384 May 29 17:35 .. 8 -rw-r--r-- 1 yongmookkim staff 6 May 29 17:35 a.txt 8 -rw-r--r-- 1 yongmookkim staff 12 May 29 17:36 b.txt 8 -rw-r--r-- 1 yongmookkim staff 156 May 29 17:46 example2.tar.gz $ tar -tf example2.tar.gz a.txt b.txt
3.3 Tar and compress multiple files and folders.
Review the current directory structure.
$ tree . ├── a.txt ├── b.txt └── folderA ├── c.txt └── folderB └── d.txt
We want to tar and compress a.txt file and folderA directory and its subdirectories and subfiles and output it to example3.tar.gz .
$ ls -lsah total 16 0 drwxr-xr-x 5 yongmookkim staff 160B May 29 17:50 . 0 drwxr-xr-x 12 yongmookkim staff 384B May 29 17:35 .. 8 -rw-r--r-- 1 yongmookkim staff 6B May 29 17:35 a.txt 8 -rw-r--r-- 1 yongmookkim staff 12B May 29 17:36 b.txt 0 drwxr-xr-x 4 yongmookkim staff 128B May 29 17:50 folderA $ tar cvzf example3.tar.gz a.txt folderA a a.txt a folderA a folderA/folderB a folderA/c.txt a folderA/folderB/d.txt $ ls -lsah total 24 0 drwxr-xr-x 6 yongmookkim staff 192B May 29 17:54 . 0 drwxr-xr-x 12 yongmookkim staff 384B May 29 17:35 .. 8 -rw-r--r-- 1 yongmookkim staff 6B May 29 17:35 a.txt 8 -rw-r--r-- 1 yongmookkim staff 12B May 29 17:36 b.txt 8 -rw-r--r-- 1 yongmookkim staff 240B May 29 17:54 example3.tar.gz 0 drwxr-xr-x 4 yongmookkim staff 128B May 29 17:50 folderA $ tar -tf example3.tar.gz a.txt folderA/ folderA/folderB/ folderA/c.txt folderA/folderB/d.txt
4. Extract the .tar.gz file
Below are some tar options to uncompress, unzip or untar a .tar.gz file.
- -x to extract the tar file
- -z to gzip uncompress the tar file
- -f to give a file name
- -v for the detail (optional)
The final command to extract the .tar.gz is tar -xvzf filename.tar.gz .
4.1 Extract the .tar.gz file to current directory
By default, the -x will extract the files or folders to the current directory running the command.
$ ls -lsah total 8 0 drwxr-xr-x 3 yongmookkim staff 96B May 29 21:31 . 0 drwxr-xr-x 6 yongmookkim staff 192B May 29 21:31 .. 8 -rw-r--r-- 1 yongmookkim staff 240B May 29 17:54 example3.tar.gz $ tar -xvzf example3.tar.gz x a.txt x folderA/ x folderA/folderB/ x folderA/c.txt x folderA/folderB/d.txt $ ls -lsah total 16 0 drwxr-xr-x 5 yongmookkim staff 160B May 29 21:36 . 0 drwxr-xr-x 6 yongmookkim staff 192B May 29 21:31 .. 8 -rw-r--r-- 1 yongmookkim staff 6B May 29 17:35 a.txt 8 -rw-r--r-- 1 yongmookkim staff 240B May 29 17:54 example3.tar.gz 0 drwxr-xr-x 4 yongmookkim staff 128B May 29 17:50 folderA
4.2 Extract the .tar.gz file to a specified directory
We can add a -C DIR to change to a specified directory and do the extract operation next, which means extract the content of .tar.gz to a specified directory.
The below example extracts the content of example3.tar.gz to this directory /home/mkyong/backup .
$ tar -xvzf example3.tar.gz -C /home/mkyong/backup x a.txt x folderA/ x folderA/folderB/ x folderA/c.txt x folderA/folderB/d.txt $ ls -lsa /home/mkyong/backup total 8 0 drwxr-xr-x 4 yongmookkim staff 128 May 29 21:42 . 0 drwxr-xr-x+ 38 yongmookkim staff 1216 May 29 21:42 .. 8 -rw-r--r-- 1 yongmookkim staff 6 May 29 17:35 a.txt 0 drwxr-xr-x 4 yongmookkim staff 128 May 29 17:50 folderA
5. References
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.
How to Extract or Unzip tar.gz Files from Linux Command Line
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.
- 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:
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:
Again, use the ls command to confirm the extension of the file.
To compress all the .txt files in a particular directory, type in:
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:
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:
To instruct tar to put the extracted unzipped files into a specific directory, enter:
tar –xvzf documents.tar.gz –C /home/user/destination
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:
This command extracts and lists all files from the documents.tar file. The -x option tells tar to extract the files.
You can also use xargs with tar to create a tar.gz archive and populate it with files from the find command.
Note: Some graphical interfaces include a tool for managing tar.gz files without the command-line. Simply right-click the item you want to compress, mouseover compress, and choose tar.gz. You can also right-click a tar.gz file, mouseover extract, and select an option to unpack the archive.
This tutorial explains how to use the tar tool, the gzip tool, and how to utilize them together to work with tar.gz files. You are now ready to extract or unzip any tar.gz file.