简体   繁体   English

如何在perl中开发一个脚本,该脚本可以递归删除子文件夹和父目录中的数据?

[英]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.

相关问题 如何使用命令行递归删除特定目录下的文件和文件夹? - How to recursively delete files and folders under a certain directory using command line? 递归删除文件和文件夹 - Delete files & folders recursively Bash脚本以递归方式逐步浏览文件夹并删除文件 - Bash script to recursively step through folders and delete files 如何使用bash递归删除大量不同文件夹中的一组目录? - How can I recursively delete a set of directories across a large array of different folders using bash? 如何递归读取文件夹及其子文件夹并搜索文件 - How to recursively read a folder and its sub-folders and search for a file bash:将父目录下所有匹配的文件递归移动到新目录下 - bash: recursively move all matched files in sub directory of parent to new sub directory 父目录的脚本创建tar,它将具有其子目录的各个tar - Script create tar of a Parent directory which will have individual tar's of its sub directories 如何编写UNIX命令或脚本来删除当前目录下所有子文件夹中相同类型的文件? - How to write a unix command or script to remove files of the same type in all sub-folders under current directory? 递归删除C中的目录 - Recursively delete a directory in C 删除目录中与文本列表不匹配的文件和文件夹 - Delete files and folders in a directory which don't match a text list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM