#!/usr/local/bin/perl -w ########################################### # tarrename # 2006, Mike Schilli ########################################### use strict; use Getopt::Std; use Pod::Usage; use Archive::Tar::Wrapper; getopts("hv", \my %opts); my($tarball, $newname) = @ARGV; if(!$newname) { pod2usage("Argument error"); } pod2usage() if $opts{h}; if(! -f $tarball) { pod2usage("Can't read $tarball ($!)"); } my($oldname, $ext) = ($tarball =~ /(.*)\.((?:tar\..*)|(?:tgz))$/); if(! $ext) { pod2usage("Can't separate name from .tgz extention in $tarball"); } my $arch = Archive::Tar::Wrapper->new(); $arch->read($tarball) or pod2usage("Cannot read tarball $tarball ($!)"); $arch->list_reset(); my $e = $arch->list_next(); my($entry, $full_path) = @$e; (my $topdir = $full_path) =~ s/\/\Q$entry\E$//; my($tardir) = <$topdir/*>; rename $tardir, "$topdir/$newname" or podusage("Cannot rename $tardir to $topdir/$newname ($!)"); $arch->write("$newname.$ext", 1); __END__ =head1 NAME tarrename - Rename a tarball and its top directory =head1 DOWNLOAD _SRC_HERE_ =head1 SYNOPSIS tarrename Foo-Bar-x.xx.tar.gz Baz-y.yy =head1 DESCRIPTION C unpacks a given tarball, renames its top directory to a new name and then wraps it back up and writes it back as a tarball under the new name. =head1 EXAMPLES $ tarrename Foo-Bar-x.xx.tar.gz Baz-y.yy.tar.gz =head1 AUTHOR 2006, Mike Schilli