根据文本文件中的部分名称将文件复制或移动到另一个目录

时间:2017-11-23 19:38:27

标签: linux bash shell command-line scripting

我想根据文本文件中的部分文件名将目录<connectionStrings> <add name="ProjectDbEntity" connectionString="metadata=res://*/Data.ProjectEntityModel.csdl|res://*/Data.ProjectEntityModel.ssdl|res://*/Data.ProjectEntityModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=company-ai-Projectserver.database.windows.net;initial catalog=company.DB_Test;persist security info=True;user id=Project;password=Project1234$$;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/> </connectionStrings> 中的一些文件复制到目录A

我尝试了下面的命令,但它无法正常工作

B

文件名的格式为for fn in $(cat filename.txt); do find . -type -f -name '$fn*' \ -exec rsync -aR '{}' /tmp/test2 \; abcd-1234-opi.txt。我必须从文件名中grep数字然后将这些文件复制到另一个文件夹,我在文本文件中有数字。

有人可以指导我吗?

3 个答案:

答案 0 :(得分:0)

如果理解正确,要搜索的模式是第一个-之后的数字的前3位数,如下所示:

abd-12312-xyz
    ^^^

88-45644-oio
   ^^^

qwe-78908-678
    ^^^

这是写这个的一种方式:

while read line; do
    pattern=${line#*-}
    pattern=${pattern:0:3}
    find . -type f -name "*$pattern*" -exec rsync -aR {} /tmp/test2 \;
done < patterns.txt

如果你的输入是“veeram_20171004-104805_APOLLO_9004060859-all.txt”, 你要提取“9004060859”, 然后你可以尝试找到一个不同的逻辑来提取它。 例如, “在最后一次' - '之后切断,并在最后一次'_'之前切断所有内容。 你可以这样写:

pattern=${line%-*}
pattern=${pattern##*_}

答案 1 :(得分:0)

如果我理解你的问题是正确的:

for file in $(<./example); do cp "${file}" /tmp/test2/; done

for file in $(<./example); do find . -type f -name ${file} -exec rsync -aR {} /tmp/test2/ \; ; done

答案 2 :(得分:0)

应该避免重复运行find。而是将表达式以编程方式计入find命令行。

awk 'BEGIN { printf "find . -type -f \\("; sep="" }
   { printf "%s -name \047*%s*\047", sep, $0; sep=" -o" }
   END { printf " \\) -exec rsync -aR '{}' /tmp/test2 \\;\n" }' filename.txt |
sh

将管道留给sh进行测试。