# How to loop the script for directories and sub-directories?

**URL:** <https://community.unix.com/t/how-to-loop-the-script-for-directories-and-sub-directories/380372>\
**Category:** Tips and Tutorials\
**Created:** [October 14, 2020, 10:57am UTC](https://community.unix.com/t/how-to-loop-the-script-for-directories-and-sub-directories/380372 "2020-10-14T10:57:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dineshkumarsrk](https://community.unix.com/user_avatar/community.unix.com/dineshkumarsrk/32/10139_2.png) [@dineshkumarsrk](https://community.unix.com/u/dineshkumarsrk)\
**Post date:** [October 14, 2020, 10:57am UTC](https://community.unix.com/t/how-to-loop-the-script-for-directories-and-sub-directories/380372/1 "2020-10-14T10:57:15Z")

</div>

I need to replace fasta file name as fasta header, so I have used the following script

```
awk '/^>/ {gsub(/.fa(sta)?$/,"",FILENAME);printf(">%s\n",FILENAME);next;} {print}' input.fasta

```

This script serves my purpose but, I need to loop it for numerous sub-directories. For example, I have a directory namely `genome` which has multiple sub-directories and those multiple directories contains multiple fasta files as shown below,

```
./genome/enzyme/ocean.fasta, river.fasta
./genome/protein/1.fasta, 2.fasta  

```

I need to implement the above mentioned script for all the fasta files, which lies in genome sub-directories, I tried to do something like this but end up with an error,

```
awk '/^>/ {gsub(/.fa(sta)?$/,"",FILENAME);printf(">%s\n",FILENAME);next;} {print}' */*

```

Please help me to make the script suitable for task.

---

<div class="post-metadata">

**Author:** ![vgersh99](https://community.unix.com/user_avatar/community.unix.com/vgersh99/32/14851_2.png) [@vgersh99](https://community.unix.com/u/vgersh99)\
**Post date:** [October 14, 2020, 12:29pm UTC](https://community.unix.com/t/how-to-loop-the-script-for-directories-and-sub-directories/380372/2 "2020-10-14T12:29:46Z")

</div>

why not using `find` (searching for '\ ***.fasta**' files) in conjunction with `xargs` and your `awk` (which can be streamlined)?  
You'll have to consider some changes to your awk code:

1. derive the **leaf** file name from the PATHNAME containing **PATH** (i.e. `a/b/c/foo.fasta` -\> `foo.fasta`). An example of this derivation was given in one of your previous threads.

2. How do you make changes to a file - not simply printing to stdout? If your version of gawk is 4.1.++ (I think), you can use [Enabling In-Place File Editing](https://www.gnu.org/software/gawk/manual/html_node/Extension-Sample-Inplace.html). Or you can come up with a different editing scheme, e.g. here's a `sed` example: `{ rm FILE; sed -e '...' > FILE; } < FILE`. There might be other alternative options.

Others might have different ideas.  
Give it a whirl.

---

<div class="post-metadata">

**Author:** ![dineshkumarsrk](https://community.unix.com/user_avatar/community.unix.com/dineshkumarsrk/32/10139_2.png) [@dineshkumarsrk](https://community.unix.com/u/dineshkumarsrk)\
**Post date:** [October 14, 2020, 1:32pm UTC](https://community.unix.com/t/how-to-loop-the-script-for-directories-and-sub-directories/380372/3 "2020-10-14T13:32:08Z")

</div>

Thank you for your suggestion. My above script also works nearly ok. But one problem is that, it is adding folder name also along with file name.

---

<div class="post-metadata">

**Author:** ![vgersh99](https://community.unix.com/user_avatar/community.unix.com/vgersh99/32/14851_2.png) [@vgersh99](https://community.unix.com/u/vgersh99)\
**Post date:** [October 14, 2020, 1:40pm UTC](https://community.unix.com/t/how-to-loop-the-script-for-directories-and-sub-directories/380372/4 "2020-10-14T13:40:41Z")

</div>

As mentioned, we already dealt with path/filename derivation.  
Look at your [previous thread](https://community.unix.com/t/how-to-make-reverse-complement-of-a-fasta-sequence-based-on-the-coordinates-given-in-a-csv-file/380175/15).

---

<div class="post-metadata">

**Author:** ![vgersh99](https://community.unix.com/user_avatar/community.unix.com/vgersh99/32/14851_2.png) [@vgersh99](https://community.unix.com/u/vgersh99)\
**Post date:** [October 15, 2020, 2:38pm UTC](https://community.unix.com/t/how-to-loop-the-script-for-directories-and-sub-directories/380372/5 "2020-10-15T14:38:07Z")

</div>

@dineshkumarsrk, just curious - any progress on this post?
