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

Hello,

I have several fastq files like this:

7097-TK273759-HiSeqShotgun_S9_R1_001.fastq.gz
7097-TK273759-HiSeqShotgun_S9_R2_001.fastq.gz
7097-TK273763-HiSeqShotgun_S10_R1_001.fastq.gz
7097-TK273763-HiSeqShotgun_S10_R2_001.fastq.gz
7097-TK273775_1-HiSeqShotgun_S14_R1_001.fastq.gz
7097-TK273775_1-HiSeqShotgun_S14_R2_001.fastq.gz
7097-TK273775_2-HiSeqShotgun_S15_R1_001.fastq.gz
7097-TK273775_2-HiSeqShotgun_S15_R2_001.fastq.gz

I want to rename like this:

TK273759_R1.fastq.gz
TK273759_R2.fastq.gz
TK273763_R1.fastq.gz
TK273763_R2.fastq.gz
TK273775_1_R1.fastq.gz
TK273775_1_R2.fastq.gz
TK273775_2_R1.fastq.gz
TK273775_2_R2.fastq.gz
fastq

3 answers

use brename

brename -p ".+(TK[0-9]+_?[12]?)-HiSeq.+(_R[12])_.+fastq.gz" -r '$1$2.fastq.gz' -d

try below code in terminal:

for file in *.fastq.gz; do
    if [[ $file =~ 7097-([^-]+)-HiSeqShotgun_(S[0-9]+)_R([0-9]+)_001.fastq.gz ]]; then
        prefix="${BASH_REMATCH[1]}_R${BASH_REMATCH[3]}"
        suffix=".fastq.gz"
        mv "$file" "${prefix}${suffix}"
    fi
done

enter image description here

The code you provided worked! I really apprecaite your help.

A small educational note: if an answer was helpful, you should upvote it; if the answer resolved your question, you should mark it as accepted. You can accept more than one answer if they all work. If an answer was not really helpful or did not work, provide detailed feedback so others know not to use that answer.

upvote_bookmark_accept

Renaming multiple files at once is something that comes up quite often. That's why it's better to be acquainted with tools that have such feature. Most of these renaming tasks are one of a kind so writing scripts and regexes might become cumbersome compare to creating a vim macro.

Terminal based file managers like nnn or ranger have a bulk renaming feature where you can edit the list of file names in your preferred editor like vim.

Also in emacs dired mode you can make your buffer writable and rename your files all at once.

Log in to answer this question.