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'm trying write a script that will download a tarball from github and extract a single file from it.

However the top level directory inside the tarball has some random characters in it, which I think change when the repo/tarball is updated, making it difficult to reliably specify the path to the file I want to extract.

This works:

tar --strip-components=1 -xf 'paulp-sbt-extras-d6c7222.tar.gz' 'paulp-sbt-extras-d6c7222/sbt'

But I want something like this:

wget -O sbt-extras.tar.gz
tar --strip-components=1 -xf 'sbt-extras.tar.gz' '[generic tld placeholder]/sbt'

Basically I want to replace all randomly-generated characters with something generic, so I don't have to edit the script anytime the random characters change.

I thought about writing the tar output to standard out and then piping into something that could grab just the sbt file and discard the rest, but not sure what tool to use for that:

wget -O sbt-extras.tar.gz
tar --strip-components=1 -xf 'sbt-extras.tar.gz' -O - | [grab the sbt file, write it, and discard everything else]

Any ideas?

share|improve this question

Found the answer, was so simple, grep to the rescue:

wget -O sbt-extras.tar.gz
tar --strip-components=1 -xf 'sbt-extras.tar.gz' -O | grep sbt > sbt
share|improve this answer

Maybe some Perl?

use Archive::Tar;

$targetfile = 'sbt';

my $tar = Archive::Tar->new;

my @items = $tar->get_files;
for $item (@items) {
  if ($item->name eq "$targetfile") {

Untested, but look at:


share|improve this answer
Thanks cjc. I try and try without the light bulb going on, then as soon as I post the question on a forum, the answer just pops into my head. Appreciate your perl script though, learn something new every day. – Kurtosis Mar 17 '12 at 21:27

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.