How to rename multiple fastq files
3
0
Entering edit mode
11 months ago

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 • 1.2k views
ADD COMMENT
3
Entering edit mode
11 months ago
Ming Tommy Tang ★ 4.5k

use brename

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

Fore reference brename is available here: https://github.com/shenwei356/brename

ADD REPLY
0
Entering edit mode

Thank you for your help!

ADD REPLY
2
Entering edit mode
11 months ago
Sajad ▴ 90

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

ADD COMMENT
0
Entering edit mode

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

ADD REPLY
2
Entering edit mode

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

ADD REPLY
0
Entering edit mode
11 months ago
barslmn ★ 2.3k

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.

ADD COMMENT

Login before adding your answer.

Traffic: 1962 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6