Re: perl question
On Tue, 17 Jul 2001, Craig Dickson wrote:
> > What would be a nice command to remove a dirtory that had files in it?
> rm -rf directory
But if you don't want to spawn a sh.
Check all this as it is off the cuff and I would usually
use backticks or system to do this type of thing.
#!/usr/bin/perl -w
use strict;
use File:Path;
rmtree("/"); # always be prepared
> > Even better.... what would be a nice command to delete all files
> > in one directory... (leaving the directory intact)
This is a vague question, so you get various answers.
Are subdirectories files?
Are hidden files to be counted?
Are files in subdirectories in the directory?
What about lost+found subdirectories?
> rm -rf directory/*
This is shell to remove all unhidden entries in a directory
and all their contents. Leaves the .files in directory.
The perl is:
#!/usr/bin/perl -w
use strict;
use File:Path;
my @list = <"directory/*">;
foreach my $en (@list) {
unlink $en if not -d $en;
rmtree($en) if -d $en;
}
> which will delete everything in a directory, including subdirectories; or
>
> find . -type f | xargs rm -f
This removes all the regular files in . and in its subdirectories.
#!/usr/bin/perl -w
use strict;
use File:Find;
find( sub { unlink @_[0] if -f @_[0] }, "directory"):
This would remove everything but directories.
find( sub { unlink @_[0] if not -d @_[0] }, "directory"):
> to delete all files in a directory or its subdirectories, but keep the
> directory structure intact.
>
> Perl isn't required.
My treatment here is naive, but hopefully
brings some of the issues to light.
Look at the boot scripts under /etc which clean out
/tmp, to get an idea of some of the issues to consider.
--
rob Live the dream.
Reply to: