简体   繁体   English

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

[英]How to copy files matching pattern to subdirectory with pattern in name

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).我想匹配以folder-*/subA/subB开头的所有路径,并将其内容复制到/new_dst/prefix-* ,其中*是匹配的字符串(在我的例子中是 1.2.3 或 1.2.4)。 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..).我可以使用任何标准的 linux 命令(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.我最初的想法是使用 find ( find. -path "folder-*/subA/subB" ) 但我无法直接访问只匹配整个路径的模式。 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.这就是为什么我试图通过 sed 解析它(例如.. -exec sed -i "s/folder-//g" )但它开始看起来比它应该的更复杂。

Suggesting first to locate the desired files using find command and -path option.建议首先使用find命令和-path选项找到所需的文件。

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

Once your files are located.找到文件后。 Feed the found files into cp command.将找到的文件输入cp命令。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM