ЭЛЕКТРОННАЯ БИБЛИОТЕКА КОАПП |
Сборники Художественной, Технической, Справочной, Английской, Нормативной, Исторической, и др. литературы. |
9.8. Removing a Directory and Its ContentsProblemYou want to remove a directory tree recursively without using SolutionUse the Example 9.3: rmtree1#!/usr/bin/perl # rmtree1 - remove whole directory trees like rm -r use File::Find qw(finddepth); die "usage: $0 dir ..\n" unless @ARGV; *name = *File::Find::name; finddepth \&zap, @ARGV; sub zap { if (!-l && -d _) { print "rmdir $name\n"; rmdir($name) or warn "couldn't rmdir $name: $!"; } else { print "unlink $name"; unlink($name) or warn "couldn't unlink $name: $!"; } } Or use Example 9.4: rmtree2#!/usr/bin/perl # rmtree2 - remove whole directory trees like rm -r use File::Path; die "usage: $0 dir ..\n" unless @ARGV; foreach $dir (@ARGV) { rmtree($dir); }
DiscussionThe File::Find module exports both a We have to use two different functions, Check first that the file isn't a symbolic link before determining if it's a directory. See AlsoThe |