diff --git a/samtools.wdl b/samtools.wdl
index ccd99cbeace3b3ce5c1641cf14a7e7f87f81a312..b574ec5f023c13adb366e301e99bd7702d686fd0 100644
--- a/samtools.wdl
+++ b/samtools.wdl
@@ -54,6 +54,7 @@ task Flagstat {
     command {
         set -e -o pipefail
         ${preCommand}
+        mkdir -p $(dirname ${outputPath})
         samtools flagstat ${inputBam} > ${outputPath}
     }