我想从目录中删除所有子目录和文件,而不是目录本身。例如,如果我在变量中传递“Sample”目录,并且“Sample”目录包含三个子目录和 3 个文件,我想删除所有这 3 个目录和 3 个文件。实际上,“Sample”目录可以包含许多子目录和文件。
最佳答案
预计到达时间:这实际上在 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 - 删除给定目录中的子目录和文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9750835/