Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to save outputs from current directory to another a directory in a shell script?

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!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading