How to edit the headers of multiple fasta files from multiple folders? (recursively) This is an expansion of my previous question, 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 project directories. Inside each project directory, there are multiple metagenome directories. Inside each metagenome directory, there is always a single .fna (fasta) file.

Names: project directories start with 'mgp', metagenome directories start with 'mgm' and end with '.3'. They end in numbers, but the numbers are not related. (eg. project directory 'mgp83581' contains metagenome directory 'mgm4729322.3').

What I want is a loop that would go through all these fasta files, and add to the headers codes corresponding to project and metagenome directory. For example, if the fasta file in project directory mgp83581, metagenome subdirectory mgm4729322.3 had the following headers...

&gt;seq1

&gt;seq2

...then I would want to change it to:

&gt;83581_322_seq1

&gt;83581_322_seq2

Basically, I want to add the number code associated with the project, and the last three digits of the 7-digit code associated with the metagenome (apart from the '.3' part of the file name).

Below is the answer that I got for my previous question, where I didn't have project directories, and where the 3-digit code for the metagenomes stayed in a manageable range. (thanks terdon!)

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

Using ubuntu. I would appreciate your help!
You can edit your for loop so as to include the paths to your files of interest.

The command below will give you the relative paths (relative to where you run the command, I think it is better to run this within your main directory) of your files of interest:

ls mpg*/mgm*.3/*.fna


You can use the output of this command (your files of interest) to iterate over via your for loop. Your code will look like:

for i in {...}; do 
    sed -i.bak "s/^&gt;/&gt;${i}_/" i
done