Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

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 am trying to pipe a tar/gzip archive into tar to decompress it. The script I have is part of a self extracting installer, where my archive is appended to the script. This works fine on linux, and the script looks like this:

export TMPDIR=`mktemp -d /tmp/selfextract.XXXXXX`

echo "TEMP: $TMPDIR"

ARCHIVE=`awk '/^__ARCHIVE_BELOW__/ {print NR + 1; exit 0; }' $0`

tail -n+$ARCHIVE $0 | tar xz -C $TMPDIR

exit 0


The tar archive as a string is after the ARCHIVE_BELOW but I omitted it from here since it's huge.

However, when I do this on FreeBSD I get the following error:

tar: Failed to open '/dev/sa0'

I read that this is because free BSD expects to read from that device by default and you can tell it to read from stdin by passing -f - like so:

tail -n+$ARCHIVE $0 | tar zxf - -C $TMPDIR

However, when I do this I just get the error:

tar: Damaged tar archive
tar: Retrying...

Can anyone point out what I am doing wrong here? I need to do it this way (Via piping) for efficiency reasons.


share|improve this question
Adding -v should at least tell you where it the process. You can also try to use -j instead of -z but it might be irrelevant with newer version. – Alex Jun 10 '14 at 17:57
Unfortunately that does not give me any more information or help resolve the issue. Thanks for the advice though. – Casey Jordan Jun 10 '14 at 18:42

In the first command:

echo $ARCHIVE | tar xz -C $TMPDIR

This is piping the file name into tar, not the contents of the file. How this is working is beyond me.

In the final command you list:

tail -n+$ARCHIVE $0 | tar zxf - -C $TMPDIR

I think this is specifying the file name as the argument to "tail", which probably isn't going to work either. Even if ${ARCHIVE} is a number, you still can't pipe the tail of an archive to tar, you need to give it the whole archive. Why are you calling tail here at all? A better solution might be:

tar xzvf ${ARCHIVE} -C ${TMPDIR}

share|improve this answer
Sorry, I tried to simplify this example and it seems I managed part of it. I have added a much fuller example above that should make this clearer. – Casey Jordan Jun 13 '14 at 1:26
I think the answer that getting the tail of an archive doesn't work still applies. – Steve Wills Jun 16 '14 at 0:05
Why does this work on linux then? – Casey Jordan Jun 18 '14 at 15:17

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.