Gossamer Forum
Home : General : Perl Programming :

archiving the individual directory

Quote Reply
archiving the individual directory
Hi guys can someone help me how can I do a script the will archive all the directory individualy using perl script
Example
Dir --- Dir1 Dir2 Dir3 CDDir4 ADir5 ..............so on so fort

to

DirArchive --Dir1.tgz Dir2.tgz Dir3.tgz CDDir4.tgz ADir5.tgz ..............so on so fort

note that the original Dir is maintained and you don't know all the contents of Dir1.


Thanks.
Quote Reply
Re: [zerocool] archiving the individual directory In reply to
Hi,

This is fully untested, but should hopefully work :)

Code:
#!/usr/local/bin/perl -w

use strict;
my $folder = '/full/path/to/folder';

opendir(DIR, $folder) || die "Cant read $folder. Reason: $!";
while (defined($file = readdir(DIR))) {
next if $file =~ /\./; #skip normal files, as we only want to do folders...
if (chdir($folder)) {
system("tar -cvvf $file ../$file.tar");
print "Compressed $filde into $folder/$file.tar \n";
}
}
closedir(DIR);

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!