Home > Back-end >  How to save outputs from current directory to another a directory in a shell script?
How to save outputs from current directory to another a directory in a shell script?

Time:03-24

I want to read all files in my current directory and would like to save the result on another directory. The code is below but could not get the files in the excepted directory. Does anyone can help me.

for file in *.sorted.bam
do 
samtools index "$file" -o "${file%.sort.bam}".bam | mv /mnt/d/Document/bt2Alignment_result
done

Thank you!

CodePudding user response:

Here's a fixed version of your code:

for file in *.sorted.bam
do
    outfile="${file%.sorted.bam}.bam"
    samtools index "$filepath" -o "$outfile"
    mv "$outfile" /mnt/d/Document/bt2Alignment_result/
done

remark: you were using the glob *.sorted.bam while trying to strip the suffix .sort.bam


That I said, I don't think that you even need to move the output file as samtools seems to have an option for specifying it directly. Also, sometimes it's convenient to specify a path in the glob (for example, ./datadir/*.sorted.bam), you should take that into account and do:

outdir="/mnt/d/Document/bt2Alignment_result"

for filepath in ./*.sorted.bam
do
    filename=${filepath##*/}
    samtools index "$filepath" -o "$outdir/${filename%.sorted.bam}.bam"
done

CodePudding user response:

Use mv in the next line with *.sorted.bam

for file in *.sorted.bam
do 
samtools index "$file" -o "${file%.sorted.bam}".bam 
mv *.sorted.bam /mnt/d/Document/bt2Alignment_result/
done

  • Related