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?
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!
• 2,542 views
•
link
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
• 0 views
•
link
• 0 views
•
link
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
• 0 views
•
link
Log in to answer this question.
Here's another safe solution using brename (download). Personally, I usually avoid batch renaming files with
mv, which might overwrite existing files by accident.