如何递归复制以“abc”开头的目录在 Linux/Unix 上?
我有一个目录 ~/plugins/
,里面有很多子目录。如果我想在其他地方创建以 abc 开头的子目录的备份,我可以使用一行复制命令来完成此操作吗?我假设这样的东西会起作用(但事实并非如此):
cp -R ~/plugins/abc* ~/destination/
如果可能的话,我宁愿使用单行命令,因为我也想对 rsync 使用相同的语法,并且如果我必须做类似的事情
find ~/plugins/ -type d -name "abc*" -exec cp -R {} ~/destination;
那么这对于 cp 命令来说效果很好,但这意味着我必须为每个目录运行 rsync 一次,而这似乎效率不高:(
I have a directory ~/plugins/
and inside there are many sub-directories. If I wanted to create a backup somewhere else of just the sub-directories starting with abc
could I do that with a one line copy command? I would assume something like this would work (but it doesn't):
cp -R ~/plugins/abc* ~/destination/
I would rather use a one-line command, if possible, because I would also like to use the same syntax for rsync, and if I have to do something like
find ~/plugins/ -type d -name "abc*" -exec cp -R {} ~/destination;
then that works fine for the cp
command but it would mean that I would have to run rsync once for each directory and that just doesn't seem efficient :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不确定为什么你尝试的方法不起作用(但是“复制”命令是什么?),但这至少在 Linux 上有效:
Not sure why what you're trying didn't work (but what is the "copy" command?), but this works on Linux at least:
这是我仍然经常使用的一个老技巧:
其中
p
保留属性,而~/destination
可以在任何地方。Here is an old trick I still use frequently:
where the
p
preserves attributes, and~/destination
can be anywhere.可以将
find
的输出与rsync
一起使用:find
中的-print0
和rsync 中的 --from0
确保我们正确处理带有空格的文件,--files-from=-
表明我们正在从中读取文件列表标准输入It is possible to use the output of
find
withrsync
:-print0
infind
, and--from0
inrsync
makes sure that we handle files with spaces correctly--files-from=-
states that we are reading a list of files from stdin将此代码片段命名为例如 copyfile
TESTED : 用法 : ./copyfile
name this code snippet for example copyfile
TESTED : usage : ./copyfile