在bash
下面,我尝试根据具有特定.bam
扩展名的文件在目录内创建子目录。该.bam
文件被修剪,结果是保存在文件夹名称$RDIR
,也可从原来的文件found.There可能有多个上一级.bam
文件,但它们始终是相同的格式。我也发表了评论。谢谢 :)。
重击
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
的结构/home/cmccabe/Desktop/folder
---这是$ 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---
脚本结构之后/home/cmccabe/Desktop/folder
---这是$ 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---
设置-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
sample="$(echo $bam|cut -d_ -f3-)" # remove before second underscore
这可以一行完成:
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.
然后添加for循环来制作目录:
for x in $i
do mkdir -p $DIR/$x
done
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
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句