How can I untar or unzip multiple tarballs at once?
As the tar command was originally designed to read from and write to tape devices (tar - Tape ARchiver), you can specify only filenames to put inside an archive or to extract out of an archive (e.g. tar x myfileonthe.tape). There is an option to tell tar, that the archive is not on some tape, but in a file: -f. This option takes exactly one argument, the filename of the file containing the archive. All other (following) filenames are taken like mentioned before: archive members:
tar -x -f backup.tar myfile.txt # OR (more common syntax IMHO) tar xf backup.tar myfile.txt
Now there's a common mistake, imagine a directory containing the following archive-files you want to extract all by one:
$ ls backup1.tar backup2.tar backup3.tar
Maybe you think of tar xf *.tar, let's see:
$ tar xf *.tar tar: backup2.tar: Not found in archive tar: backup3.tar: Not found in archive tar: Error exit delayed from previous errors
What happened? The Shell replaced your *.tar by the matching filenames, you really wrote:
tar xf backup1.tar backup2.tar backup3.tar
And taken the statements from the beginning, it means: "extract the files backup2.tar and backup3.tar from the archive backup1.tar", which will of course only succeed, when there are such filenames stored in the archive.
The solution is relatively easy: extract the contents of all archives ONE BY ONE. As we use a UNIX shell and we are lazy, we do that with a loop:
for tarname in *.tar; do tar xf "$tarname" done
What happens? The for-loop will iterate through all filenames matching *.tar and call tar xf for each of them. That way you extract all archives one-by-one and you even do it automagically.
The second common archive type in these days is ZIP. The command to extract contents from a ZIP file is unzip (who would have guessed that!). The problem here is the very same: unzip takes only one option specifying the ZIP-file. So, you solve it the very same way:
for zipfile in *.zip; do unzip "$zipfile" done
Not enough? Ok. There's another variant, unzip can do that: It takes shell-like patterns to specify the ZIP-file names. And to avoid interpretion of those patterns by the shell, you need to quote them. unzip itself and NOT the shell will interpret *.zip in this case:
unzip "*.zip" # OR, to make more clear what happens: unzip \*.zip