从列表中查找文件并将它们复制到新目录中

Mor*_*mid 0 macos terminal find

我想通过 OS X 的命令行终端查找文件,然后将找到的文件复制到新目录。我要查找的文件的文件名在 listexample.txt 文件中,其中大约有 5000 个文件名。

文件 listexample.txt 如下所示:

1111 00001 55553.bmp
1113 11312 24125.bmp
…
Run Code Online (Sandbox Code Playgroud)

我试过这样的事情:

find /directory -type f "`cat listexample.txt`" -exec cp {} …
Run Code Online (Sandbox Code Playgroud)

但无法让它运行。

我现在有这个,但它不起作用:

cat listexample.txt | while read line; do grep "$line" listexample.txt -exec find /directorya "$line" -exec cp {} /directoryb \; done
Run Code Online (Sandbox Code Playgroud)

这个想法是读取列表 example.txt 的行,然后使用 grep 获取该行,在目录 a 中找到文件,然后将找到的文件复制到新目录 b。我认为由于我的文件名的性质,见上文,名称中也存在空格问题。

我也开始使用这种方法来查看发生了什么,但并没有走多远。

for line in `cat listexample.txt`; do grep $line -exec echo "Processing $line"; done
Run Code Online (Sandbox Code Playgroud)

Mor*_*mid 6

这是查找和复制脚本 (copy.sh) 的解决方案,以防有人遇到类似问题:

首先,使用以下命令授予脚本权限:chmod +x fcopy.sh 然后使用以下命令运行它:./fcopy.sh listexample.txt

脚本内容:

#!/bin/bash
target="/directory with images"

while read line
do
    name=$line
    echo "Text read from file - $name"
    find "${target}" -name "$name" -exec cp {} /found_files \;    

done < $1
Run Code Online (Sandbox Code Playgroud)

干杯