in reply to Is this the most elegant way to code directory lookup?
- unless (-d $srcdir && -d $destdir ) {die "Error: $!";} + for($srcdir,$destdir) { ! -d $_ and die "Directory '$_' - $!\n" }
You wouldn't know which directory is missing.
- chdir $srcdir; - opendir(INDIR,$srcdir) || die "Can't open directory: $!"; + chdir $srcdir or die "Can't chdir to '$srcdir': $!\n"; + open my $indir, '.' or die "Can't read '.' in $srcdir: $!\n";
Point is, you are already in $srcdir, so you are here->.
Instead of using the ugly and hard to grok
unless (-d || $_ eq "." || $_ eq "..") { # . and .. are always -d
in the loop, after having lumped all directory entries (files, directories, symlinks, sockets, device files) into @files (are you really looking for files only? and making tar.gz's containing a single file each?) it would be more elegant
my @files = grep { -f } readdir $indir;
to stuff only the interesting files into @files.
`tar -cz $_ -f $_.tar.gz`; `mv *.gz $destdir`;
Really? what if your directory contains *.gz files? Your asking tar to complain. And use system, not backticks.
I would pack all files into one tar file:
chdir $srcdir or die "Can't chdir to '$srcdir': $!\n"; open my $indir, '.' or die "Can't read '.' in $srcdir: $!\n"; (my $tarfile = $srcdir) =~ s|.*/||; $tarfile = "$destdir/$tarfile-" . time . '.tar'; my @files = grep { -f and -M <= 1 } readdir $indir; # packing all files at once doesn't work with large directories # (limit of command line length) while(my $file = shift @files) { system ('tar', 'uf', $tarfile, $file) and die "Can't pack '$file' into '$tarfile' (exitcode $?)\n"; } system ('gzip', $tarfile) and die "Couldn't gzip $tarfile (exitcode $?)\n";
--shmem
_($_=" "x(1<<5)."?\n".q·/)Oo. G°\ /
/\_¯/(q /
---------------------------- \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
|
|---|