[英]How to develop a script in perl which can delete the data in sub folders recursively and also the parent directory?
Hi have the folder structure as follows 嗨,文件夹结构如下
mishel/ #Parent directory
mike1 #sub file of mishel
minni1 #sub file of mishel
rosy1 #sub file of mishel
rocky/ #sub directory of mishel
missi #subfile of rocky
marsh #subfile of rocky
milly #subfile of rocky
monu/ #sub directory of rocky
mike #sub file of monu
minni #sub file of monu
rosy #sub file of monu
so here is my question: I want to use a script which automatically deletes all the files and sub folders at a single shot. 所以这是我的问题:我想使用一个脚本,它可以自动删除所有文件和子文件夹。
If we use the function "rmdir(dirname)" the directory must empty. 如果我们使用函数“ rmdir(dirname)”,则目录必须为空。 So is there any chance of deleting starting from the sub directory to the parent directory.
因此,是否有可能从子目录删除到父目录。
Write a function that takes a directory as an argument. 编写一个将目录作为参数的函数。 Have that function do a
readdir
in the directory. 让该函数在目录中执行
readdir
。 For each item returned by readdir, check if it's a file - if so, delete it. 对于readdir返回的每个项目,请检查它是否是文件-如果是,则将其删除。 If it's a directory, call the function again with the subdirectory as an argument.
如果是目录,请以子目录为参数再次调用该函数。 If there are no items (other than
.
and ..
), delete the directory. 如果没有任何项目(
.
和..
除外),请删除目录。
use Path::Class;
my $dir = dir('foo');
$dir->recurse(callback => sub {
unlink $_[0] if !$_[0]->is_dir;
});
$dir->rmtree(0, 1);
Or : 要么 :
use File::Path ('rmtree');
rmtree('foo', 0, 0);
See http://www.perlmonks.org/bare/?node_id=605930 参见http://www.perlmonks.org/bare/?node_id=605930
perl -e 'system "find mishel -delete"'
尝试使用File::Path
模块的remove_tree
函数
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.