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:

wget https://github.com/paulp/sbt-extras/tarball/master
tar --strip-components=1 -xf 'paulp-sbt-extras-d6c7222.tar.gz' 'paulp-sbt-extras-d6c7222/sbt'

But I want something like this:

wget https://github.com/paulp/sbt-extras/tarball/master -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 https://github.com/paulp/sbt-extras/tarball/master -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?


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

wget https://github.com/paulp/sbt-extras/tarball/master -O sbt-extras.tar.gz
tar --strip-components=1 -xf 'sbt-extras.tar.gz' -O | grep sbt > sbt

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:




  • 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 clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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