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
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 thefind
is 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 '19 at 20:08cd "$RDIR"/BAM
fail before it gets tofind
and it fails becauseR_2019*
doesn't match anything (leaving the pattern unexpanded). – Kusalananda♦ Mar 19 '19 at 20:19