Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract files inside a directory from a tar file in terminal?

I want to extract only the files inside a folder of a tar file

Example:

Contents of tar file:

  • /home/parent_dir/child_dir/

I want to extract only the files inside child_dir to another directory

like image 936
steamboy Avatar asked May 13 '10 02:05

steamboy


People also ask

How do I extract only certain files from a tar?

Now, if you want a single file or folder from the “tar” file, you need to use the name of the “tar” file and the path to a single file in it. So, we have used the “tar” command with the “-xvf” option, the name of the “tar” file, and the path of a file to be extracted from it as below.

How do I extract the contents of a tar file?

To extract (unzip) a tar.gz file simply right-click on the file you want to extract and select “Extract”. Windows users will need a tool named 7zip to extract tar.gz files. The -v option will make the tar command more visible and print the names of the files being extracted on the terminal.


2 Answers

The command

tar xf tarfile.tar /home/parent_dir/child_dir

will only extract files in child_dir and its subordinates.

If /home/parent_dir/child_dir is not where you want them to be, GNU tar provides a --transform option that would be used like:

tar  --transform 's,/home/parent_dir/child_dir,foo,' --show-transformed -xf tarfile.tar

which will put the files that would have gone into /home/parent_dir/child_dir into ./foo instead.

like image 105
msw Avatar answered Nov 15 '22 10:11

msw


cd <another_directory>
tar xvf <path_to_tar>/<tarfile>.tar <child_dir>
e.g.
cd <parent_directory>
tar cvf test.tar *
tar tf test.tar
see the folder you wanted. e.g. src/org
cd <some other directory you want to extract to>
tar xvf ..\test.tar src/org
ls
you'll now see the directory you were after from the tar e.g. src/org

like image 28
user326608 Avatar answered Nov 15 '22 11:11

user326608