perl – 删除给定目录中的子目录和文件

前端之家收集整理的这篇文章主要介绍了perl – 删除给定目录中的子目录和文件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想删除目录中的所有子目录和文件,但不删除目录本身.例如,如果我在变量中传递“Sample”目录,并且“Sample”目录包含三个子目录和3个文件,我想删除所有3个目录和3个文件.实际上,“Sample”目录可以包含许多子目录和文件.

解决方法

ETA:这实际上是在perlfaq5: How do I delete a directory tree?

使用File::Path核心模块.

perl -MFile::Path=remove_tree -we 
    'remove_tree("Sample",{keep_root=>1}) or die $!'

keep_root选项将导致remove_tree保留顶级目录:

keep_root => $bool

When set to a true value,will cause all files and subdirectories to be removed,except the initially specified directories. This comes in handy when cleaning out an application’s scratch directory.

猜你在找的Perl相关文章