Extracting selected files from a tar file

I have a tar file containing multiple files. Is there any way to extract the selected files from tar?
I'm willing to give filelist in a file.

Winzip commandline interface allows me to do that.
LVL 4
prashant_n_mhatreAsked:
Who is Participating?
 
griesshConnect With a Mentor Commented:
Just for a test, replace the 'tar' in the loop with

echo $name

You should see al the file names you hav ein your <filelist.file>. Are you sure you have the right script?
If your file is "x.tar", your file list is "tar.txt", the script should look like this:

while read name
do
  tar -xvf x.tar $name
done < tar.txt


Let me know what happens. BTW: What is your OS?

======
Werner

0
 
griesshCommented:
Since this is an easy question, yes, for at least 50 points.

From the EE "tips":

When you get to the "Ask a Question" page, first give your question a title. Be as specific as you can so that it will be easy for Experts to determine if they can help you. Next, select a point value for your question. If your question is easy to answer, offer 50 question points. If it is more difficult, increase the points to 100, and if it is very difficult, offer 200 question points.

======
Werner
0
 
griesshCommented:
BTW:
>Winzip commandline interface allows me to do that

You are sure this is a UNIX question?

======
Werner
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
prashant_n_mhatreAuthor Commented:
Yes...I wan't do the same thing using tar
0
 
griesshCommented:
if your tar is on floppy:

tar xvf /dev/fd0 <filenames> if your floppy drive is /dev/fd0


if it's on disk:

tar -xvf <tarfile> <filenames>

Good luck, let me know if there are problems ...

======
Werner
0
 
griesshCommented:
BTW: if you use the 't' option with tar, you will get a list of all files in the tar file.

======
Werner
0
 
prashant_n_mhatreAuthor Commented:
Can't I give file name list from a file?
0
 
griesshCommented:
I am not aware of a direct file option. But this might help:

while read name
do
   tar -xvf <tarfile> $name
done < <filelist.file>


======
Werner
0
 
prashant_n_mhatreAuthor Commented:
No..the script doesn't extract files from the tar file.
0
 
prashant_n_mhatreAuthor Commented:
I'm deleting this question..No satisfactory answer
0
 
griesshCommented:
you are giving up fast! You didn't even tell me what is wrong ...

=====
Werner
0
 
prashant_n_mhatreAuthor Commented:
It extracts all the files. The same thing I was doing earlier.
0
 
griesshCommented:
Just for a test, replace the 'tar' in the loop with

echo $name

You should see all the file names you have in your <filelist.file>. Are you sure you have the right script?
If your file is "x.tar", your file list is "tar.txt", the script should look like this:

while read name
do
  tar -xvf x.tar $name
done < tar.txt


Let me know what happens. BTW: What is your OS?

======
Werner

0
 
prashant_n_mhatreAuthor Commented:
This is my script

while read name                  
do                              
#tar -xvf 20010807.nc.tar $name
echo $name                      
done < files.lst                
                                 
echo displays coorect files.

20010807.nc.tar conatins many files. I'm specifying 4 files in files.lst.

my OS is DYNIX/PTX
0
 
griesshCommented:
Hmmm, never heard of that ...

Can you untar a single file with:

tar -xvf 20010807.nc.tar <one_of_your_files>

also try to do a

man tar

that should give you the option of the tar command.

======
werner
0
 
yuzhCommented:
You have to specified the file names exactly as they are stored in the
tar file.

To get a file list from the tar file (assume MY.tar)

   tar -tf MY.tar > MYFILES

then use a text editor to delete the file you don't want from MYFILE,

To extract the FILES from MY.tar:

   tar -xvf MY.tar `cat MYFILES`


If you only want to get one file:

   tar -xvf MY.tar path-filename

0
 
griesshCommented:
yuzh

Thanks for the comment. I hope prashant_n_mhatre knows what's in the file ... the 't' option was mentioned earlier already.

======
Werner
0
 
ahoffmannCommented:
tar xf your-tar-file -I your-selected-files-file

BTW, what the hell is so difficult in doing a: man tar
0
 
griesshCommented:
ahoffmann

apparently some people don't like to read and understand the man pages ... I thought that would be an easy one ...

BTW '-l' is a errormsg for linked files for me ...

======
Werner
0
 
ahoffmannCommented:
griessh, what do you mean with '-l' ?
0
 
griesshCommented:
It looks like ther is a '-l' option in your tar command, that's confusing me ...

======
Werner
0
 
ahoffmannCommented:
hey griessh, are you using a WYSIWYG system like M$ ? Then it might be vice to use the copy&paste functionality, or simply a font which has uniq shapes for '1' and 'l' and 'I'.
For programmers in rainy Seatle there might not be a esential difference for these characters, but for tar it is.
:-)
0
 
prashant_n_mhatreAuthor Commented:
Thank you all !!! I'll try out your suggestions and get back to you.
0
 
griesshCommented:
I really didn't expect there to be a "l", but even with the pipe I'm lost ...
It's raining in Seattle? In The Boston area here we're over 95F/150% humidity, but I already hear the thunder rolling in :-)
And yes, I'm on NT here, my AIX box is for serious development work only.

======
Werner
0
 
PeterMacCommented:
Prashant

      problem is not how to extract specific files, check "man tar" for that, all the tar command wants is a list of files on the end of the command line.


      PROBLEM - most versions of tar are very specific on the way the file name is specified, it must match exactly the name in the tar directory. If file is saved as "./tmp/myfile" specifying "myfile" won't find it. Use "tar -t" to check, you must give full path as stored.


      PROBLEM - accessing list from a file. There is no way of telling tar to access an external filelist you must use a construction such as "`cat filelist`" on the command line to pass contents of "filelist" to tar. Single quotes used above are backwards not normal single quote.
0
 
griesshCommented:
PeterMac, welcome to EE.
We usually write a comment instead of an answer, so the question won't be locked and other experts have the chance to give their opinions, too. Only if you are 150% sure that you have the right answer, we would post it as an answer. Please take a look at "Tips on Comments and Answers" at the end of this page.

It is also considered to be not very polite to repost comments from other experts as answers.

prashant_n_mhatre

please reject PeterMac's answer. At the same time it would be nice to accept one of our comments as an answer or at least let us know if there are still any problems.

======
Werner
0
 
prashant_n_mhatreAuthor Commented:
Unfortunately I got busy with other activities and I didn't get time to test your suggestions.
I'll do it very soon.

Presently I'm extracting all files, moving the required files to another directory and deleting the remaining files.

Thanks PeterMac for your comment but what griessh says is correct. It seems that you are new to EE.
I never proposed an answer even if I'm 1000 % sure. Sorry for the time being I'm rejecting your answer.

I'll award points to the most useful comment.
0
 
avkbhasCommented:
well, I hope this comment helps. whenever I need to extract tars that contain a specific pattern in their file name, I use the following command.

tar -tf tarfile.tar | grep "pattern" | xargs tar -xvf tarfile.tar

If you have a specific list of file names that match exactly, then use,

cat ListOfFileNamesToBeXtracted | xargs tar -xvf tarfile.tar

and as rightly suggested by somebody, you can always feed the list by using

tar -xvf tarfile.tar `cat ListOfFileNamesToBeXtracted`

and the cat command is within back quotes which can be found right under the Esc key in the keyboard.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.