This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Rename multiple fastq.gz files

Hi everyone,

I have little experience in bash and would like to rename multiple fastq.gz files. I know this question has already been asked multiple times, but nothing I have tried has worked so far, unfortunately.

Original names are as follows:

JM1-44589688_S693_L001_R1_001.fastq.gz
JM110-44589731_S719_L001_R2_001.fastq.gz

I would the final names like this:

JM1_1.fastq.gz
JM110_2.fastq.gz

I tried to use the rename command but I didn't manage to find a way to delete only de central part of the label using this command.

I also tried this:

rename '-' '_' *fastq.gz
for i in *.fastq
   do
   mv $i $(echo $i | awk '{split($1,a,/_/); print a[1]"_"a[5]}')
   done

But this way I lose the suffix .fastq.gz.

Your help will be very much appreciated!

fastq next-gen

Here's another safe solution using brename (download). Personally, I usually avoid batch renaming files with mv, which might overwrite existing files by accident.

$ brename -p '^(\w+).+_R([12])_\d+.fastq.gz' -r '${1}_$2.fastq.gz' -d
Searching for paths to rename...

  [OK] JM1-44589688_S693_L001_R1_001.fastq.gz -> JM1_1.fastq.gz
  [OK] JM110-44589731_S719_L001_R2_001.fastq.gz -> JM110_2.fastq.gz

2 path(s) to be renamed

1 answer

This will be a very rough way of doing it-

#listing the fastq files
ls *fastq.gz
JM1-44589688_S693_L001_R1_001.fastq.gz  JM110-44589731_S719_L001_R2_001.fastq.gz

#changing the names
ls *fastq.gz |awk -F [-_] '{print $1"_"$5".fastq.gz"}' |sed 's/R1/1/g' |sed 's/R2/2/g'
JM1_1.fastq.gz
JM110_2.fastq.gz

Thank you! Just a stupid question, when I run this command it gives me the list of the names expected but does not actually change the names of the files in the directory. Do I have to change anything in the command line to effectively rename the files?

I just gave hint in the comment above. Please try this, it should work for you.

#!/bin/bash
for file in `ls *fastq.gz`; do 
    filename=$(basename "$file")
    new_name=`echo ${filename} |awk -F [-_] '{print $1"_"$5".fastq.gz"}' |sed 's/R1/1/g' |sed 's/R2/2/g'`
    mv ${file} ${new_name};
done

Sorry, I misunderstood. Thank you so much for this.

Of course! If it worked, accept the answer.

Log in to answer this question.