如何通过将文件名匹配到textfile中的name的子字符串来重命名文件

时间:2017-10-30 00:17:18

标签: linux bash unix text rename

我有一个包含300个文件的目录,如下所示:

S221_L001_R2_001.fastq.gz   S30_L001_R2_001.fastq.gz    
S95_L001_R2_001.fastq.gz    S159_L001_R2_001.fastq.gz   
S222_L001_R2_001.fastq.gz   S31_L001_R2_001.fastq.gz    
S96_L001_R2_001.fastq.gz

我有一个看起来像这样的文本文件:

G1F0Blank3_S184_L001_R1_001.fastq.gz
G1F0Blank3_S184_L001_R2_001.fastq.gz
G1F0C1A_S101_L001_R1_001.fastq.gz
G1F0C1A_S101_L001_R2_001.fastq.gz
G1F0C1B_S154_L001_R1_001.fastq.gz
G1F0C1B_S154_L001_R2_001.fastq.gz

我想将文本文件中文件名前面的字符串添加到我目录中的匹配文件中。文件文件中有一个条目用于我的目录中的每个文件。我最希望能够在我的unix终端中执行此操作。

谢谢!

2 个答案:

答案 0 :(得分:0)

   # first, list all files:
   ls *.gz | while read FILENAME
   do
       # then, get the new filename from the file
       # replace filenames.txt with your TXT file
       NEW_FILENAME=$( grep "$FILENAME" /path/to/filenames.txt )
       # and finally, rename everything
       [ "$NEW_FILENAME" != "" ] && mv "$FILENAME" "$NEW_FILENAME" -v
   done

如有疑问,请在

之前备份所有内容

答案 1 :(得分:0)

numpy.array