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.
Tutorial details | |
---|---|
Difficulty | Easy (rss) |
Root privileges | No |
Requirements | tar with Linux or Unix OS |
Time | 1m |
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 file.name.tar -C /path/to/directory
GNU/tar Linux syntax:
tar xf file.tar -C /path/to/directory
OR
tar xf file.tar --directory /path/to/directory
Extract .tar.gz archive:
tar -zxf file.tar --directory /path/to/directory
Extract .tar.bz2/.tar.zx archive:
tar -jxf file.tar --directory /path/to/directory
Where,
- 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, enter:
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
Sample outputs:
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:
etc/adduser.conf etc/apg.conf etc/appstream.conf etc/brltty.conf etc/ca-certificates.conf etc/debconf.conf etc/deluser.conf etc/fuse.conf etc/fwupd.conf etc/gai.conf etc/hdparm.conf etc/host.conf etc/kernel-img.conf etc/kerneloops.conf etc/ld.so.conf etc/libao.conf etc/libaudit.conf etc/logrotate.conf etc/ltrace.conf etc/mke2fs.conf etc/mtools.conf etc/nsswitch.conf etc/pam.conf etc/pnm2ppa.conf etc/popularity-contest.conf etc/resolv.conf etc/rsyslog.conf etc/sensors3.conf etc/sysctl.conf etc/ucf.conf etc/updatedb.conf etc/usb_modeswitch.conf
Conclusion
You learned how extract files to different folder and directroy under Linux and Unix-like system using the CLI. See tar command man page for more information by typing the following man command:
$ man tar
🐧 10 comments so far... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
Just a side-note ;) http://xkcd.com/1168/
Lmao!
Excellent very neat and clear…………….
on the specific files example. it didn’t work on my environment unless i moved the -C flag before the explicit file names
e.g.
tar -xvf $HOME/etc.backup.tar -C /tmp/data file1 file2 file3 dir1
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?
New software usually goes in /usr/local/. Your personal data stays in your own $HOME.
This is very concise and clear; very well written. It really helped me.
Thank you for writing it, Vivek Gite!
Glad you found it useful.
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
The page has been updated. I appreciate your comment and feedback. Thanks!