Hi I am trying to convert bam files generated from Ion Torrent Proton sequencing to fastq format so that I can upload them to KBase for analysis.

The files are named as follows: 01_thu159_IX_RNA_001.bam 01_thu163_IX_RNA_001.bam 01_thu164_IX_RNA_001.bam 01_thu190_IX_RNA_001.bam

So I tried:

samtools fastq *.bam > *.fastq

However, that did not work. But, I know it is possible to use a placeholder for the name in code by using something like $f or I have also seen %N%. I am just not sure how to basically use those placeholders to be able to quickly apply a for loop, which will convert the files to fastq format without having to do each one individually. I have 28 samples each containing 4 bam files. Any advice on how I can do this would be greatly appreciated.

  • $\begingroup$ I am not familiar with samtools however your example, which I believe is run through the terminal, is printing all the output into a single file and not into many, as you may think. I guess that by using > *.fastq you meant to create many fastq files, which is not what you're doing. also I found this link that may help you onestopdataanalysis.com/bam-to-fastq and here it looks like they process one file at a time. if this is the case you just need a for loop to run the command for each file $\endgroup$
    – gabt
    Aug 18, 2021 at 10:23
  • $\begingroup$ Hi there, yes I was running that command in the terminal. So the idea basically was I don't want to have to type the name of the individual files in each case. For example I would like to know how to write the for loop so that it converts the bam file to fastq format and maintains the same file name just the format is changed. Otherwise I will have to type out each name individually as such: samtools fastq 01_thu159_IX_RNA_001.bam > 01_thu159_IX_RNA_001.fastq and this is tedious considering how many files I have. $\endgroup$
    – Justin1609
    Aug 18, 2021 at 10:32
  • $\begingroup$ ok, so your question actually was about how to run a command on multiple files at once, am I right? If yes, then here you can find some answers to that specific question: unix.stackexchange.com/questions/80851/… unix.stackexchange.com/questions/80851/… $\endgroup$
    – gabt
    Aug 18, 2021 at 10:39
  • $\begingroup$ Yes, that is correct. Thank you, I truly appreciate the assistance. $\endgroup$
    – Justin1609
    Aug 18, 2021 at 10:55
  • $\begingroup$ of course, if you are not able to write the necessary code, you can still ask a question regarding that specific point. However, I believe there should be enough information in the two answers I linked to help you. $\endgroup$
    – gabt
    Aug 18, 2021 at 10:59

1 Answer 1


Using GNU parallel:

ls *.bam | parallel -j 2 "samtools fastq {} > {.}.fastq"

Use -j to control the number of parallel jobs. That only works for single-end data though as paired-end data must first be sorted or collated by name.

  • $\begingroup$ Thank you very much, I really appreciate it. My data is single-end reads, as it was generated using Ion Torrent, so this should work well. $\endgroup$
    – Justin1609
    Aug 18, 2021 at 13:24
  • $\begingroup$ @Justin1609 Yes, I guess that should work with the above command without problems. $\endgroup$
    – user3051
    Aug 18, 2021 at 13:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.