Untar in linux tar gz

How to ungzip or untar tar.gz file? [SOLVED]

You may have encountered the «.gz, .tgz, .tar.gz» file extensions in the Linux operating system before. These extensions may be more familiar when it comes to installing applications from source code and you want to view logs. Because Linux Operating systems prefer these file extensions in the file compression process and they give output.

So, how can we untar tar.gz files? Can we unzip tar.gz files? Which application we can use to perform ungzip operation?

You will find answers to these questions in this article.

Install tar and gzip/gunzip(ungzip) Package

To use gunzip, the gzip package is installed. In fact, tar and gzip packages come pre-installed on many operating systems. Some operating systems give packages like «sudo», «gnome» as dependent packages. Therefore, it may not be possible to remove them. So how to install tar and gzip(gunzip) packages? You can find the answer to this question below.

Install to Redhat based operating systems(Fedora, Centos, AlmaLinux, Rocky Linux, etc):

sudo dnf -y install gzip tar
sudo yum -y install gzip tar

Install to Debian-based operating systems (Ubuntu, Pardus, Linux Mint, Kali Linux, etc.):

sudo apt install gzip tar -y
sudo apt-get install gzip tar -y

Install to on openSUSE Leap:

sudo zypper install gzip tar

We learned how to install packages according to Linux distributions. We have 2 ways to open tar.gz files (tar, gunzip). Let’s make it more understandable with examples.

Method-1: Untar tar.gz files using tar command

Extract all content with tar command, extract a specific file and list the files without extracting them.

Example-1: Extract tar.gz or .gz archive

To extract a tar.gz or gz archive we need to use following set of arguments.

  • -x is to extract the tar file
  • -z is used to perform gunzip or extract .gz archive
  • -v is to get verbose output
  • -f to provide the archive file

Here I have a sample archive, let us try to extract it:

~]# tar -xzvf example.tar.gz myssh hy.txt infra.manifest

As you can see, the contents of the archive are successfully extracted to the current folder.

Example-2: Extract the content into a different directory or path

We saw in the last example that by default tar will extract content into the current folder. If we want to specify the destination directory where the content should be extracted and kept then we need to use -C or —directory argument.

Here you can see that we are extracting the content of our archive into /tmp/temp.Jsuv directory instead of the current path.

~]# tar -xzvf example.tar.gz -C /tmp/temp.JsUv/ myssh hy.txt infra.manifest ~]# ls -l /tmp/temp.JsUv/ total 24 -rw-------. 1 root root 51 Oct 20 12:45 hy.txt -rw-------. 1 root root 15254 Aug 23 08:06 infra.manifest -rw-------. 1 root root 3618 Oct 27 09:18 myssh

Example-3: Extract multiple tar.gz archives recursively

Here I have a wrapper archive which internally contains 2 nested gzip archives.

~]# tar -tzvf wrapper_archive.tar.gz -rw------- root/root 7471 2022-10-27 09:19 test/example1.tar.gz -rw------- root/root 530 2022-10-27 09:26 test/example2.tar.gz

To extract the wrapper and the nested archives we have to use the following command:

~]# tar --to-command='tar -xzvf -' -xzvf wrapper_archive.tar.gz test/example1.tar.gz myssh hy.txt infra.manifest test/example2.tar.gz test.yaml testscript.py ~]# ls -l total 44 -rw-------. 1 root root 51 Oct 20 12:45 hy.txt -rw-------. 1 root root 15254 Aug 23 08:06 infra.manifest -rw-------. 1 root root 3618 Oct 27 09:18 myssh -rw-------. 1 root root 373 Oct 21 12:11 testscript.py -rw-------. 1 root root 328 Oct 10 10:35 test.yaml -rw-------. 1 root root 8215 Oct 27 09:29 wrapper_archive.tar.gz 

Example-4: Extract specific file(s)

To extract a specific file we have to provide the file name which we need to extract from the archive. Use the following syntax:

Читайте также:  Линукс минт какую оболочку выбрать

But to be able to know the filename, we must check the content of the archive. So we can list the content of the archive without actually extracting everything by using -tz argument where -t is for —list i.e. list the contents of the archive.

# tar -tzvf example.tar.gz -rw------- root/root 3618 2022-10-27 09:18 myssh -rw------- root/root 51 2022-10-20 12:45 hy.txt -rw------- root/root 15254 2022-08-23 08:06 infra.manifest

Now that we know the content of our archive, we can extract myssh file from this archive without extracting other contents.

~]# tar -xzvf example.tar.gz myssh myssh ~]# ls -l myssh -rw-------. 1 root root 3618 Oct 27 09:18 myssh 

Similarly we can extract more than one files at a time by specifying the list of files separated by comma

~]# tar -xzvf example.tar.gz myssh hy.txt myssh hy.txt ~]# ls -l myssh hy.txt -rw-------. 1 root root 51 Oct 20 12:45 hy.txt -rw-------. 1 root root 3618 Oct 27 09:18 myssh 

Example-5: List files from the archive without extraction

We have briefly touched this point in our previous examples. Sometimes you may not need the file to open, you may want to check it and open it later. Run the below command in the terminal to see the contents of a tar.gz file without extracting it:

foc@fedora:~/unzip_folder$ tar -tf example_folder.tar.gz example_folder/ example_folder/example_file1 example_folder/example_file3 example_folder/example_file4 example_folder/example_file5 example_folder/example_file2

Method-2: Unzip .gz archive using gunzip command

Let’s do the steps we mentioned above with gunzip.

Example-1: Extract .gz archive content

We can use -d or —decompress or —uncompress to extract a .gz archive:

~]# gunzip -dv script.sh.gz script.sh.gz: 80.0% -- replaced with script.sh ~]# ls -l total 12 -rw-------. 1 root root 8618 Oct 27 10:16 script.sh 

Example-2: Extract multiple .gz archives recursively

To extract multiple gz archives we can place them inside a directory and then use -r or —recursive to perform the extraction recursively.

temp.JsUv]# ls -l total 20 -rw-------. 1 root root 210 Oct 27 10:20 post-init.log.gz -rw-------. 1 root root 1751 Oct 27 10:20 script.sh.gz temp.JsUv]# cd .. tmp]# gunzip -r temp.JsUv/ tmp]# ls -l temp.JsUv/ total 16 -rw-------. 1 root root 286 Oct 27 10:20 post-init.log -rw-------. 1 root root 8618 Oct 27 10:20 script.sh

As you can see, both our gz archives are extracted.

Читайте также:  Backup exec for linux

Example-3: List files inside gz archive without extraction

View gunzip file content without extracting. The -l parameter shows the compressed file contents without extracting:

# gunzip -l post-init.log.gz compressed uncompressed ratio uncompressed_name 210 286 37.8% post-init.log

Summary

File compression and extraction is part of system administration. You must perform these operations using the applications that come with the operating system itself. This is the most stable and safe method.

For more detailed information about these commands, you can visit the manual pages.

You can get help locally with the —help command:

foc@fedora:~/unzip_folder$ tar --help Usage: tar [OPTION. ] [FILE]. GNU 'tar' saves many files together into a single tape or disk archive, and can restore individual files from the archive. Examples: tar -cf archive.tar foo bar # Create archive.tar from files foo and bar. tar -tvf archive.tar # List all files in archive.tar verbosely. tar -xf archive.tar # Extract all files from archive.tar. Main operation mode: -A, --catenate, --concatenate append tar files to an archive -c, --create create a new archive --delete delete from the archive (not on mag tapes!) -d, --diff, --compare find differences between archive and file system -r, --append append files to the end of an archive --test-label test the archive volume label and exit -t, --list list the contents of an archive -u, --update only append files newer than copy in archive -x, --extract, --get extract files from an archive . 
foc@fedora:~/unzip_folder$ gunzip --help Usage: /usr/bin/gunzip [OPTION]. [FILE]. Uncompress FILEs (by default, in-place). -c, --stdout write on standard output, keep original files unchanged -f, --force force overwrite of output file and compress links -k, --keep keep (don't delete) input files -l, --list list compressed file contents -n, --no-name do not save or restore the original name and timestamp -N, --name save or restore the original name and timestamp -q, --quiet suppress all warnings -r, --recursive operate recursively on directories . 

Didn’t find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Источник

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.

tutorial on extracting files using tar.gz from command line

  • 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.

Читайте также:  Linux cpuinfo вытаскиваем информацию

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.

confirming that the file has been compressed

To decompress a file, use the gunzip command:

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

example file has been unzipped

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:

output in the linux terminal when extracting a tar.gz 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

how to unzip a tar.gz file to a specific destination in Linux

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.

Command that extracts all files from a zipped document

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.

Источник

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