我想删除目录中的所有子目录和文件,但不删除目录本身.例如,如果我在变量中传递“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.