如何将匹配模式的文件复制到名称中包含模式的子目录

发布于 2025-01-09 00:42:00 字数 725 浏览 0 评论 0原文

我有以下目录结构:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

-小熊_ 2025-01-16 00:42:00

建议首先使用 find 命令和 -path 选项找到所需的文件。

 find root-folder -path "*/subA/subB/*" -name "*.txt"

找到您的文件后。将找到的文件输入 cp 命令。

 cp $(find root-folder -path "*/subA/subB/*" -name "*.txt") target-folder

Suggesting first to locate the desired files using find command and -path option.

 find root-folder -path "*/subA/subB/*" -name "*.txt"

Once your files are located. Feed the found files into cp command.

 cp $(find root-folder -path "*/subA/subB/*" -name "*.txt") target-folder
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文