Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a tape with multiple tar files on it. I do not know how many (it was created by a third party and sent to me with minimal information). How do I untar all the files without having to repeat the tar -xvf /dev/st2 command 100 times. I need to repeat this for 45 tapes, each with possibly a 100 files

share|improve this question

migrated from Oct 30 '12 at 22:56

This question came from our site for professional and enthusiast programmers.

Most *nix tape drivers have a non-rewinding variant (probably /dev/rst2 in your case). Use that as the device, and look into the mt program, that allows you to forward- and backward-space over individual files on the tape. – twalberg Oct 30 '12 at 21:22
They put a 100 archives on each tape? Or one archive with a 100 files? – Hennes Oct 30 '12 at 23:06
Seems like it. Its from a third party, I am trying to follow up with them to find out why they did that! – Anu Oct 30 '12 at 23:56
Seems like a script is the way to go. ServerFault will not let me post an answer to my question until tomorrow...but the answer is a simple script with a loop in it to extract the tar files – Anu Oct 30 '12 at 23:56
A csh one liner might even be enough. Something using the return value from tar. E.g. while (! tar -xf /dev/nst0 ) echo "Another file done. Repeating for the next one!" – Hennes Oct 31 '12 at 0:35
up vote 2 down vote accepted

If the tape drive has a non-rewinding interface you can use a loop to extract one file after another.

Typical examples of tape drive name are: /dev/rst2 for raw access to a rewinding SCSI tapedrive, and /dev/nrst2 or /dev/tape/nrst2 for the same device without rewinding (non rewinding)

This can be combined with the exit code from tar. Like all unix program exit code 0 means "success, no problems."

This you can use a somewhat ugly one liner in a shell:
while (! tar -xf /dev/nst0 ) echo "Another file done. Repeating for the next one!"

Or write a somewhat neater program:

#!/usr/bin/env bash
while [ return_value -eq 0 ]
   echo starting on file number $1
   return_value=$(( tar -xf /dev/nst0 ))
   echo file nu,ber $1 extracted from tape.
   counter=$(( $counter + 1 ))
echo Reached end of tape or tar returned an error.
echo exiting.

Untested script!

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.