Link to home
Start Free TrialLog in
Avatar of prashant_n_mhatre
prashant_n_mhatre

asked on

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.
Avatar of griessh
griessh
Flag of United States of America image

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
BTW:
>Winzip commandline interface allows me to do that

You are sure this is a UNIX question?

======
Werner
Avatar of prashant_n_mhatre
prashant_n_mhatre

ASKER

Yes...I wan't do the same thing using tar
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
BTW: if you use the 't' option with tar, you will get a list of all files in the tar file.

======
Werner
Can't I give file name list from a file?
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
No..the script doesn't extract files from the tar file.
I'm deleting this question..No satisfactory answer
you are giving up fast! You didn't even tell me what is wrong ...

=====
Werner
It extracts all the files. The same thing I was doing earlier.
ASKER CERTIFIED SOLUTION
Avatar of griessh
griessh
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

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
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
Avatar of yuzh
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

yuzh

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

======
Werner
tar xf your-tar-file -I your-selected-files-file

BTW, what the hell is so difficult in doing a: man tar
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
griessh, what do you mean with '-l' ?
It looks like ther is a '-l' option in your tar command, that's confusing me ...

======
Werner
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.
:-)
Thank you all !!! I'll try out your suggestions and get back to you.
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
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.
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
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.
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.