- How to uncompress a tar.gz in another directory
- 5 Answers 5
- How To Untar Files In Linux?
- Untar Command Syntax
- Untar tar File
- Untar tar.gz File
- Untar tar.bz2 File
- Untar To Specified Path or Folder
- List Contents of tar File
- List Contents of tar.gz File
- List Contents of tar.bz2 File
- Untar Single File
- Untar Specific Extensions
- Untar Files with Specified Pattern
- How do I untar a subdirectory into the current directory? [closed]
- How to Extract Tar Files to Specific or Different Directory in Linux
- Example 1: Extracting tar Files to a Specific Directory
- Example 2: Extract .tar.gz or .tgz Files to Different Directory
- Example 3: Extract tar.bz2, .tar.bz, .tbz or .tbz2 Files to Different Directory
- Example 4: Extract Only Specific or Selected Files from Tar Archive
- Summary
How to uncompress a tar.gz in another directory
Why all the downvotes? There are more options than just -C (and maybe his version does not provide this).
Stack Overflow is a site for programming and development questions. This question appears to be off-topic because it is not about programming or development. See What topics can I ask about here in the Help Center. Perhaps Super User or Unix & Linux Stack Exchange would be a better place to ask.
@jww While I see your point we also have tags named tar and gz around in this forum. Extracting a tarball into a specific directory can well be part of a shell script and thus part of programming. I guess this kind of question can be asked in either forum then.
5 Answers 5
You can use the option -C (or —directory if you prefer long options) to give the target directory of your choice in case you are using the Gnu version of tar . The directory should exist:
mkdir foo tar -xzf bar.tar.gz -C foo
If you are not using a tar capable of extracting to a specific directory, you can simply cd into your target directory prior to calling tar ; then you will have to give a complete path to your archive, of course. You can do this in a scoping subshell to avoid influencing the surrounding script:
mkdir foo (cd foo; tar -xzf ../bar.tar.gz) # instead of ../ you can use an absolute path as well
Or, if neither an absolute path nor a relative path to the archive file is suitable, you also can use this to name the archive outside of the scoping subshell:
TARGET_PATH=a/very/complex/path/which/might/even/be/absolute mkdir -p "$TARGET_PATH" (cd "$TARGET_PATH"; tar -xzf -) < bar.tar.gz
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*"
How do I untar a subdirectory into the current directory? [closed]
How to I extract a subdirectory in a tarball into the current directory? Example, the tarball from wordpress:
wordpress/ wordpress/wp-trackback.php wordpress/wp-config-sample.php wordpress/wp-settings.php wordpress/wp-rss2.php wordpress/readme.html wordpress/index.php .
How do I extract everything under wordpress/ into the current directory? In otherwords, it will not create a wordpress directory. I've tried this with no luck:
tar xvfz latest.tar.gz wordpress -C ./
I know I can extract it normally and move it back, but I figure there has to be a way to do it in one shot.
I have had this exact problem come up in a programming context. Let's try to be a bit more broadminded, people -- if s/wordpress/kernel_source/g is enough to make a question "programming related", it was "programming related" to begin with.
@j_random_hacker - the question is not programming related. the question is basically "how do I use the tar utility on a unix system?" there is nothing programming related about that. it might go on serverfault or on a forum for using unix.
There is no better place than here to ask such a question. If this place excludes "non-programming" questions, the scope of the website needs to be broadened to include questions such as mine. Plus, there are many similar questions about tar that were not closed on these grounds (I checked before posting this). If you are gonna close this, you should close those as well. Or should I slightly reword this question to ask "how do I extract $RANDOM_LIBRARY" into the current directory" so it sounds like it is programming related, but really me cheating the system?
The guy's even trying to untar his php files, for pete's sake. This site isn't just about programming, it's about knowing how to use the tools that enable it. That's why we have rich sections on vim and emacs. Tar is one of those tools, and passing command line arguments is just a simple scripting language.
I mean, look over at the related stuff. there are a ton of "non-programming" questions that were not closed. Why single this out. I searched far and wide for an answer to this question before asking here. I mean--how is this +5 one programming related: stackoverflow.com/questions/223656/…
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.
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:
# tar -xf file_name.tar -C /target/directory # tar -xf file_name.tar.gz --directory /target/directory
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:
# tar -xvf articles.tar -C /tmp/my_article/
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/
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.
# tar -zvxf documents.tgz -C /tmp/tgz/
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.
# tar -jvxf documents.tbz2 -C /tmp/tar.bz2/
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:
# mkdir /backup/tar_extracts # tar -xvf etc.tar etc/issue etc/fuse.conf etc/mysql/ -C /backup/tar_extracts/
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.