# Extract multiple tar



## sk8harddiefast (Oct 8, 2010)

I want to extract multiple tar.gz files from terminal.
I run:

```
sudo tar -zxvf *.tar.gz
```
But I get this.

```
tar: xmms-256.tar.gz: Not found in archive
tar: xmmearth.tar.gz: Not found in archive
tar: titanium.tar.gz: Not found in archive
tar: sword.tar.gz: Not found in archive
tar: spiffMEDIA.tar.gz: Not found in archive
tar: sinistar.tar.gz: Not found in archive
tar: nuance-green-2.0.tar.gz: Not found in archive
tar: nuance-2.0.tar.gz: Not found in archive
tar: nixamp2.tar.gz: Not found in archive
tar: myway.tar.gz: Not found in archive
tar: minEguE-xmms-v2.tar.gz: Not found in archive
tar: minEguE-xmms-v1.tar.gz: Not found in archive
tar: maXMMS.tar.gz: Not found in archive
tar: m2n.tar.gz: Not found in archive
tar: ions.tar.gz: Not found in archive
tar: gLaNDAmp-2.0.tar.gz: Not found in archive
tar: fyre.tar.gz: Not found in archive
tar: fiRe.tar.gz: Not found in archive
tar: eMac_Xmms_color_schemes.tar.gz: Not found in archive
tar: eMac-XMMS.tar.gz: Not found in archive
tar: detone_green.tar.gz: Not found in archive
tar: detone_blue.tar.gz: Not found in archive
tar: cracked.tar.gz: Not found in archive
tar: cherry_best.tar.gz: Not found in archive
tar: cherry.tar.gz: Not found in archive
tar: chaos_XMMS.tar.gz: Not found in archive
tar: cart0onix.tar.gz: Not found in archive
tar: bmXmms.tar.gz: Not found in archive
tar: blueHeart_Xmms.tar.gz: Not found in archive
tar: blueHeart-xmms-20.tar.gz: Not found in archive
tar: blackstar.tar.gz: Not found in archive
tar: arctic_Xmms.tar.gz: Not found in archive
tar: XawMMS.tar.gz: Not found in archive
tar: XMMS-Green.tar.gz: Not found in archive
tar: XMMS-AfterStep.tar.gz: Not found in archive
tar: X-Tra.tar.gz: Not found in archive
tar: WoodPanel.tar.gz: Not found in archive
tar: Winamp_X_XMMS_1.01.tar.gz: Not found in archive
tar: Vulcan21.tar.gz: Not found in archive
tar: Vulcan.tar.gz: Not found in archive
tar: Vegetal_Blues.tar.gz: Not found in archive
tar: Vegetali_1-1.tar.gz: Not found in archive
tar: UltrafinaSEM.tar.gz: Not found in archive
tar: UltrafinaSE.tar.gz: Not found in archive
tar: Ultrafina2000.tar.gz: Not found in archive
tar: Ultrafina.tar.gz: Not found in archive
tar: Ultrafina-pw.tar.gz: Not found in archive
tar: SuedE.tar.gz: Not found in archive
tar: Plume-XMMS-v1.tar.gz: Not found in archive
tar: Panic.tar.gz: Not found in archive
tar: OmniAMP-1.3.tar.gz: Not found in archive
tar: NoerdAmp-SE.tar.gz: Not found in archive
tar: NeXTAmp2.4.tar.gz: Not found in archive
tar: NeXTAmp2-1.0pre1.tar.gz: Not found in archive
tar: MarbleX.tar.gz: Not found in archive
tar: Marble.tar.gz: Not found in archive
tar: LinuxDotCom.tar.gz: Not found in archive
tar: Inverse.tar.gz: Not found in archive
tar: Helix-Sawfish-xmms.tar.gz: Not found in archive
tar: HeliXMMS.tar.gz: Not found in archive
tar: Ghost-10.tar.gz: Not found in archive
tar: GTK+.tar.gz: Not found in archive
tar: Freshmeat_Amp.tar.gz: Not found in archive
tar: FreeBSD.tar.gz: Not found in archive
tar: FB_2.1.tar.gz: Not found in archive
tar: FB_1.2.tar.gz: Not found in archive
tar: Eclipse.tar.gz: Not found in archive
tar: Cyrus-XMMS.tar.gz: Not found in archive
tar: Covenant.tar.gz: Not found in archive
tar: Coolblue.tar.gz: Not found in archive
tar: Concept_X.tar.gz: Not found in archive
tar: ConceptX-Gold.tar.gz: Not found in archive
tar: ColderXMMS.tar.gz: Not found in archive
tar: Cobalt-Obscura.tar.gz: Not found in archive
tar: CX2.tar.gz: Not found in archive
tar: BrushedMetal_Xmms.tar.gz: Not found in archive
tar: BlueSteel_xmms.tar.gz: Not found in archive
tar: BlueSteel.tar.gz: Not found in archive
tar: BlueIce.tar.gz: Not found in archive
tar: BlackXMMS.tar.gz: Not found in archive
tar: Aqua.tar.gz: Not found in archive
tar: Apple_Platinum_Amp.tar.gz: Not found in archive
tar: AdamAmp.tar.gz: Not found in archive
tar: Absolute_Blue-XMMS.tar.gz: Not found in archive
tar: Error exit delayed from previous errors.
```
What I am missing?


----------



## sk8harddiefast (Oct 8, 2010)

Well I found a solution 
Create a simple text named *sh* ,inside the folder with all tar archives.
Write this lines into *sh* file.

```
#/bin/sh
for file in `ls *.tar.gz`
do
tar zxvf $file
done
```
Save it and Run:

```
chmod +x sh
chown -R [B]user[/B] sh
./sh
```
Where *user* is the output of command: *whoami *
Done


----------



## da1 (Oct 8, 2010)

You don't even need a script for that. 

This will do the jos:

```
for i in `ls *.tar.gz| awk '{print $NF}'`; do tar -xvf $i; done
```


----------



## sk8harddiefast (Oct 8, 2010)

I will say why I prefer script.
If I must do this a lot of times, I write one time the script and I run it always.
Is faster to write the same thing again and again. A can even remember it


----------



## Alt (Oct 8, 2010)

```
echo *.tar.gz | xargs -n 1 tar -zxvf
```


----------



## jilles@ (Oct 9, 2010)

This is even simpler, and also works if the filenames contain special characters:

```
for i in *.tar.gz; do tar -xvf "$i"; done
```

If the sudo is actually needed, consider

```
sudo sh -c 'for i in *.tar.gz; do tar -xvf "$i"; done'
```
instead of placing sudo before tar to avoid needing to type your password several times if the process takes very long.


----------



## sk8harddiefast (Oct 9, 2010)

Thanks all of you  I will write them because I will forgot them for sure


----------



## ladless (Nov 25, 2010)

Hey, I'm pretty much trying to achieve the same thing.

Untar multiple archives. However, I am required to untar them in different directories that have the name of the source archive.

For example the archives are:

archive01.gz
archive02.gz
archive03.gz

I will need to untar them as follows:

archive01 <- that's a directory containing all files that were in archive01.gz
archive02
archive03

So far I tried

```
for a in `ls -1 *.gz`
do
 #now we're gonna remove all the extension (all that's after the dot) and use that as the folder name
 mkdir `echo ${a%%.*};`;
 tar xzvf $a;
done
```

However my code doesn't work for some reason.

Any thoughts would be kindly appreciated.

Thanks.


----------



## sk8harddiefast (Nov 25, 2010)

Look my second post. I think that is you need.


----------



## ladless (Nov 25, 2010)

i doubt it will work. where do you pass the mkdir parameter ? you don't even tell tar to create such dir. i am working on a variant now.


----------



## ladless (Nov 25, 2010)

Problem solved:

```
for a in `ls -1 *.gz`
do
 #now we're gonna remove the extension (all that's after the dot) and use that as the folder name
 mkdir `echo ${a%%.*};`;
 tar xzv --directory ${a%%.*} --file $a;
done
```


----------



## jalla (Nov 25, 2010)

What shell are you using?
If it's csh, try something like this

```
foreach t (*.gz)
 d=$t:r
 mkdir $d;
 (cd $d; tar -xzf ../$t)
end
```

Or similar in sh

```
for t in *.gz
do
 d=`echo $t|sed -e 's/.gz//'`
 mkdir $d
 tar -C $d -xzf $t
done
```


----------



## ladless (Nov 25, 2010)

Thank you jalla. I am using bash. Putting the script in a sh file and running "sh tarscript.sh" works for me.


----------

