1

In the bash below I am trying to create sub-directories inside a directory from files with specific .bam extentions. The .bam file are trimmed 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. Thank you :).

bash

DIR=/home/cmccabe/Desktop/folder   ## define data directory path
cd "$DIR" || exit 1  # check directory exists or exit
for RDIR in R_2019* ; do  ## start processing matching "R_2019*" to operate on desired directory and expand
 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"/"${x%.*}"  ## make directory of sample id one level up
done  ## close loop

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

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 ---

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

bash: cd: R_2019*/BAM: No such file or directory
++ find . -type f -name '*.bam'
+ bam=
++ echo
++ cut -d_ -f3-
+ sample=
+ mkdir -p ''
mkdir: cannot create directory ‘’: No such file or directory
4
  • 2
    What is the issue? Does the code work the way you want? If not, in what way? Mar 19, 2019 at 19:50
  • bam=$(find . -type f -name "*.bam") # extract .bam is not extracted.... bash: cd: R_2019*/BAM: No such file or directory... mkdir: cannot create directory ‘’: No such file or directory.... I think the findis not working as expected, but not sure how to fix it. I added `set -x to the post as well. Thank you :).
    – justaguy
    Mar 19, 2019 at 20:08
  • 1
    Well, your cd "$RDIR"/BAM fail before it gets to find and it fails because R_2019* doesn't match anything (leaving the pattern unexpanded).
    – Kusalananda
    Mar 19, 2019 at 20:19
  • 1
    Also related: Why is looping over find's output bad practice?
    – Kusalananda
    Mar 19, 2019 at 20:40

1 Answer 1

1

Seems to be issue with following line:

 bam=$(find . -type f -name "*.bam")   # extract .bam
 sample="$(echo $bam|cut -d_ -f3-)" # remove before second underscore

Modifications:

This can be achieved in one line:

i=$(find . -type f -name "*.bam" -print | while read f;do echo "$f" | cut -d_ -f3-;done| cut -f 1 -d '.') ## To take the file names and then cut.

and then add for loop to make dir:

for x in $i
        do mkdir -p $DIR/$x
        done

Final Script:

DIR=/home/vvek/MyLearning/Linux/bam/   ## define data directory path
cd "$DIR" || exit 1  # check directory exists or exit
for RDIR in 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 $DIR/$x
        done
done  ## close loop
2
  • Thank you very much for your help and explanatiom.m :)
    – justaguy
    Mar 20, 2019 at 21:54
  • Happy to be helpful.. keep learning :p Mar 21, 2019 at 2:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .