Command to extract tar gz in linux

How To Extract .tar.gz Files using Linux Command Line

In this tutorial we can learn how to extract tar.gz files using Linux Command line tools.

A .tar.gz file is nothing, but an archive. It is a file that acts as a container for other files. The tar program provides the ability to create tar archives, as well as various other kinds of manipulation. For example, you can use Tar on previously created archives to extract files, to store additional files, or to update, or list files which were already stored. An archive can contain many files, folders, and subfolders, usually in compressed form using gzip or bzip2 program on Unix operating systems. Initially, tar archives were used to store files conveniently on magnetic tape. The name “Tar” comes from this use; it stands for tape archiver. You need to use the tar command which can create and manipulate archive files in .tar.gz under Unix like operating systems. It is very useful for archiving multiple files together into a single archive file. It allows us to restore files individually. Tar can direct its output to available devices, files, or other programs (using pipes), it can even access remote devices or files (as archives). tar.gz file is actually the product of two different things. Tar basically just packages a group of files into a single file bundle, but doesn’t offer compression on it’s own. To compress tar you’ll want to add the highly effective gzip compression. In this documentation, we can discuss about how to extract the tar.gz files from the command line. For this, open a command-line terminal and then type the following commands to open and extract a .tar.gz file.

Extracting .tar.gz files

1) If your tar file is compressed using a gzip compressor, use this command to uncompress it.

x: This option tells tar to extract the files.

v: The “v” stands for “verbose.” This option will list all of the files one by one in the archive.

z: The z option is very important and tells the tar command to uncompress the file (gzip).

f: This options tells tar that you are going to give it a file name to work with.

Читайте также:  Virtualbox astra linux полный экран

A tarball is a group or archive of files that are bundled together using the tar command and have the .tar file extension.

How To Extract .tar.gz Files using Linux Command Line

2) To uncompress tar.gz file into a different directory, use the command below:

$ tar xvzf file.tar.gz -C /path/to/somedirectory

Where the -C argument is used to specify the path to place the file. By defaults files will be extracted into the current directory. To change the directory, we use -C option.

How To Extract .tar.gz Files using Linux Command Line

3) To extract test.doc file from the file.tar.gz tarball, use the following command:

How To Extract .tar.gz Files using Linux Command Line

4) To view a detailed table of content by listing all files in archive, you can use the following command:

How To Extract .tar.gz Files using Linux Command Line

5) To extract the .gz file without tar, you can use the following command:

How To Extract .tar.gz Files using Linux Command Line

Extracting .tar.bz2 files

This is just about the same as the gzip decompression. The major difference is that the z option has been replaced by the j option.

If your tar file is compressed using a bZip2 compressor, use the following command to extract it.

Where ‘j’ will decompress a bzip2 file.

How To Extract .tar.gz Files using Linux Command Line

If you need any further assistance please contact our support department.

21 Responses to “How To Extract .tar.gz Files using Linux Command Line”

i have total 125 tar.gz files to unzip can you suggest a command to unzip all 125 tar.gz in a single stretch

If you want to extract all the tarballs at once, in one location, use wildcard characters in command. For example, if you want to unzip all the tarballs at once located in the current directory, execute sudo tar -xzvf *.tar.gz» . Let us know if you have more questions.

Источник

How to Extract/Unzip Tar Gz File in Linux

How to Extract (Unzip) Tar Gz File in Linux

In this article, we will explain how to extract tar.gz files using the tar command.

Tar is an abbreviation for tape archive , and it is one of the most commonly used commands for dealing with compressed archive files.

Gz is an abbreviation for gunzip. It is a particular compression algorithm. Most Linux distributions have the tar command pre-installed. The tar program compresses and extracts files using various methods.

Tar supports a broad range of compression methods, including gzip, bzip2, xz, lzip, and others.

Table of Contents

Extract a tar.gz file quickly

In this section, we’ll show you a simple method to extract any tar.gz file. In the later sections, you will get to know the tar command in a little bit more detail.

To extract all the files inside of a tar.gz file, use the -xf flag with the tar command:

Читайте также:  Using kali linux tools

Here, x stands for extract and f stands for the archive file. The tar command detects the compression type automatically and extracts it. You don’t need to specify the file/compression type to extract. For example, you could extract a tar.gz file with the same command.

Alternatively, you can also use the graphical user interface (GUI) instead of the command line. Just right click on the tar gz archive file you want to extract and click on the Extract option.

Basic usage of tar command

The basic syntax of the tar command is as follows:

The tar command has a plethora of options in the help menu. You can access it by typing in tar —help .

We’ll be using the main operation mode most of the time. This mode has some basic options for creating and extracting archives. Below are three of these options:

-c, --create create a new archive -t, --list list the contents of an archive -x, --extract, --get extract files from an archive

You’ve already seen the usage of the -x flag for extracting an archive. Let’s take a look at some other options now.

Listing the contents of a tar gz archive

If you just wanted to take a look at the contents of an archive, you would use the -t or —list flag:

tar -tf compressed_file.tar.gz

We can get more details about the archive using the -v or —verbose flag with the command. The output will include file/folder details such as owner, permissions, etc. Let’s see it in action:

tar -tf compressed_file.tar.gz
-rw-r--r-- root/root 3153920 2021-10-15 21:55 file -rw-r--r-- edxd/edxd 1048576 2021-10-15 21:54 file.txt -rw-r--r-- root/root 2097152 2021-10-15 21:54 file.log

As you can see, the file permissions and owner along with the file size is shown in the output.

Extract specific files/folders from a tar gz archive

Imagine you need a specific file from a large archive. In this case, you might want to only extract that specific file from the archive. This can be done by simply specifying the filename (and file path) followed by the extract command.

Let’s see how to extract only the required files:

Check what files are in the working directory with the ls command:

compressed_file.tar.gz file.log.save file.tar.gz tutorial.firstpage.php

I have already shown you the content of the compressed_file.tar.gz in the previous section (listing files). Now let’s extract file and file.log from this archive. We’ll be using the following command:

tar -xf compressed_file.tar.gz file file.log

Here, we used the -xf flag with the command to extract the compressed_file.tar.gz archive. We also mentioned file and file.log indicating which files to extract. Now let’s check our working directory again with the ls command:

compressed_file.tar.gz file file.log file.log.save file.tar.gz tutorial.firstpage.php

As you can see, the file and file.log have been extracted from the compressed_file.tar.gz archive. You can also extract specific directories/folders by using this method. The command would look like the following:

tar -xf archive.tar.gz dir1 dir2 dir3…

Here, dir1, dir2, and dir3 are the names of the directories/folders you want to extract from the archive.

Читайте также:  Как включить wifi astra linux

Use wildcards for specific file extensions

Let’s say you want to extract all the files that have the same extension from inside of an archive. In this case, you could use a wildcard like *.extension format. Here, you can use your specific extension. For example, if you wanted to extract all the text files, you would use *.txt . Let’s see this in action:

First, let’s take a look at our current directory:

compressed_file.tar.gz file.tar.gz test.tar.gz

Let’s take a look at the files inside the test.tar.gz archive:

file.log tutorial.firstpage.php num.txt pr_ex_creator.txt

Now, we have two text files that end in .txt extension. Let’s extract these two files with the *.txt wildcard. We also need to use the —wildcards flag to enable the wildcard option:

tar -xf test.tar.gz --wildcards *.txt

Now let’s check our current directory to see if it worked:

compressed_file.tar.gz file.tar.gz num.txt pr_ex_creator.txt test.tar.gz

From the output, we can see that those two text files were extracted. This is how you can use wildcards to extract all the files with a specific extension.

Change the location of extraction

If you want to change the location where the extracted files will occupy, you can use the -C flag and specify the location of your choice. Here’s an example of how to do it:

tar -xf compressed_file.tar.gz -C /home/new/

Here, after the -C flag, I’ve specified the path for saving the extracted files. The path /home/new/ is marked in color. Let’s take a look at the contents of the directory:

As you can see, the compressed_file.tar.gz file extracted in the /home/new/ location.

Conclusion

We demonstrated how to extract a tar.gz archive in this lesson. In summary, you can extract the archive with the tar command with the -xf parameter. When possible, utilize the graphical user interface if you’re still unclear how to use the tar command.

Some examples may be found in the tar command’s help menu. If you have any queries, please leave them in the comments section.

Источник

Оцените статью
Adblock
detector