# Bash to create sub directories from specific file extension

**URL:** <https://community.unix.com/t/bash-to-create-sub-directories-from-specific-file-extension/375177>\
**Category:** Shell Programming and Scripting\
**Created:** [March 20, 2019, 8:43am UTC](https://community.unix.com/t/bash-to-create-sub-directories-from-specific-file-extension/375177 "2019-03-20T08:43:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmccabe](https://community.unix.com/letter_avatar/cmccabe/32/6_449c533f586939b6e5f246632a46421f.png) [@cmccabe](https://community.unix.com/u/cmccabe)\
**Post date:** [March 20, 2019, 8:43am UTC](https://community.unix.com/t/bash-to-create-sub-directories-from-specific-file-extension/375177/1 "2019-03-20T08:43:06Z")

</div>

In the `bash` below I am trying to create sub-directories inside a directory from files with specific `.bam` extensions. There may be more then one `$RDIR` ing the directory and the `.bam` file(s) are trimmed (removing the extension and IonCode\_0000\_) and the result is the folder name that is saved in `$RDIR` or one level up from where the original files are found. There may be multiple `.bam` files but they are always the same format. I made comments as well and ran `set -x` and think the `find` is not working as expected but am not able to fix it . Thank you :).

structure of `/home/cmccabe/Desktop/folder` --- this is `$DIR` ---

```nohighlight
R_2019_00_00_00_00_00_xxxx_xx-0000-00 --- this is $RDIR ---
       BAM ---subdirectory---
       IonCode_0241_19-0000-Last-First.bam.bai
       IonCode_0241_19-0000-Last-First.bam
       IonCode_0243_19-0001-Las-Fir.bam.bai
       IonCode_0243_19-0001-Las-Fir.bam
     QC ---subdirectory---

```

after script structure of `/home/cmccabe/Desktop/folder` --- `this is $DIR` ---

```nohighlight
R_2019_00_00_00_00_00_xxxx_xx-0000-00 --- this is $RDIR ---
     BAM ---subdirectory---
     19-0000-Last-First ---subdirectory---
     19-0001-Las-Fir ---subdirectory---
     QC ---subdirectory---

```

set -x

```nohighlight
set -x
cmccabe@DTV-A5211QLM:~$ DIR=/home/cmccabe/Desktop/sub ## define data directory path
+ DIR=/home/cmccabe/Desktop/sub
cmccabe@DTV-A5211QLM:~$ for RDIR in "$DIR"/R_2019* ; do ## start processing matching "R_2019*" to operate on desired directory and expand
> echo "$RDIR"
> cd "$RDIR"/BAM ## change directory to subfolder inside $RDIR
> bam=$(find . -type f -name "*.bam") # extract .bam
> sample="$(echo $bam|cut -d_ -f3-)" # remove before second underscore
> mkdir -p "${sample%.*}" && mv "$sample" "RDIR"/"${sample%.*}" ## make directory of sample id one level up
> cd ..
> done ## close loop
+ for RDIR in '"$DIR"/R_2019*'
+ echo /home/cmccabe/Desktop/sub/R_2019_01_23_13_22_58_user_S5-0271-93
/home/cmccabe/Desktop/sub/R_2019_01_23_13_22_58_user_S5-0271-93
+ cd /home/cmccabe/Desktop/sub/R_2019_01_23_13_22_58_user_S5-0271-93/BAM
++ find . -type f -name '*.bam'
+ bam='./IonCode_0251_19-0003-La-Fi.bam
./IonCode_0243_19-0001-Las-Fir.bam
./IonCode_0255_190319-Control.bam
./IonCode_0241_19-0000-Last-First.bam'
++ echo ./IonCode_0251_19-0003-La-Fi.bam ./IonCode_0243_19-0001-Las-Fir.bam ./IonCode_0255_190319-Control.bam ./IonCode_0241_19-0000-Last-First.bam
++ cut -d_ -f3-
+ sample='19-0003-La-Fi.bam ./IonCode_0243_19-0001-Las-Fir.bam ./IonCode_0255_190319-Control.bam ./IonCode_0241_19-0000-Last-First.bam'
+ mkdir -p '19-0003-La-Fi.bam ./IonCode_0243_19-0001-Las-Fir.bam ./IonCode_0255_190319-Control.bam ./IonCode_0241_19-0000-Last-First'
+ mv '19-0003-La-Fi.bam ./IonCode_0243_19-0001-Las-Fir.bam ./IonCode_0255_190319-Control.bam ./IonCode_0241_19-0000-Last-First.bam' 'RDIR/19-0003-La-Fi.bam ./IonCode_0243_19-0001-Las-Fir.bam ./IonCode_0255_190319-Control.bam ./IonCode_0241_19-0000-Last-First'
mv: cannot stat �19-0003-La-Fi.bam ./IonCode_0243_19-0001-Las-Fir.bam ./IonCode_0255_190319-Control.bam ./IonCode_0241_19-0000-Last-First.bam': No such file or directory
+ cd ..
+ for RDIR in '"$DIR"/R_2019*'
+ echo /home/cmccabe/Desktop/sub/R_2019_01_23_13_22_58_user_S5-0271-96
/home/cmccabe/Desktop/sub/R_2019_01_23_13_22_58_user_S5-0271-96
+ cd /home/cmccabe/Desktop/sub/R_2019_01_23_13_22_58_user_S5-0271-96/BAM
++ find . -type f -name '*.bam'
+ bam='./IonCode_0251_19-0003-La-Fi.bam
./IonCode_0243_19-0001-Las-Fir.bam
./IonCode_0255_190319-Control.bam
./IonCode_0241_19-0000-Last-First.bam'
++ echo ./IonCode_0251_19-0003-La-Fi.bam ./IonCode_0243_19-0001-Las-Fir.bam ./IonCode_0255_190319-Control.bam ./IonCode_0241_19-0000-Last-First.bam
++ cut -d_ -f3-
+ sample='19-0003-La-Fi.bam ./IonCode_0243_19-0001-Las-Fir.bam ./IonCode_0255_190319-Control.bam ./IonCode_0241_19-0000-Last-First.bam'
+ mkdir -p '19-0003-La-Fi.bam ./IonCode_0243_19-0001-Las-Fir.bam ./IonCode_0255_190319-Control.bam ./IonCode_0241_19-0000-Last-First'
+ mv '19-0003-La-Fi.bam ./IonCode_0243_19-0001-Las-Fir.bam ./IonCode_0255_190319-Control.bam ./IonCode_0241_19-0000-Last-First.bam' 'RDIR/19-0003-La-Fi.bam ./IonCode_0243_19-0001-Las-Fir.bam ./IonCode_0255_190319-Control.bam ./IonCode_0241_19-0000-Last-First'
mv: cannot stat �19-0003-La-Fi.bam ./IonCode_0243_19-0001-Las-Fir.bam ./IonCode_0255_190319-Control.bam ./IonCode_0241_19-0000-Last-First.bam': No such file or directory
+ cd ..

```

---

<div class="post-metadata">

**Author:** ![cmccabe](https://community.unix.com/letter_avatar/cmccabe/32/6_449c533f586939b6e5f246632a46421f.png) [@cmccabe](https://community.unix.com/u/cmccabe)\
**Post date:** [March 20, 2019, 10:47am UTC](https://community.unix.com/t/bash-to-create-sub-directories-from-specific-file-extension/375177/2 "2019-03-20T10:47:47Z")

</div>

I modified the `find` and added a `loop` to `mkdir` and got the expected results. Just wanted to share in case it helps anyone else in the future. Thank you :).

```nohighlight
DIR=/home/cmccabe/Desktop/sub ## define data directory path
for RDIR in "$DIR"/R_2019* ; do ## start processing matching "R_2019*" to operate on desired directory and expand
 cd "$RDIR"/BAM ## change directory to subfolder inside $RDIR
i=$(find . -type f -name "*.bam" -print | while read f;do echo "$f" | cut -d_ -f2-;done| cut -f 1 -d '.') # extract .bam
for x in $i
        do mkdir -p $RDIR/$x
        done
done ## close loop

```

---

<div class="post-metadata">

**Author:** ![nezabudka](https://community.unix.com/letter_avatar/nezabudka/32/6_449c533f586939b6e5f246632a46421f.png) [@nezabudka](https://community.unix.com/u/nezabudka)\
**Post date:** [March 20, 2019, 11:44am UTC](https://community.unix.com/t/bash-to-create-sub-directories-from-specific-file-extension/375177/3 "2019-03-20T11:44:06Z")

</div>

```nohighlight
$(find * -type f -name "*.bam" | cut -d_ -f2 | cut -d. -f1)

```

--- Post updated at 18:32 ---

endless running through directories means only one thing, you don't know where you will when will creating catalogs

```nohighlight
find "$RDIR"/BAM -type f -name "*.bam" | cut -d_ -f2 | cut -d. -f1 | xargs -I {} mkdir -p "$RDIR/"{}

```

--- Post updated at 18:44 ---

In any case, it is better not to run on items twice.

```nohighlight
do
    cd "$RDIR"/BAM
    find * -type f -name "*.bam" | cut -d_ -f2 | cut -d. -f1 | xargs -I {} mkdir -p "$RDIR/"{}
done

```
