如何按文件夹更改文件名?

时间:2013-12-10 18:16:31

标签: shell terminal

我有一个包含大约1,000个文件的文件夹,如下所示:

$ awk '{print $9}' folder
1003ns_cells_found.fcs
1003pi_cells_found.fcs
1007ns_cells_found.fcs
1007pi_cells_found.fcs
1029ns_cells_found.fcs
1029pi_cells_found.fcs
1041nsA_cells_found.fcs

我还有另一个ID文件:

$ head -5 ID
1003 BD2188
1003 BD2188
1007 BD2116
1007 BD2116
1029 BD2012

我会用ID文件的第二列替换文件夹中所有文件的ID号。有谁知道如何通过Terminal / Shell实现它?

1 个答案:

答案 0 :(得分:2)

这个简单的脚本应该这样做:

#!/bin/bash

ls ./folder | while read line; do
cat ID | awk -v x=${line:0:4} 'x==$1 {printf "%s", $2; exit}'
echo -e "${line:4}";
done

文件夹结构如下所示:

$ find
.
./ID <-- file with the IDs
./folder
./folder/1003pi_cells_found.fcs
./folder/1041nsA_cells_found.fcs
./folder/1007pi_cells_found.fcs
./folder/1007ns_cells_found.fcs
./folder/1029pi_cells_found.fcs
./folder/1003ns_cells_found.fcs
./folder/1029ns_cells_found.fcs
./script <-- script to execute

输出:

BD2188ns_cells_found.fcs
BD2188pi_cells_found.fcs
BD2116ns_cells_found.fcs
BD2116pi_cells_found.fcs
BD2012ns_cells_found.fcs
BD2012pi_cells_found.fcs
nsA_cells_found.fcs

最后一个为空,因为在文件1041中找不到ID