How to edit the headers of multiple fasta files from multiple folders? My directories are organized as follows: one main directory, in which I have multiple directories that end with a number ranging from 314 to 727, followed by .3   . For example, 'mgm4761314.3'. Within these directories, I have fasta files named after a similar pattern (for example, mgm4761314.3.299.screen.passed.fna is in the directory mgm4761314.3).

There is only one such file (.fna) per directory.

What I've been trying to do is to write a loop that would go through all these folders and fasta files, and add the short number associated with the folder/file name (eg. 314, 315...727) to the headers. For example, for my first folder/fasta file, instead of:

&gt;seq1
&gt;seq2


I want:

&gt;314_seq1
&gt;314_seq2


The output would be exported to a file called, for instance, 314.fna.

I've sort of made it work for one number at a time, but I have plenty of files, so I'd appreciate if anyone could help me with the loop part! This is what I tried (and it didn't work). Using ubuntu.

for i in {314..727}; do sed "s/&gt;/&gt;${i}_/" [...]/*${i}.3/mgm4761${i}.3.299.screen.passed.fna &gt; [...]/*${i}.3/${i}.fna; done
You were so close! This should do the trick:

for i in {314..727}; do 
    sed -i.bak "s/^&gt;/&gt;${i}_/" mgm4761${i}.3/*.fna
done


Note that I am using -i which makes sed edit the original file. Because I use -i.bak, this will create a backup file with the original data. Once you are sure the command does what you need, you can delete all of these files with rm */*fna.bak.