0% found this document useful (0 votes)
42 views1 page

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

Uploaded by

Szutor
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views1 page

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

Uploaded by

Szutor
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Learn Linux in One Week and Go From Zero to Hero - Get This Book

Home Linux eBooks Linux Online Courses Linux Hosting A-Z


A-Z Linux
Linux Commands
Commands Tech Deals About Us Follow Us

Linux Distro’s FAQ’s Programming Linux Commands Linux Tricks Best Linux Tools Certi cations Guides Monitoring Tools

How to Extract Tar Files to Speci c or Different


Directory in Linux
Aaron Kili Last Updated: July 17, 2017 Linux Commands 12 Comments

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.

Privacy

A Beginners Guide To Learn Linux


for Free [with Examples]

Extract Linux Tar Files Different or New Directory

One thing to understand is that you can extract tar les to a different or speci c 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.

Mastering tar Command with this 18 Examples in Linux

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

The general syntax of tar utility for extracting les:

# tar -xf file_name.tar -C /target/directory


# tar -xf file_name.tar.gz --directory /target/directory

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

Red Hat RHCSA/RHCE 8


Certi cation Study Guide [eBooks]

Linux Foundation LFCS and LFCE


Let us now look at some examples below. Certi cation Study Guide [eBooks]

Example 1: Extracting tar Files to a Speci c Directory


Linux Commands and Tools
In the rst example, I will extract the les in articles.tar to a directory /tmp/my_article . Always make sure
that the directory into which you want to extract tar le exists. 10 Useful “IP” Commands to
Con gure Network Interfaces
Let me start by creating the /tmp/my_article directory using the command below:
Wikit – A Command Line Tool to
Search Wikipedia in Linux
# mkdir /tmp/my_article
11 Linux Kernel Boot-time
Parameters Explained
You can include the -p option to the above command so that the command does not complain.
How to Rename File While
Downloading with Wget in Linux
To extract the les in articles.tar to /tmp/my_article , I will run the command bellow:
20 Linux YUM (Yellowdog Updater,
Modi ed) Commands for Package
# tar -xvf articles.tar -C /tmp/my_article/
Management

Diskonaut – A Terminal Disk Space


Navigator for Linux

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.

# tar -xvf articles.tar --directory /tmp/my_articles/

Img 02: Extract Tar Files to Speci c Directory

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


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

# mkdir -p /tmp/tgz

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

# tar -zvxf documents.tgz -C /tmp/tgz/

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 les:

# mkdir -p /tmp/tar.bz2

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

# tar -jvxf documents.tbz2 -C /tmp/tar.bz2/

Img 04: Extract tar.bz2 Files to Different Directory

Example 4: Extract Only Speci c or Selected Files from Tar


Archive
The tar utility also allows you to de ne the les that you want to only extract from a .tar le. In the next
example, I will extract speci c les out of a tar le to a speci c directory as follows:

# mkdir /backup/tar_extracts
# tar -xvf etc.tar etc/issue etc/fuse.conf etc/mysql/ -C /backup/tar_extracts/

Img 05: Extract Speci c Files From Tar Archive

Summary
That is it with extracting tar les to a speci c directory and also extracting speci c les from a tar le. If you
nd this guide helpful or have more information or additional ideas, you can give me a feedback by posting a
comment.

Linux Tricks , Tar Command Examples

How to Use 8 Useful ‘Debian Goodies Utilities’ to Manage How to Use ‘Tmux Terminal’ to Access Multiple Terminals
Debian Packages Inside a Single Console

If you liked this article, then do subscribe to email alerts for Linux tutorials. If you have any questions or
doubts? do ask for help in the comments section.

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.

Related Posts

How to Use fgrep Command 12 Practical Examples of An Easy Way to Hide Files
to Search for Strings in Linux Linux Grep Command and Directories in Linux

How to Use ‘dir’ Command Linux mkdir Command 20 Linux egrep Command
with Different Options and Examples Examples
Arguments in Linux

12 thoughts on “How to Extract Tar Files to Speci c or


Different Directory in Linux”

Pascal
April 16, 2021 at 10:42 am

Great article.

Just to note that “Example 4: Extract Only Speci c or Selected Files from Tar Archive” is the wrong
syntax.

The order of the arguments has to be changed. The single les or directories to be extracted need to
be last:

It shall read

# tar -xvf etc.tar -C /backup/tar_extracts/ etc/issue etc/fuse.conf etc/mysql/

reference: https://stackover ow.com/a/9249779

Reply

SHIVANI UMEACADEMY. COM


February 7, 2019 at 3:52 pm

Your blog is very good, your blog has great information, your content is also very good, your blog has
got a lot of help.

Reply

Aaron Kili
February 10, 2019 at 2:29 pm

@SHIVANI

I would like you to know that we are pleased to have read your beautiful and kind note of
appreciation and encouragement.

Reply

Jalal Hajigholamali
January 6, 2016 at 12:22 pm

Hi,
tar command accepts options without minus[-] sign
also ‘-j’ option is optional and can be ignore….
example:
tar xvf …..
Thanks a lot

Reply

Aaron Kili
January 9, 2016 at 3:48 am

@Jalal Hajigholamali Welcome, and thanks for the info also

Reply

Ren
January 3, 2016 at 6:26 am

When I extract only speci c les from Tar archive, ‘-C’ option is doesn’t work.

Reply

Ravi Saive
January 4, 2016 at 11:06 am

@Ren,

Have you created the separate directory before extracting the speci c les to that directory?
The -C option is used to specify a different directory and it must exists before extracting les.

For example,

# mkdir /tmp/dump les


# tar -xvf powertop-2.7.tar.gz powertop-2.7/m4/po.m4 -C /tmp/dump les

Else, you can also extract les withing the current directory without creating a new directory
like:

# tar -xvf powertop-2.7.tar.gz powertop-2.7/m4/lib-ld.m4

powertop-2.7/m4/po.m4

Reply

Eric
November 1, 2017 at 8:30 pm

I have the same issue as @Ren, and in my case I have created the destination dir… no
matter how I specify things the extracted le always ends up in the current directory.

Reply

Eric
November 1, 2017 at 8:34 pm

Oh… if I change the order it works. If I move the “–directory ” earlier in the args, it works.

Reply

David
January 3, 2016 at 12:27 am

In the last example, extracting several les from the etc directory out of the tar le, what is the cts/ in
the rst line of the extracted les?

Reply

Konrad
January 2, 2016 at 10:50 pm

Great post. It would be helpful for Linux beginners to explain the switches used. What does -xf do?
You explain the addition of the -v switch which is good. What’s the difference between -C and –
directory? In Example 2, you throw in the -z switch and in Example 3, the -j switch without any
explanation. Then in example 4, you talk about extracting single les, yet the command also includes
an option to extract all les in a speci c directory – etc/mysql/. Again, a good example for using
different options, but it could use a bit of explanation.

Reply

Ravi Saive
January 4, 2016 at 11:11 am

@Konrad,

Yes, we haven’t speci ed the meaning of each tar command option used in these examples,
the reason because we’ve already requested users to read the article that says Mastering tar
Command with this 18 Examples in Linux, before heading up further. No issue you can nd all
these options explanation below:

c – create a archive le.


x – extract a archive le.
v – show the progress of archive le.
f – lename of archive le.
t – viewing content of archive le.
j – lter archive through bzip2.
z – lter archive through gzip.
r – append or update les or directories to existing archive le.
W – Verify a archive le.
wildcards (*) – Specify patters in unix tar command.

Reply

Got something to say? Join the discussion.


Have a question or suggestion? Please leave a comment to start the discussion. Please keep in mind that all
comments are moderated and your email address will NOT be published.

Name *

Email *

Save my name, email, and website in this browser for the next time I comment.

Post Comment

Linux Server Monitoring Tools Learn Linux Tricks & Tips Best Linux Tools

How to Monitor Ubuntu Performance Using How to Test Website Loading Speed in Linux 3 Useful GUI and Terminal Based Linux Disk
Netdata Terminal Scanning Tools

How to Install Icinga2 Monitoring Tool on Ways to Use ‘ nd’ Command to Search 8 Best Video Editing Softwares I Discovered
Ubuntu 20.04/22.04 Directories More Ef ciently for Linux

httpstat – A Curl Statistics Tool to Check How to Download MP3 Tracks from a Best RDP (Remote Desktop) Clients for Linux
Website Performance YouTube Video Using YouTube-DL
8 Top Open Source Reverse Proxy Servers for
systemd-analyze – Find System Boot-up How to Run MySQL/MariaDB Queries Directly Linux
Performance Statistics in Linux from the Linux Command Line
10 Best Clipboard Managers for Linux
LibreNMS – A Fully Featured Network 7 Ways to Determine the File System Type in
5 Most Notable Open Source Centralized Log
Monitoring Tool for Linux Linux (Ext2, Ext3 or Ext4)
Management Tools
Swatchdog – Simple Log File Watcher in How to Block or Disable Normal User Logins
Real-Time in Linux in Linux

Donate to TecMint Contact Us Advertise on TecMint Linux Services Copyright Policy Privacy Policy Career Sponsored Post

Tecmint: Linux Howtos, Tutorials & Guides © 2022. All Rights Reserved.
The material in this site cannot be republished either online or of ine, without our permission.

Hosting Sponsored by : Linode Cloud Hosting

You might also like