根据文本文件中的部分名称将文件复制或移动到另一个目录 [英] Copy or move files to another directory based on partial names in a text file

查看:77
本文介绍了根据文本文件中的部分名称将文件复制或移动到另一个目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据部分文件名将目录 A 中的某些文件复制到目录 B 中文本文件。

I want to copy some files from directory A to directory B on basis of partial filenames which are in a text file.

我尝试了以下命令,但不起作用

I tried the command below, but it's not working

for fn in $(cat filename.txt); do find . -type -f -name '$fn*' \
    -exec rsync -aR '{}' /tmp/test2 \;

文件名的格式为 abcd-1234-opi.txt rety-4567-yuui.txt 。我必须从文件名中提取数字,然后将这些文件复制到另一个文件夹,我在文本文件中有数字。

File names are in format abcd-1234-opi.txt, rety-4567-yuui.txt. I have to grep numbers from file names and then copy those files to another folder, I have numbers in a text file.

有人可以引导我吗?

推荐答案

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

Running find repeatedly should probably be avoided. Instead, factor the expressions into the find command line programmatically.

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 进行测试。

这篇关于根据文本文件中的部分名称将文件复制或移动到另一个目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆