General
cancel
Showing results for 
Search instead for 
Did you mean: 

Extracting from multiple tar files: Oneliner wanted

SOLVED
Go to solution
Runar Jørgensen
Frequent Advisor

Extracting from multiple tar files: Oneliner wanted

Greetings

I'm extracting files from multiple tar files. This for-loop works:

$ for xx in `ls -1`; do tar xvf $xx; done

but why doesn't this little thing work?

$ ls -1 |awk '{print $1}' | tar xvf -

Tar gives no error message, but no extracting gets done. Puzzling...

Any pointers or comments?

Regards,
Runar
6 REPLIES
Ivan Ferreira
Honored Contributor

Re: Extracting from multiple tar files: Oneliner wanted

The xvf - instruction assumes that you are passing to the pipe the content of a tar file, not a list of tar files, so it's different:

cat test.tar | tar tvf -
ls test.tar | tar tvf -

You are trying to do the second one, that won't ever work.


Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
Mr Arc
Occasional Advisor

Re: Extracting from multiple tar files: Oneliner wanted

HI ,
Try below shell code for extracting many files.

tar_files=`ls *.tar 2>/dev/null`

if [ -n "$tar_files" ]
then
for i in $tar_files
do
tar -zxvf $i 2>&1
done
fi
Runar Jørgensen
Frequent Advisor

Re: Extracting from multiple tar files: Oneliner wanted

Ivan

I'm curious about your reply. I believed tar wanted a one-by-one list of file names as input. But, what you are saying is that I'm really supposed to supply the content of the tar files?

If I have two tar files FILE1.tar and FILE2.tar, Then this (below) should work, but it doesn't. Tar only extracts the first tar file. How come?

$ ls
FILE1.tar FILE2.tar
$ tar tvf FILE1.tar
-rw-r--r-- runar/wheel 401021 2008-02-27 08:42:06 typescript
-rw-r--r-- runar/wheel 0 2008-02-27 12:26:14 typescript1
-rw-r--r-- runar/wheel 0 2008-02-27 12:26:14 typescript10
-rw-r--r-- runar/wheel 0 2008-02-27 12:26:14 typescript2
-rw-r--r-- runar/wheel 0 2008-02-27 12:26:14 typescript3
-rw-r--r-- runar/wheel 0 2008-02-27 12:26:14 typescript4
-rw-r--r-- runar/wheel 0 2008-02-27 12:26:14 typescript5
-rw-r--r-- runar/wheel 0 2008-02-27 12:26:14 typescript6
-rw-r--r-- runar/wheel 0 2008-02-27 12:26:14 typescript7
-rw-r--r-- runar/wheel 0 2008-02-27 12:26:14 typescript8
-rw-r--r-- runar/wheel 0 2008-02-27 12:26:14 typescript9
$ tar tvf FILE2.tar
-rw-r--r-- runar/wheel 144244 2008-02-27 12:39:21 find_xargs.log
-rw-r--r-- runar/wheel 0 2008-02-27 12:28:15 fred.foo
$ cat *.tar | tar xvf -
typescript
typescript1
typescript10
typescript2
typescript3
typescript4
typescript5
typescript6
typescript7
typescript8
typescript9
runar@MMtux:~/tmp$ ls
FILE1.tar typescript typescript10 typescript3 typescript5 typescript7 typescript9
FILE2.tar typescript1 typescript2 typescript4 typescript6 typescript8

Troy,
Thanx for the script.

Regards,
Runar

Ivan Ferreira
Honored Contributor
Solution

Re: Extracting from multiple tar files: Oneliner wanted

That would be the same as:

cat tar1.tar > tar3.tar
cat tar2.tar >> tar3.tar

At that point, you should have tar1 and tar2 in the tar3.tar file. But the problem is that tar1.tar also stored it's end of tar archive mark in the file, so, you won't be able to continue above the end of the tar1 content even in the tar3 file.

As you cannot "forward" a mark in the tarfile as in a tape (mt fsf), probably you won't be able to read the second tar.
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
Ray Mitchell
Occasional Visitor

Re: Extracting from multiple tar files: Oneliner wanted

I use a couple of different commands, based on what I am un-tarring from:
from tape:
while [ $? == 0 ] ; do tar xvf /dev/nst0

for a directory on disk
for x in $(ls ) ; do tar xvf /dev/nst0 /$x

I also pipe the output to a file for review if needed.

Ray
Runar Jørgensen
Frequent Advisor

Re: Extracting from multiple tar files: Oneliner wanted

Excellent followups
Thanx

Runar