Here's an updated version of the little tarball cleaner I wrote a
while back. I've figured out how to get the information straight
from the tarball itself.
If you've untarred a tarball, and want to remove all the files that
it dumped out, you can use this. I have it in my bin/ directory and
call it when I need it. It's handy when you have a tarball that
empties in the current directory instead of some subdirectory.
It should be invoked from the directory that the tarball was
untarred from.
--- Cut & Paste ---
#!/usr/bin/perl -w
# Removes files by using the tarball to get the files
# by Rob Hudson (02012000)
my $tarball = $ARGV[0] if ($ARGV[0] ne '')
|| die "Must specify a tar file.\n";
my @files;
my @dirs;
# Assume it is gzipped if it ends in .tgz or .gz
# otherwise assume it is a plain tar file.
if ($tarball =~ m/\.t?gz$/) {
@files = `tar -tzf $tarball`;
}
else {
@files = `tar -tf $tarball`;
}
# Removes files. If it ends with '/', then its a directory
# Push those into the directory array and move on.
foreach $file (@files) {
chomp $file;
if ($file =~ m!.*/$!) {
push @dirs, $file;
next;
}
print "Removing file: $file...\n";
system ("rm -f $file");
}
# Using pop here to go backwards thru the array since directories
# can be nested.
while ($dir = pop(@dirs)) {
print "Removing directory: $dir\n";
system ("rmdir $dir");
}
--- End ---