I am trying to generate separate .md5 files for each .fastq file in a directory. I found solutions to generate a single .md5 file for many files, but this is not what I am looking for.

I have file1.fastq, file2.fastq, file3.fastq and want to generate file1.md5, file2.md5, file3.md5.

I assume that a FOR loop would do the trick but I am not a programmer and can't seem to find a solution to this problem.

I also tried the following code:

find . -type f -name "*.fastq.gz" -exec sh -c "md5sum < {} > {}.md5" \;

It correctly generates an .md5 file for each .fastq file, but the .md5 file contents are incorrect, i.e. I get 64399513b7d734ca90181b27a62134dc -

instead of 64399513b7d734ca90181b27a62134dc testfile.fastq

Can anyone help?


2 Answers 2


The simplest case is:

for file in *fastq; do md5sum "$file" > "$file".md5; done

That, however, will create file names like file1.fastq.md5. Extensions are basically irrelevant here, so that isn't a problem, but if you prefer file1.md5 do this instead:

for file in *fastq; do md5sum "$file" > "${file//.fastq}".md5; done
  • The first option is exactly what I needed. Thanks terdon!
    – Chris_bio
    Jan 29, 2016 at 11:08

Also your md5sum command doesn't print the filename because each file is read from the shell due to the < redirection, and md5sum can't possibly know the original filename, since it's feeded the file's content directly.

So if you want to process all the files in the current working directory recursively you can use this command instead:

find . -type f -name "*.fastq.gz" -exec sh -c "md5sum {} > {}.md5" \;

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .