Learn something new everyday. I always used basename but this is better in so many ways.
• 0 views
•
link
I'm writing a shell script that would convert all my .sam files to .bam files. I'm using a loop for all files in the folder that end with .sam. Now my question is how do I write in the script that the output files will have the same name as the input files, but end with .bam?
for file in *.sam; do samtools view -bS $file > ${file/%.sam/.bam}; done
Thank you! That saved so much time :)
strictly speaking, if you want to make sure that there's no other ".sam" string in the filename that could avoid this code to replace the extension, you can always force the string replacement to start from back:
for file in *.sam; do samtools view -bS $file > ${file/%.sam/.bam}; done
rather than simply
for file in *.sam; do samtools view -bS $file > ${file/.sam/.bam}; done
Log in to answer this question.