如何在 Perl 中递归复制目录并过滤文件名?
如何在 Windows 系统上复制包含子目录(不包括与某个正则表达式匹配的文件或目录)的目录?
How do I copy a directory including sub directories excluding files or directories that match a certain regex on a Windows system?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我会做这样的事情:
I'd do something like this:
另一个选项是 File::Xcopy。 顾名思义,它或多或少模拟了 Windows xcopy 命令,包括其过滤和递归选项。
从文档中:
Another option is File::Xcopy. As the name says, it more-or-less emulates the windows xcopy command, including its filtering and recursive options.
From the documentation:
如果您碰巧使用的是类 Unix 操作系统并且可以访问
rsync (1)
,您应该使用它(例如通过system()
)。Perl 的 File::Copy 有点损坏(例如,它不复制 Unix 系统上的权限),因此如果您不想使用系统工具,请查看 CPAN。 也许 File::Copy::Recursive 可能有用,但是我没有看到任何排除选项。 我希望其他人有更好的主意。
If you happen to be on a Unix-like OS and have access to
rsync (1)
, you should use that (for example throughsystem()
).Perl's File::Copy is a bit broken (it doesn't copy permissions on Unix systems, for example), so if you don't want to use your system tools, look at CPAN. Maybe File::Copy::Recursive could be of use, but I don't see any exclude options. I hope somebody else has a better idea.
我不知道如何对副本进行排除,但您可以按照以下方式进行操作:
I don't know how to do an exclusion with a copy, but you could work something up along the lines of:
经典的答案是使用“
cpio -p
”:“
cpio
”命令处理实际复制,包括权限保留。 'cd $SOURCE_DIR; 的技巧 寻找 。 ...
' 处理从名称中删除源路径的前导部分。 调用“find
”的唯一问题是它不会遵循符号链接; 如果您想要的话,您需要添加“-follow
”。A classic answer would use '
cpio -p
':The '
cpio
' command deals with the actual copying, including permission preservation. The trick of 'cd $SOURCE_DIR; find . ...
' deals with removing the leading part of the source path from the names. The only problem with that invocation of 'find
' is that it won't follow symlinks; you need to add '-follow
' if that's what you want.