bash - 使用命令查找和复制,但使用带有名称的 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
................................
解决方案
您可以使用echo和sed:
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 {} +
推荐阅读
- c# - 这种嵌套的泛型方法怎么解决呢?
- javascript - 如何在递归中转换函数
- python - 为课堂外的scrapy设置开始网址
- angular - Angular Reactive Form - 从 HTML 手动更新 FormGroup
- excel - 如何使用 office-js 在任务窗格 [加载项] 中列出所有工作簿工作表?
- kibana - 从开发环境打开kibana主页失败
- swift - Swift:“'Any'类型的值没有下标”错误/ swift 5
- javascript - Webpack 构建不适用于爬虫
- laravel - 无法通过 Guzzle 请求将数据发布到 Laravel 5.4
- mongodb - 当 lambda 容器过期时,是否有关闭 mongo 连接的好方法?