如何在bash进程中获取选择性文件列表



我的目录/foo/bar/中有几百个文件。我只想grep不以Not开头,但以.vcf结尾的文件列表。

下面的代码选择了所有以.vcf结尾的文件,但我只想在/foo/bar/中的文件(A_test.chr1.vcfB_test.chr2.vcfC_test.chr3.vcf)上循环这个bash代码。

for i in /foo/bar/*.vcf;do
       do something
    done

我在/foo/bar/ 中的文件

    A_test.chr1.vcf
    B_test.chr2.vcf
    C_test.chr3.vcf
    A_test.chr4.other
    Not_other.chr4.other  
    Not1_test.chr1.vcf
    Not2_test.chr2.vcf

任一:

find /foo/bar -type f ( ! -name "Not*" -iname "*.vcf" ) | xargs DoSomething

find /foo/bar -type f ( ! -name "Not*" -iname "*.vcf" ) -exec DoSomething {} ;

语法:

for i in $(find /foo/bar -type f ( ! -name "Not*" -iname "*.vcf" ); DoSomething; done

也可以,但如果你的文件名中有空格,那就很危险了。


编辑

基本上,所有内容都基于find命令。

mkdir -p foo/bar
cd foo/bar/
touch A_test.chr4.vcf  A_test.chr4.other Not_other.chr4.other Not1_test.chr1.vcf
cd ../..
find foo/bar/ -type f ( ! -name "Not*" -iname "*.vcf" )

结果是:

./A_test.chr4.vcf

尝试下一个:find ./foo/bar/ -type f -name *.vcf | while read -r line; do echo $line & done;

最新更新