查找特定文件并复制到新文件夹

问题描述 投票:0回答:1

我试图找到多个目录中的所有txt文件,并使用

file
的内容来匹配每个txt文件并将它们复制到一个新目录(全部)。并非每个目录都会有匹配项 每个里面都有比txt文件更多的文件,但这些都是不需要的。下面执行但没有复制文件。谢谢你:)。

目录

123456789000
  123456789000_00a.txt
  123456789000_00b.txt
  123456789000_04a.txt
  123456789000_04b.txt
  123456789000_05a.txt
  123456789000_05b.txt
123456789111
  123456789111_00a.txt
  123456789111_00b.txt
123456789222
  123456789222_00a.txt
  123456789222_00b.txt

文件

123456789000_00
123456789000_04
123456789111_00

都想要

123456789000_00a.txt
123456789000_00b.txt
123456789000_04a.txt
123456789000_04b.txt
123456789111_00a.txt
123456789111_00b.txt

重击

for a in *; do  # loop and read into a
  [ -d "$a" ] || continue # ensure a is directory
    cd "$a" ## descend into each $a
     find . -type f -name "*.txt" -exec cp {} ${dest} \; # find txt file in directory and use file to match
    cd ..  ## go one directory back
done  <file  ## close loop
bash find
1个回答
1
投票

您的示例代码实际上根本不使用

file
的内容,它在 glob 上循环(大概与您的目录匹配),更改为每个目录并尝试获取所有 txt 文件。您在
$(dest}
中还存在语法错误,这可能就是没有复制任何内容的原因。

这更接近您所描述的:

while IFS= read -r line ; do
  find -type f -name "${line}*txt" -exec cp {} "${dest}" \; 
done < file
© www.soinside.com 2019 - 2024. All rights reserved.