Batch organise files into folders

时间:2018-01-25 16:18:35

标签: unix file-transfer

I am trying to write a loop to organise my files into separate folders on a data storage server.

The idea I had, but am unable to realise follows;

DATA_DIR=/data/user/alingment
OUTDIR=/data/user/reorganised

#reorganise files

numbers=list(range(1,67))
for number in `ls numbers`
do 
    mv $DATA_DIR/NG-.{9}BS[$number]\..*\.bam$ $OUTDIR/sample[$number]
done

To give you an example of the file names, I've included a couple below;

NG-5353_STD.BS54.HWI-ST486_0066_6.Lane_3.read_2_aligned.bam
NG-5353_STD.BS54.HWI-ST486_0066_6.Lane_3.read_2_aligned.bam.bai
NG-5353_STD.BS57.UnknownInstrumentName_11.Lane_5.read_2_aligned.bam
NG-5353_STD.BS57.UnknownInstrumentName_11.Lane_5.read_2_aligned.bam.bai

I just want to .bam files, and I want to organise based on the identifier BS(ID number), hence the list of numbers.

I am very new to this, so any help would be greatly appreciated.

Thank you.

1 个答案:

答案 0 :(得分:0)

最终,我设法搞清楚了。对于任何有类似问题的人,我最终使用了两个循环和一个if语句。见下文:

DATA_DIR=/data/01aligned_files/BAM_Files
OUTDIR=/data/03merge_by_sample/input

#reorganise files
for file in $DATA_DIR/*.bam
do
    for i in {1..66..1}
    do
    REGEX_FILE="NG-.{9}BS"$i"b?\..*"
    echo $REGEX_FILE
        if [[ "$file" =~ $REGEX_FILE ]]; then mv $file $OUTDIR/BS$i; fi
    done
    echo $file
done