This is a test version of Biostars. For the public version, visit https://www.biostars.org.
move fasta and fastq files with the same name folder

I have multiples files

genome_1.fasta 

genome_1_R1.fastq 

genome_1_R2.fasta 

genome_2.fasta 

genome_2_R1.fastq 

genome_2_R2.fasta

genome_2.fasta 

genome_2_R1.fastq 

genome_2_R2.fasta 

I want to move the files with the same base name to a folder that correspond to each genome name, I mean all genome_1* (fasta and fastq) files will be moved to genome_1/ folder

if I make it for fasta first, and then for fastq, I have no problems

for f in *.fasta; do dir="${f%.fasta}"; mkdir $dir; mv "$f" "$dir"; done

it will create the folder and move all fasta to it now all fastq

for f in *.fastq; do dir="${f%_R*.fastq}";  mv "$f" "$dir"; done

how can I use a single loop to move .fasta, _R1.fastq and _R2.fastq ???

it possible to add multiples patterns for dir variable ? something like:

dir="${f%[.fasta\|_R1.fastq\|_R2.fastq]}"

Thanks

for loop

1 answer

You can add multiple extension in your for loop.

for f in *.fasta *.fastq; do
    base_name=$(basename "$f" | cut -d. -f1)

    genome_name=$(echo "$base_name" | cut -d_ -f1)

    mkdir -p "$genome_name"

    mv "$f" "$genome_name/"
done

Log in to answer this question.