我有一个带有不同文件名的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
................................
您可能想要列出一个目录中的所有文件,然后使用您的列表迭代找到的文件列表。
首先将找到的文件列表保存到文件
find . -type f > foundFiles.txt
然后你需要使用你的文件来搜索另一个
cat list.txt | while read line
do
if [ `grep -c "${line}" foundFiles.txt` ]
then
cp -v $(grep "${line}" foundFiles.txt) /destination_folder/
fi
done
我会让你把这个基础做成一个脚本,以便再次使用。
您可以使用echo和sed:
echo $(sed "s/.*/" "/;s/^/ -name /;s/$/ -o/;$ s/-o//" list.txt)
这会输出find命令中要使用的文件列表:
-name "file1.txt.bz2" -o -name "file2.txt.bz2" -o -name "file3.txt.bz2"
然后使用find中的-exec cp -t targetDir {} +
复制文件:
find ( $(eval echo $(sed "s/.*/" "/;s/^/ -name /;s/$/ -o/;$ s/-o//" list.txt)) ) -exec cp -t targetDir {} +
循环浏览文件并将结果附加到目标文件夹:
for i in `cat list.txt`;
do cp `find * -name $i` destination_folder/;
done
这将查找list.txt
中的所有文件,并将这些文件复制到destination_folder/
。
for i in `cat list.txt`
创建一个变量i
,该变量在整个文件中循环。
cp `find * -name $i` destination_folder/
找到文件的路径并将其复制到destination_folder/
。