Linux untar to directory

How to Extract Tar Files to Specific or Different Directory in Linux

The tar utility is one of the utilities that you can use to create a backup on a Linux system. It includes many options that one can use to specify the task to achieve.

Extract Linux Tar Files Different or New Directory

One thing to understand is that you can extract tar files to a different or specific directory, not necessarily the current working directory. You can read more about tar backup utility with many different examples in the following article, before proceeding further with this article.

In this guide, we shall take a look at how to extract tar files to a specific or different directory, where you want the files to reside.

The general syntax of tar utility for extracting files:

Note: In the above first syntax, the -C option is used to specify a different directory other than the current working directory.

Let us now look at some examples below.

Example 1: Extracting tar Files to a Specific Directory

In the first example, I will extract the files in articles.tar to a directory /tmp/my_article . Always make sure that the directory into which you want to extract tar file exists.

Let me start by creating the /tmp/my_article directory using the command below:

You can include the -p option to the above command so that the command does not complain.

To extract the files in articles.tar to /tmp/my_article , I will run the command bellow:

Img 01: Extract Tar Files to Different Directory

In the above example I used the -v option to monitor the progress of the tar extraction.

Let me also use the —directory option instead of -c for the example above. It works just in the same way.

Img 02: Extract Tar Files to Specific Directory

Example 2: Extract .tar.gz or .tgz Files to Different Directory

First make sure that you create the specific directory that you want to extract into by using:

Now we will extract the contents of documents.tgz file to separate /tmp/tgz/ directory.

Img 03: Extract tar.gz or .tgz Files to Different Directory

Example 3: Extract tar.bz2, .tar.bz, .tbz or .tbz2 Files to Different Directory

Again repeating that you must create a separate directory before unpacking files:

Now we will be unpacking the documents.tbz2 files to /tmp/tar.bz2/ directory.

Img 04: Extract tar.bz2 Files to Different Directory

Example 4: Extract Only Specific or Selected Files from Tar Archive

The tar utility also allows you to define the files that you want to only extract from a .tar file. In the next example, I will extract specific files out of a tar file to a specific directory as follows:

Читайте также:  Что надо сделать после установки linux mint 19

Img 05: Extract Specific Files From Tar Archive

Summary

That is it with extracting tar files to a specific directory and also extracting specific files from a tar file. If you find this guide helpful or have more information or additional ideas, you can give me a feedback by posting a comment.

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

We are thankful for your never ending support.

Источник

Linux tar Command and How To Tar A Directory

Old days of Unix to make backups into tape archives tar utility was used. The days past and Linux become popular in UNIX world. But the popularity of tar is never ended. tar is used compress and extract files. We will look usage examples of tar in this tutorial. Tar command is similar to the Windows zip. We can call this for Linux zip folder. There are alternative ways to compress files but we will look tar Linux.

Create Archive with Linux tar

Generally used to make multiple files and directories one file. We will tar a directory

  • c for compress but actually it is not compressed
  • v for verbosity to see what happens
  • f for archive file name
  • dymerge.tar is new tar file name
  • dymerge is the source directory name

Compress While Archiving

We can compress archived with with z parameter. z is used to gzip format

  • z is for gzip compression other options are default for our usage

To compress with bzip2 j parameter should be provided.

List Files in Tar Archive

We can list files without opening the tar. t parameter is used to list. But if the archive is gzip we should provide z too. As we see tar.gz is extension for tarred and gzipped files.

  • t list files
  • z archive is gzip format

List Files in Tar Archive

If the archive is bzip2 we can use following command

Extract Single File From Archive

With tar a single file can be extracted from archive. x is the parameter to be used for this operation

  • dymerge.tar.gz is our archive that contains our single file
  • dymerge/surnames.txt is the file we want to extract

Extract Multiple Files From Archive with tar

We can extract multiple files with tar. We need too provide –wildcards parameter and related files for this. Here * is used for globing

  • –wildcards is the parameter
  • *.txt is the file names we want to exract

Untar Command

We can create some alias to create new untar command like below.

Untar Command

Adding File into Archive

After creating an archive we may need to update or add new files and folders to the existing archive. We can use r option to add new file or folder by specifying file and folder names at the end of the command like below. In this example we will add file named test.txt into the existing archive named dymerge.tar.gz

  • We append test.txt file with r option
  • We can add directories too
Читайте также:  Кали линукс не удалось открыть файл блокировки var lib dpkg lock open 13

Estimate Archive Size

Before archiving a directory with tar command we can learn the size of the archive with the help of wc command. We will tar the directory in to the starndard output which will be redirected to the wc command. wc command will count the bytes and provide estimated size of the given archived file.

Linux tar Command and How To Tar A Directory Infografic

Источник

Untar Tar.gz – Linux Tar Command – HowTo: Extract Tar File

Most of the Linux files that can be downloaded from the Internet are compressed with a tar , tar.gz and tar.bz2 compression formats and it is important to know how to extract such files.

The following article will help you to extract (unpack) and uncompress (untar) – tar , tar.gz and tar.bz2 files from the Linux command line.

You will learn how to list the contents of a tar archive without unpacking it and how to extract only a single file or a single directory.

File extension Description
tar Simple TAR archive without compression
tar.gz TAR archive compressed with GZIP
tar.bz2 TAR archive compressed with BZIP2

Cool Tip: No more wasted time! Download from the web and untar in one step from the Linux command line! Read more →

Untar tar, tar.gz, tar.bx2 Files

Extract a tar file:

Extract and uncompress a tar.gz file:

Extract and uncompress a tar.bz2 file:

Option Description
-x Extract files from an archive
-v Verbosely list files processed
-f Specify an archive or a tarball filename
-z Decompress the contents of the compressed archive created by gzip program ( tar.gz )
-j Decompress the contents of the compressed archive created by bzip2 program ( tar.bz2 )

List the Contents of a tar, tar.gz, tar.bz2 Files

For example, it goes without saying, that it is inadvisable to untar the whole large archive if you need for example to extract only a dingle file or a directory from it.

And of course this is possible with the Linux tar command, but firstly you need to check what is there inside the tarball without unpacking it.

List the contents of a tar file:

List the contents of a tar.gz file:

List the contents of a tar.bz2 file:

Option Description
-t List the contents of an archive

Cool Tip: There is no more need to remember all these -xvf , -xvzf , -xvif keys! This awesome bash function permits to extract any archive type with the single extract command! Read more →

Extract a Single File from a Tarball

Extract a file bar.txt , from an archive:

You can also specify a path to the file:

Extract a Single Directory from a Tarball

Extract a folder, called docs , from an archive:

Источник

tar command in Linux with examples

The Linux ‘tar’ stands for tape archive, is used to create Archive and extract the Archive files. tar command in Linux is one of the important command which provides archiving functionality in Linux. We can use Linux tar command to create compressed or uncompressed Archive files and also maintain and modify them.

Syntax:

Options:
-c : Creates Archive
-x : Extract the archive
-f : creates archive with given filename
-t : displays or lists files in archived file
-u : archives and adds to an existing archive file
-v : Displays Verbose Information
-A : Concatenates the archive files
-z : zip, tells tar command that creates tar file using gzip
-j : filter archive tar file using tbzip
-W : Verify a archive file
-r : update or add file or directory in already existed .tar file

Читайте также:  Как сделали заставку windows 10

What is an Archive file?
An Archive file is a file that is composed of one or more files along with metadata. Archive files are used to collect multiple data files together into a single file for easier portability and storage, or simply to compress files to use less storage space.

Examples:
1. Creating an uncompressed tar Archive using option -cvf : This command creates a tar file called file.tar which is the Archive of all .c files in current directory.

Output :

2. Extracting files from Archive using option -xvf : This command extracts files from Archives.

Output :

3. gzip compression on the tar Archive, using option -z : This command creates a tar file called file.tar.gz which is the Archive of .c files.

4. Extracting a gzip tar Archive *.tar.gz using option -xvzf : This command extracts files from tar archived file.tar.gz files.

5. Creating compressed tar archive file in Linux using option -j : This command compresses and creates archive file less than the size of the gzip. Both compress and decompress takes more time then gzip.

Output :

6. Untar single tar file or specified directory in Linux : This command will Untar a file in current directory or in a specified directory using -C option.

7. Untar multiple .tar, .tar.gz, .tar.tbz file in Linux : This command will extract or untar multiple files from the tar, tar.gz and tar.bz2 archive file. For example the above command will extract “fileA” “fileB” from the archive files.

8. Check size of existing tar, tar.gz, tar.tbz file in Linux : The above command will display the size of archive file in Kilobytes(KB).

9. Update existing tar file in Linux

Output :

10. list the contents and specify the tarfile using option -tf : This command will list the entire list of archived file. We can also list for specific content in a tarfile

Output :

11. Applying pipe to through ‘grep command’ to find what we are looking for : This command will list only for the mentioned text or image in grep from archived file.

12. We can pass a file name as an argument to search a tarfile : This command views the archived files along with their details.

13. Viewing the Archive using option -tvf

Output :

What are wildcards in Linux
Alternatively referred to as a ‘wild character’ or ‘wildcard character’, a wildcard is a symbol used to replace or represent one or more characters. Wildcards are typically either an asterisk (*), which represents one or more characters or question mark (?),which represents a single character.

Example :

14. To search for an image in .png format : This will extract only files with the extension .png from the archive file.tar. The –wildcards option tells tar to interpret wildcards in the name of the files
to be extracted; the filename (*.png) is enclosed in single-quotes to protect the wildcard (*) from being expanded incorrectly by the shell.

Note: In above commands ” * ” is used in place of file name to take all the files present in that particular directory.

?list=PLqM7alHXFySFc4KtwEZTANgmyJm3NqS_L
This article is contributed by Akansh Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Источник

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