Solved

Extracting selected files from a tar file

Posted on 2001-08-09
28
365 Views
Last Modified: 2010-04-21
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.
0
Comment
Question by:prashant_n_mhatre
  • 14
  • 8
  • 3
  • +3
28 Comments
 
LVL 11

Expert Comment

by:griessh
ID: 6369517
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
 
LVL 11

Expert Comment

by:griessh
ID: 6369525
BTW:
>Winzip commandline interface allows me to do that

You are sure this is a UNIX question?

======
Werner
0
 
LVL 4

Author Comment

by:prashant_n_mhatre
ID: 6369530
Yes...I wan't do the same thing using tar
0
 
LVL 11

Expert Comment

by:griessh
ID: 6369570
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
 
LVL 11

Expert Comment

by:griessh
ID: 6369572
BTW: if you use the 't' option with tar, you will get a list of all files in the tar file.

======
Werner
0
 
LVL 4

Author Comment

by:prashant_n_mhatre
ID: 6369655
Can't I give file name list from a file?
0
 
LVL 11

Expert Comment

by:griessh
ID: 6369721
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
 
LVL 4

Author Comment

by:prashant_n_mhatre
ID: 6369800
No..the script doesn't extract files from the tar file.
0
 
LVL 4

Author Comment

by:prashant_n_mhatre
ID: 6370021
I'm deleting this question..No satisfactory answer
0
 
LVL 11

Expert Comment

by:griessh
ID: 6370145
you are giving up fast! You didn't even tell me what is wrong ...

=====
Werner
0
 
LVL 4

Author Comment

by:prashant_n_mhatre
ID: 6370153
It extracts all the files. The same thing I was doing earlier.
0
 
LVL 11

Accepted Solution

by:
griessh earned 50 total points
ID: 6370182
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
 
LVL 11

Expert Comment

by:griessh
ID: 6370185
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
 
LVL 4

Author Comment

by:prashant_n_mhatre
ID: 6370205
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 11

Expert Comment

by:griessh
ID: 6370662
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
 
LVL 38

Expert Comment

by:yuzh
ID: 6370869
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
 
LVL 11

Expert Comment

by:griessh
ID: 6370904
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
 
LVL 51

Expert Comment

by:ahoffmann
ID: 6371817
tar xf your-tar-file -I your-selected-files-file

BTW, what the hell is so difficult in doing a: man tar
0
 
LVL 11

Expert Comment

by:griessh
ID: 6372202
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
 
LVL 51

Expert Comment

by:ahoffmann
ID: 6373022
griessh, what do you mean with '-l' ?
0
 
LVL 11

Expert Comment

by:griessh
ID: 6373059
It looks like ther is a '-l' option in your tar command, that's confusing me ...

======
Werner
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 6373093
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
 
LVL 4

Author Comment

by:prashant_n_mhatre
ID: 6373100
Thank you all !!! I'll try out your suggestions and get back to you.
0
 
LVL 11

Expert Comment

by:griessh
ID: 6373526
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
 
LVL 9

Expert Comment

by:PeterMac
ID: 6413422
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
 
LVL 11

Expert Comment

by:griessh
ID: 6413477
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
 
LVL 4

Author Comment

by:prashant_n_mhatre
ID: 6413557
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
 

Expert Comment

by:avkbhas
ID: 6437641
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

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This tech tip describes how to install the Solaris Operating System from a tape backup that was created using the Solaris flash archive utility. I have used this procedure on the Solaris 8 and 9 OS, and it shoudl also work well on the Solaris 10 rel…
My previous tech tip, Installing the Solaris OS From the Flash Archive On a Tape (http://www.experts-exchange.com/articles/OS/Unix/Solaris/Installing-the-Solaris-OS-From-the-Flash-Archive-on-a-Tape.html), discussed installing the Solaris Operating S…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now