如何将匹配模式的文件复制到名称中包含模式的子目录
我有以下目录结构:
folder-1.2.3/subA/subB/file.txt
folder-1.2.3/subA/subB/other_sub/file2.txt
folder-1.2.4/subA/subB/file.txt
folder-1.2.4/subA/subB/other_sub/file2.txt
我想匹配以 folder-*/subA/subB
开头的所有路径并将其内容复制到 /new_dst/prefix-*
其中 *
是匹配的字符串(在我的例子中是 1.2.3 或 1.2.4)。 结果是:
new_dst/prefix-1.2.3/file.txt
new_dst/prefix-1.2.3/other_sub/file2.txt
new_dst/prefix-1.2.4/file.txt
new_dst/prefix-1.2.4/other_sub/file2.txt
我可以使用任何标准的 Linux 命令(cp、find、grep、aws、sed ..)。我最初的想法是使用 find (find . -path "folder-*/subA/subB"
) 但我无法直接访问仅匹配整个路径的模式。这就是为什么我尝试通过 sed 解析它(例如 .. -exec sed -i "s/folder-//g"
),但它开始看起来比应有的更复杂。
I have following directory structure:
folder-1.2.3/subA/subB/file.txt
folder-1.2.3/subA/subB/other_sub/file2.txt
folder-1.2.4/subA/subB/file.txt
folder-1.2.4/subA/subB/other_sub/file2.txt
I want to match all paths starting with folder-*/subA/subB
and copy its content to /new_dst/prefix-*
where *
is matched string (1.2.3 or 1.2.4 in my case).
The outcome would be:
new_dst/prefix-1.2.3/file.txt
new_dst/prefix-1.2.3/other_sub/file2.txt
new_dst/prefix-1.2.4/file.txt
new_dst/prefix-1.2.4/other_sub/file2.txt
I can use any standard linux command (cp, find, grep, aws, sed..). My initial idea was to use find (find . -path "folder-*/subA/subB"
) but I don't have direct access what pattern was matched only the whole path. That's why I tried to parse it through sed (for example .. -exec sed -i "s/folder-//g"
) but it's starting to look more complicated than it should be.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
建议首先使用
find
命令和-path
选项找到所需的文件。找到您的文件后。将找到的文件输入 cp 命令。
Suggesting first to locate the desired files using
find
command and-path
option.Once your files are located. Feed the found files into
cp
command.