bash zsh awk按列表整理文件

背景

我们有一堆文件,在文件夹A中,需要按照文件列表list.txt 复制文件到B中,A中文件和list.txt 中文件均较多

1、文件列表

list.txt类似如下

mnist_test_644.png

mnist_test_2180.png

mnist_test_122.png

mnist_test_2816.png

2、bash

b=`sed ‘r/g’ list.txt`

for i in $b;

do

cp -r “A/”$i “B/”;

done

3、zsh

for i (${(s: :)$(<list.txt)});

do

cp -r “A/”$i “B/”;

done

或者(不推荐,如果txt每行有空格,导致错误,上面兼容更好)

fl=$(<list.txt)

for i (${(f)fl});

do

cp -r “A/”$i “B/”;

done

4、awk 拼接语句管道执行

awk ‘{print “cp -r A/”$1″ B/;” }’ list.txt |sh 

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注