Linux untar all files

How to Untar Files in Linux?

All the Linux distributions offer the “tar (tape archive)” command line tool to create archive files. The “archive” file comprises one or more files and metadata. They are beneficial as they are easily portable and take less storage. Once the archive file is created, it can be untared or extracted using the “tar” command.

This guide provides the possible ways to untar files in Linux using the “tar” command.

The outcomes of this guide are listed below:

  • Untar the “tar” Format Files
  • Untar the “tar.gz” Format
  • Untar a “tar.bz2/.tar.zx/.tar.bz/.tbz/ or .tbz2” Files
  • Untar the Specific File
  • Untar Files Into a Particular Directory

Let’s start with the process to untar files in Linux.

How to Untar the “tar” Format Files in Linux?

The basic syntax to untar the tar archive in Linux is written below:

Syntax:

The above syntax holds the following components:

  • tar: Represents the “tar” utility.
  • x: Helps to extract the archive.
  • f: Tells the archive with the given filename
  • file_name.tar: Denotes the tar archive.

Example:

Let’s use this syntax to untar the “Sample.tar” tar archive file. This file is available in the “notes” directory as shown in the screenshot:

Use the below “tar” command to list down the content of the “Sample.tar” archive file:

To untar the “Sample.tar” archive into its PWD “notes” execute the “tar” command in the following way:

Run the “ls -l” command for the verification of extracted “Sample.tar” archive file:

The content of the “Sample.tar” archive file is extracted or untar into the “notes” directory. The archive file also remains to save in the “notes” directory.

How to Untar the “tar.gz” Format files in Linux?

A tar.gz archive is typically smaller than a regular tar archive because the gzip compression algorithm reduces the size of the file. The generalized syntax to untar the compressed archive is typed below:

The only difference between the syntax is the “z” flag to untar/extract the compressed archive file.

The compressed tar archive has a “tar.gz” extension. In this situation, the “Test.tar.gz” compressed located in the “Extra” directory has the following contents:

Now, type the tar command in the terminal to untar the “Test.tar.gz” archive file:

For the verification execute the “ls-l” command as shown below:

$ ls -l

It is verified that the “Test.tar.gz” is extracted or displaying the “Extra” directory that is compressed in it.

Читайте также:  Linux проверка пароля пользователя

How to Untar a “tar.bz2/.tar.zx/.tar.bz/.tbz/ or .tbz2” File?

The tar archive compressed with “bzip2” follows the “tar.bz2”, “tar.bz”, “tbz2”, and “tbz” extensions at their ends. The generalized syntax to untar the “tar.bz2”, “tar.zx”, “tbz” compressed archives are written below:

The “-j” flag is used to extract the “bzip2” compressed tar archive files.

As the “Extra.tar.tbz” already created compressed tar archive in the “home” directory is displayed in the terminal:

$ tar -jcvf Extra.tar.tbz Directory1

Extract it into the “pwd” directory using the “tar” command:

The above command is successfully executed without any error.

Run the “ls -l” command for the verification:

The output verifies that “Extra.tar.tbz” content is extracted into the “home” directory.

How to Untar the Specific File/Directory in Linux?

The “tar” file also facilitates the Linux user to untar or extract the specific file/directory from the archive files. As the “test.tar” archive file contains the following content:

Use the “tar” command to extract the “FF.txt” directory from the above-shown list:

$ tar -xvf test.tar Dir1/D21/FF.txt -C

It is confirmed that the “FF.txt” text file is extracted into the present working directory. For more verification, use the “ls -l” command:

How to Untar Files Into Particular Directory?

It is not mandatory to untar the files in the same directory in which the archive file is located. However, the user can extract/untar the archive file into a directory. For this purpose, use the “-C” flag with the absolute path of the directory:

For “tar” Format Archive Files:

$ tar -xf file_name.tar -C /path/to/directory/

For “tar.gz” Format Archive Files:

$ tar -zxf file_name.tar -C /path/to/directory/

That’s how you can untar files in Linux.

Conclusion

Linux offers the “tar” command to untar archive files easily. The option “-x” of the “tar” utility is used to perform this task. Moreover, the “tar” command is also useful to untar the compressed archive files with the extension “tar.gz” and other compression formats. This post has described all the possible ways to untar both Files in Linux.

Источник

How to Untar All Files in a Directory: A Comprehensive Guide Using Linux/Unix Commands

Learn how to untar all files in a directory using Linux/Unix command prompt. This guide covers various methods to extract tar archives, including extracting all tar.gz files, extracting multiple files from a tar archive, and extracting multiple tar.gz files with a single tar call.

  • Extracting all tar.gz files in a directory in Unix
  • Extracting multiple files from a tar archive
  • Extracting multiple tar.gz files with a single tar call
  • Extracting tar files to a different or specific directory
  • Untarring multiple files in Windows using Keka
  • Other helpful code examples for untarring all files in a directory
  • Conclusion
  • How do I untar a list of files?
  • How do I extract a tar file from a specific directory?
  • How do I untar multiple files in Windows?
  • How do I untar a file in Linux?

If you are a Linux/Unix user, you may have come across situations where you need to extract multiple tar files in a directory or extract specific files from a tar archive. To accomplish this, you need to use the correct commands and syntax for untarring multiple files in a directory. In this article, we will provide a comprehensive guide to help you extract all tar files in a directory, extract multiple files from a tar archive, extract multiple tar.gz files with a single tar call, extract tar files to a different or specific directory, and even untar multiple files in Windows using Keka.

Читайте также:  System accounting and logging in linux

Extracting all tar.gz files in a directory in Unix

The tar command is used to create and extract tar archives while the gzip command is used to compress and decompress files in Unix. When you encounter a situation where you need to extract all tar.gz files in a directory, you can use the find command with the -execdir option. Here’s an example command:

find . -type f -name '*.tar.gz' -execdir tar -xzvf <> \; 
  • find : searches for files in a directory hierarchy.
  • -type f : specifies that we are searching for files only.
  • -name ‘*.tar.gz’ : specifies the pattern of the files we are looking for.
  • -execdir : specifies that we want to execute a command in the directory where the file is found.
  • tar -xzvf <> \; : extracts the files found by find .

Extracting multiple files from a tar archive

Sometimes, you may need to extract specific files from a tar archive without extracting all the files. To accomplish this, you can use the tar command with the —extract or -x option, followed by the specific files you want to extract. Here’s an example command:

tar -xvf archive.tar file1.txt file2.txt 
  • tar : specifies the tar command.
  • -xvf : specifies the options to extract files from the archive and to display verbose output.
  • archive.tar : specifies the name of the tar archive.
  • file1.txt file2.txt : specifies the files you want to extract from the archive.

Extracting multiple tar.gz files with a single tar call

If you have multiple tar.gz files that you need to extract, you can use the cat command to concatenate them into a single file and then use the tar command to extract the files. Here’s an example command:

cat file1.tar.gz file2.tar.gz | tar -xzvf - 
  • cat : concatenates the files.
  • file1.tar.gz file2.tar.gz : specifies the files to be concatenated.
  • | : pipes the output of the cat command to the input of the tar command.
  • tar : specifies the tar command.
  • -xzvf : specifies the options to extract files from the archive and to display verbose output.
  • — : specifies that the input is coming from the pipe.

Extracting tar files to a different or specific directory

By default, when you extract files from a tar archive, they are extracted to the same directory as the archive. However, you may need to extract the files to a different or specific directory. To accomplish this, you can use the -C option followed by the directory path. Here’s an example command:

tar -xzvf archive.tar.gz -C /path/to/directory/ 
  • tar : specifies the tar command.
  • -xzvf : specifies the options to extract files from the archive and to display verbose output.
  • archive.tar.gz : specifies the name of the tar archive.
  • -C : specifies the directory to extract the files to.
  • /path/to/directory/ : specifies the directory where the files will be extracted.

You can also use the -s parameter to modify the name of the extracted files. This is useful when dealing with older versions of the basename command. Here’s an example command:

tar -xzvf archive.tar.gz -s '/^old/new/' -C /path/to/directory/ 
  • -s ‘/^old/new/’ : specifies that the string old in the file name should be replaced with new .
  • /^old/ : specifies the regular expression to search for.
  • new/ : specifies the string to replace the regular expression with.

Untarring multiple files in Windows using Keka

If you are a Windows user, you can use the freeware Keka to untar multiple files. Keka is a file archiver for macOS and Windows that can extract files from multiple archive formats, including tar, zip, and rar. Here’s how you can use Keka to extract tar archives:

  1. Download and install Keka from the official website.
  2. Open Keka and drag and drop the tar archive(s) you want to extract onto the Keka window.
  3. Keka will automatically extract the files to the same directory as the archive(s) or to a directory you specify.
Читайте также:  Setting time server in linux

Other helpful code examples for untarring all files in a directory

Conclusion

In conclusion, knowing the right commands and syntax for untarring multiple files in a directory is essential for Linux/Unix users. In this comprehensive guide, we discussed how to extract all tar files in a directory, extract multiple files from a tar archive, extract multiple tar.gz files with a single tar call, extract tar files to a different or specific directory, and even untar multiple files in Windows using Keka. By following these steps, you can efficiently extract multiple files and save time. Additionally, we provided additional helpful points for the reader, such as using compression tools, maintaining directory structure, and using a GUI tool for extraction.

Источник

How To Untar Files In Linux?

How To Untar Files In Linux?

The *.tar is a popular archive format used to compress files in Linux and Unix operating systems. The tar format is used with different compression algorithms like gz, bz2, etc. The most important function of the tar format is the ability to store multiple files and directories as a single file where it can be easily compressed. In the tutorial, we examine how to untar files with different compressions algorithms like gz, bz2 etc.

Untar Command Syntax

The tar command has the following syntax which can be used to untar files and folders in different ways.

tar OPTIONS TAR_FILE PATH
  • OPTIONS is used to untar different compressions formats.
  • TAR_FILE is the tar file.
  • PATH is optional and used in the tar file is extracted differently than the current working path.

Untar tar File

A tar file can be untared or extracted with the following command.

Untar tar.gz File

The tar files can be compressed with the gzip as gz format. In the following example, we extract the tar.gz file.

Untar tar.bz2 File

The bz2 is another popular compression format where tar can be compressed with it. The *.tar.bz2 can be untarred like below.

Untar To Specified Path or Folder

The untar operation extract files to the current working directory by default. But we can also specify another directory to untar a tar archive. The path can be specified with the -C option like below.

$ tar -xvf nmap.tar.bz2 -C /home/ismail/Downloads/

List Contents of tar File

A typical tar file contains lots of files and directories. The contents of the tar file can be listed with the following command.

List Contents of tar.gz File

List Contents of tar.bz2 File

Untar Single File

A single file can be untared from a tar archive. The file which will be extracted is added after the tar archive file.

$ tar -xvf nmap.tar.gz nmap/capsule.c

Untar Specific Extensions

Files with specific extensions can be untar using the tar command. In the following example, we untar files with *.txt extension.

$ tar -xvf nmap.tar.gz --wildcards "*.txt"

Untar Files with Specified Pattern

$ tar -xvf nmap.tar.gz --wildcards "Cache*"

Источник

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