首页 > 解决方案 > 使用命令查找和复制,但使用带有名称的 list.txt

问题描述

我有一个具有不同文件名的list.txt,我想在子目录中找到所有这些 3600 文件名,然后复制到 /destination_folder。

我可以使用命令 find /path/ {file.txt} 然后复制到 /destination_folder 吗?

list.txt应具有以下文件名/行:

test_20180724004008_4270.txt.bz2
test_20180724020008_4278.txt.bz2
test_20180724034009_4288.txt.bz2
test_20180724060009_4302.txt.bz2
test_20180724061009_4303.txt.bz2
test_20180724062010_4304.txt.bz2
test_20180724063010_4305.txt.bz2
test_20180724065010_4307.txt.bz2
test_20180724070010_4308.txt.bz2
test_20180724071010_4309.txt.bz2
test_20180724072010_4310.txt.bz2
test_20180724072815_4311.txt.bz2
test_20180724073507_4312.txt.bz2
test_20180724074608_4314.txt.bz2
test_20180724075041_4315.txt.bz2
test_20180724075450_4316.txt.bz2
test_20180724075843_4317.txt.bz2
test_20180724075843_4317.txt.bz2
test_20180724080207_4318.txt.bz2
test_20180724080522_4319.txt.bz2
test_20180724080826_4320.txt.bz2
test_20180724081121_4321.txt.bz2
................................

标签: bashfind

解决方案


您可以使用echosed

echo $(sed "s/.*/\"\0\"/;s/^/ -name /;s/$/ -o/;$ s/-o//" list.txt)

这将输出要在find命令中使用的文件列表:

-name "file1.txt.bz2" -o -name "file2.txt.bz2" -o -name "file3.txt.bz2"

然后使用-exec cp -t targetDir {} +in find复制文件:

find \( $(eval echo $(sed "s/.*/\"\0\"/;s/^/ -name /;s/$/ -o/;$ s/-o//" list.txt)) \) -exec cp -t targetDir {} +

推荐阅读