【问题标题】:Find files that match lines of a txt and then cp them to a new directory查找与文本行匹配的文件,然后将它们复制到新目录
【发布时间】:2022-10-15 09:37:59
【问题描述】:

我有一个 whitelist.txt。在这个 txt 里面是一个类似这样的文件名列表:

9853412_00000_0_0.vcf
7549682_00000_0_0.vcf
3589647_00000_0_0.vcf

我想在特定的 SOURCE 目录中查找名称与白名单中的文件匹配的文件。然后我想将匹配的文件复制到一个新的目标目录。

例如,如果我的源目录中的文件如下所示:

9853412_00000_0_0.vcf
7549682_00000_0_0.vcf
3589647_00000_0_0.vcf
8965423_00000_0_0.vcf
2547936_00000_0_0.vcf
5479241_00000_0_0.vcf

然后我的脚本应该能够从 SOURCE 中选择前 3 个 vcfs 并将它们复制到 TARGET。

我已经尝试过这些脚本。它们运行时没有错误消息,但没有文件可以从 SOURCE cp 到 TARGET,我不知道为什么。

#!/bin/sh

SOURCE="/my/source/dir/*.vcf"
TARGET="/my/target/dir/"

while IFS= read -r line; do
    find $SOURCE -type f -name "$line" -exec cp {} $TARGET \;  
done < whitelist.txt
#!/bin/sh

SOURCE="/my/source/dir/*.vcf"
TARGET="/my/target/dir/"

while IFS= read -r line; do
    find $SOURCE -type f -name "${line}" -exec cp '{}' $TARGET \;  
done < whitelist.txt

【问题讨论】:

    标签: file unix while-loop find cp


    【解决方案1】:

    快到了,只需删除*.vcf

    #!/bin/bash
    
    SOURCE="/my/source/dir/"
    TARGET="/my/target/dir/"
    
    while IFS= read -r line
    do
        find "$SOURCE" -type f -name "$line" -exec cp {} $TARGET ;
    done < whitelist.txt
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-14
      • 2013-08-22
      • 2016-02-20
      • 2020-12-22
      • 1970-01-01
      • 2020-05-20
      相关资源
      最近更新 更多