Created
June 1, 2009 09:36
-
-
Save kentfredric/121325 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use strict; | |
use warnings; | |
# You'll likely need to `install` this. | |
# But don't worry, its just a text file. | |
use File::Find::Rule (); | |
use Path::Class qw( file ); | |
use File::Basename qw( basename ); | |
my %unpacked = (); | |
while (1) { | |
# Exclude anything thats already been unpacked from the unpack list | |
my $exclude = File::Find::Rule->file->name( keys %unpacked ); | |
# Scan /downloads for files ending with .tar.gz that are not yet unpacked | |
my @to_unpack = | |
File::Find::Rule->file->name('*.tar.gz')->not($exclude)->in('/downloads'); | |
for (@to_unpack) { | |
print "Found $_\n"; | |
} | |
# If you find nothing, break the inifine while loop | |
last if not(@to_unpack); | |
# Unpack found items. | |
for my $archive (@to_unpack) { | |
# record the archive as unpacked in the hash-map | |
$unpacked{$archive} = 1; | |
my $file = file($archive); | |
my $parent_dir = $file->dir; | |
# set this to 1 if you want to create a directory for each unpacked archive | |
if (0) { | |
# Create a directory named foo.tar.gz_unpacked | |
$parent_dir = $parent_dir->subdir( basename($archive) . "_unpacked" ); | |
# Make sure it exists with mkdir | |
$parent_dir->mkpath; | |
} | |
print "Extracting $file to $parent_dir \n"; | |
# Could use Archive::Tar or something similar to do this | |
# and this code would suddently become platform agnostic! | |
system 'tar', '-C', $parent_dir, '-xf', $archive; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment