I am given a file containing the md5 values for files within the same folder. The information is in the file md5checksums.txt in the following format:

b0da7ead9d82a3494d7e0a7099871ef4  ./GCF_000959505.1_ASM95950v1_assembly_report.txt
7ff32cbb16daf46c87b3546ad576ff66  ./GCF_000959505.1_ASM95950v1_assembly_stats.txt
034081da3aa0708f06c2ec1129e4aca9  ./GCF_000959505.1_ASM95950v1_cds_from_genomic.fna.gz

I want to do md5 checks for all the files. I got this command:

awk '{system("md5 "$2)}' md5checksums.txt

But this just gets the md5 values

MD5 (./GCF_000959505.1_ASM95950v1_assembly_report.txt) = b0da7ead9d82a3494d7e0a7099871ef4
MD5 (./GCF_000959505.1_ASM95950v1_assembly_stats.txt) = 7ff32cbb16daf46c87b3546ad576ff66
MD5 (./GCF_000959505.1_ASM95950v1_cds_from_genomic.fna.gz) = 3a30966523a36368ab432f666001f80a

I would like to extract the calculated md5 against the first column of md5checksums.txt I thought I could do something like an awk inside an awk, but I can't get it to work:

awk '{system("md5 "$2" | awk HERE EVALUATE RESULTS AND CHECK IF EQUAL TO $1")}' md5checksums.txt
  • 3
    BSD md5 vs GNU md5sum output. Give md5 the -r option to produce the same output format as md5sum. – Mark Plotnick Dec 19 '18 at 16:52
  • Great, but I still have to parse the results since they are given in 2 columns – Julio Diaz Dec 19 '18 at 16:56
  • do you want to check file againt their md5 sum ? Yes=> md5sum -r Or see if same file comme with new name ? – Archemar Dec 19 '18 at 17:14
  • I suspect that this an escaping problem, you need to escape the $ to stop it being interpreted by the outer awk. I am sure there are better answers though. – ctrl-alt-delor Dec 19 '18 at 17:40

I'm a bit confused as to why you involve awk in this.

To verify the MD5 checksums in a file produced by GNU md5sum, you do

md5sum -c file.txt

Or, on an OpenBSD or NetBSD system whose md5 utility supports -c filename (not FreeBSD or macOS):

md5 -c file.txt

In your case, file.txt would be your md5checksums.txt file.

  • How would I check the resulting value to the one given in a file along the md5s of other files? I should have made it clear that I would like to check the md5 values for all the files in the folder. – Julio Diaz Dec 19 '18 at 16:50
  • @JulioDiaz I don't thing I understand that question. In my example, file.txt is the file that contains the MD5 checksums and pathnames. md5sum -c file.txt will calculate the MD5 checksums of the files listed and compare them to the corresponding pre-calculated checksums. You probably just need to use md5checksums.txt instead of file.txt. – Kusalananda Dec 19 '18 at 16:55
  • I see. Is there a homologue to -c in md5? – Julio Diaz Dec 19 '18 at 16:59
  • @JulioDiaz You haven't yet said what type of Unix you are working with. – Kusalananda Dec 19 '18 at 17:07
  • Im working from OSX – Julio Diaz Dec 19 '18 at 17:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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