
以下では、bash
特定の拡張子を持つファイルのディレクトリ内にサブディレクトリを作成しようとしています.bam
。ファイルが.bam
切り捨てられ、結果は$RDIR
元のファイルの場所または1レベル上のフォルダ名に保存されます。複数のファイルが存在する可能性がありますが、.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
答え1
次の行に問題があるようです。
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