See all UNIX related articles/faq
I want to extract tar file to specific directory called /tmp/data. How can I extract a tar archive to a different directory using tar command on a Linux or Unix-like systems?

You do not need to change the directory using the cd command and extract files. This page explains how to extract a tar archive to different directory on a Linux/Unix system using the tar command.
How to Extract Tar Files to Specific or Different Directory in Linux
Tutorial details
Difficulty level Easy
Root privileges No
Requirements Linux or Unix terminal
Category Archiving
Prerequisites tar command
OS compatibility BSD • Linux • macOS • Unix • WSL
Est. reading time 3 minutes

Syntax For Tar Command To Extract Tar Files To a Different Directory

Untarring a file can be done using the following syntax. Typical Unix tar syntax:
$ tar -xf -C /path/to/directory
GNU/tar Linux syntax:
$ tar xf file.tar -C /path/to/directory
OR use the following to extract tar files to a different directory:
$ tar xf file.tar --directory /path/to/directory
Extract .tar.gz archive:
$ tar -zxf file.tar --directory /path/to/directory
Want to extract .tar.bz2/.tar.zx archive? Try:
$ tar -jxf file.tar --directory /path/to/directory

  • x : Extract files
  • f : Tar archive name
  • --directory : Set directory name to extract files
  • -C : Set dir name to extract files
  • -z : Work on .tar.gz (gzip) file format
  • -j : Work on .tar.bz2 (bzip2) file format
  • -J (capital J) : Work on .tar.xz (xz) file format (see how to extract tar.xz files in Linux for more details)
  • -v : Verbose output i.e. show progress on screen

Example: Extract files to another directory

In this example, I’m extracting $HOME/etc.backup.tar file to a directory called /tmp/data. First, you have to create the directory manually using the mkdir command:
$ mkdir /tmp/data
To extract a tar archive $HOME/etc.backup.tar into a /tmp/data, enter:
$ tar -xf $HOME/etc.backup.tar -C /tmp/data
To see a progress pass the -v option:
$ tar -xvf $HOME/etc.backup.tar -C /tmp/data

Gif 01: tar Command Extract Archive To Different Directory Command

Gif 01: tar Command Extract Archive To Different Directory Command

Use the ls command and cd command to view extracted files. For example:
$ cd /tmp/data
$ ls -l
# Use the grep/egrep command to filter out results
$ ls -l | grep 'filename_here'

You can use the df command and du command to view Unix and Linux file system disk space usage too when extracting files. For example:
$ df -H
$ du -csh /tmp/data

Extract only specific files from a tar archive

You can extract specific files too:
## extract only file1, file2, file3
## and dir1 to /tmp/data/

$ tar -xvf $HOME/etc.backup.tar file1 file2 file3 dir1 -C /tmp/data

Extract .tar.gz/.tgz archive to specific folder

To extract a foo.tar.gz (.tgz extension file) tarball to /tmp/bar, enter:
$ mkdir /tmp/foo
$ tar -zxvf foo.tar.gz -C /tmp/foo

Extract .tar.bz2/.tbz2/.tb2/.tar.xz archive to specific directory

To extract a foo.tar.bz2 (.tbz, .tbz2 & .tb2 extension file) tarball to /tmp/bar, enter:
$ mkdir /tmp/bar
$ tar -jxvf bar.tar.bz2 -C /tmp/bar

Sample outputs:



You learned how extract files to different folder and directory under Linux and Unix-like system using the CLI. See the following man page by typing the man command or help command:
$ man tar
$ tar --help | grep -w -E -- '-(C|directory|f|j|J|v|x|z)'

🥺 Was this helpful? Please add a comment to show your appreciation or feedback.

nixCrat Tux Pixel Penguin
Hi! 🤠
I'm Vivek Gite, and I write about Linux, macOS, Unix, IT, programming, infosec, and open source. Subscribe to my RSS feed or email newsletter for updates.

14 comments… add one
  • Erathiel Jan 8, 2015 @ 11:07

    Just a side-note ;)

    • Tony Jan 9, 2015 @ 13:00


  • Nagaraju Jan 9, 2015 @ 6:19

    Excellent very neat and clear…………….

  • Jossef Apr 18, 2016 @ 19:09

    on the specific files example. it didn’t work on my environment unless i moved the -C flag before the explicit file names

    tar -xvf $HOME/etc.backup.tar -C /tmp/data file1 file2 file3 dir1

  • fanuch Aug 1, 2016 @ 6:27

    I’m very curious to know if there is a directory that is common place to install tar in, or no one follows a directory structure?

    • 🛡️ Vivek Gite (Author and Admin) Vivek Gite Aug 1, 2016 @ 10:22

      New software usually goes in /usr/local/. Your personal data stays in your own $HOME.

  • Symantha Meyers Aug 30, 2020 @ 21:39

    This is very concise and clear; very well written. It really helped me.
    Thank you for writing it, Vivek Gite!

    • 🛡️ Vivek Gite (Author and Admin) Vivek Gite Aug 31, 2020 @ 5:57

      Glad you found it useful.

  • Not a name Oct 8, 2020 @ 20:47

    Hello, thanks for this article! But something is wrong, in the syntax section you use -z for tar.gz and L-j for tar.bz2/.tar.zx. After when you explain the flags you say:
    -j : Work on .tar.gz file format
    -z : Work on .tar.bz2 file format

    • 🛡️ Vivek Gite (Author and Admin) Vivek Gite Oct 9, 2020 @ 11:20

      The page has been updated. I appreciate your comment and feedback. Thanks!

  • Myles Dugenfelder Nov 16, 2020 @ 16:18

    Where does the file get decompressed to if you do not specify the destination DIR?

  • Christian May 17, 2022 @ 16:21

    I keep getting:

    `tar: Cannot use multi-volume compressed archives
    Try 'tar --help' or 'tar --usage' for more information.`
    • 🛡️ Vivek Gite (Author and Admin) Vivek Gite May 17, 2022 @ 16:43

      Try passing the -M to tar command. You need all volume to work. For example, if I have demo.tar, demo1.tar, demo2.tar as multi-volume , then I need to run:

      tar -xMf demo.tar

      Then you will get prompt:

      Prepare volume #2 for ‘demo.tar’ and hit return:

      You need to enter n demo1.tar. For example:

      Prepare volume #2 for ‘demo.tar’ and hit return: n demo1.tar

      Repeat this for rest of volumes, when prompted:

      Prepare volume #2 for ‘demo.tar’ and hit return: n demo2.tar

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre> for code samples. Your comment will appear only after approval by the site admin.