Click to See Complete Forum and Search --> : [SOLVED] batch untar . . .
techwise
06-09-2005, 12:14 AM
I have a dir with almost 200 tar.gz archives and I want to extract all of them with one command/script.
Have any of you done something like this and how?
Thanks
Mike
Try this:
for i in *.tar.gz ; do tar -zxf $i ; done
techwise
06-09-2005, 12:32 AM
Youre tha man! Thanks.
Would you be so kind as to explain what this did?
Mike
Gertrude
06-09-2005, 01:01 AM
It creates a for loop using "i" as a variable so it will go though all the filnames till its done..
The "*.tar.gz" will match everything that ends in ".tar.gz"
The "do tar -zxf $i" is where it untars the files using "$i" as the variable from the previous part. The $ tells the shell to use a variable. You dont have to use "i" it could be anything. Its like a place holder for data
"done" is what it says..
Edit:
you could have just used the wildcard "*" with the tar command though...
tar -zxvf *.tar.gz
techwise
06-09-2005, 01:17 AM
Thanks for the explanation.
I did try using the wildcard with tar "tar -xzvf *tar.gz" and it returned an error.
Your solution worked perfectly. Thanks again.
Mike
Icarus
06-09-2005, 08:12 AM
you can also use find to do the task
find . -name '*.tar.gz' -exec tar xzf {} \;
bwkaz
06-09-2005, 07:13 PM
Originally posted by Gertrude
you could have just used the wildcard "*" with the tar command though... Actually, you can't do that. ;)
tar only takes the archive name immediately after the -f option. Any further non-option arguments that it sees are interpreted as files to extract from the archive, not extra archives to extract from. Yes, this is inconsistent, but there are workarounds (the for loop and the find -exec option), so it's not too terrible.
hammer123
06-12-2005, 02:00 AM
a very odd inconsistency indeed. what is its purpose? i mean i bet its done to remain standard but is that standard behavior due to a fluke of the way tar was developed or does it have a productive reason?
error27
06-12-2005, 02:30 AM
With tar the '-' is optional. Back in the day they didn't do things in a standard way. If you allowed more than one file you couldn't tell which was a filename and which was an option.
bwkaz
06-12-2005, 01:25 PM
Also, tar was used as a tape archiver (and still is -- plus, that's where its name came from). There's only one tape in the drive at a time.
So when it moved to being able to write to a file instead of a tape, it only needed one filename to write to. Or read from.