Only get hash value using md5sum (without filename)

158,730

Solution 1

Using AWK:

md5=`md5sum ${my_iso_file} | awk '{ print $1 }'`

Solution 2

A simple array assignment works... Note that the first element of a Bash array can be addressed by just the name without the [0] index, i.e., $md5 contains only the 32 characters of md5sum.

md5=($(md5sum file))
echo $md5
# 53c8fdfcbb60cf8e1a1ee90601cc8fe2

Solution 3

You can use cut to split the line on spaces and return only the first such field:

md5=$(md5sum "$my_iso_file" | cut -d ' ' -f 1)

Solution 4

On Mac OS X:

md5 -q file

Solution 5

md5="$(md5sum "${my_iso_file}")"
md5="${md5%% *}" # remove the first space and everything after it
echo "${md5}"
Share:
158,730

Related videos on Youtube

John Doe
Author by

John Doe

Updated on July 29, 2021

Comments

  • John Doe
    John Doe almost 3 years

    I use md5sum to generate a hash value for a file. But I only need to receive the hash value, not the file name.

    md5=`md5sum ${my_iso_file}`
    echo ${md5}
    

    Output:

    3abb17b66815bc7946cefe727737d295  ./iso/somefile.iso
    

    How can I 'strip' the file name and only retain the value?

    • Sridhar Sarnobat
      Sridhar Sarnobat over 7 years
      Very surprising this isn't an option for md5sum.
    • TommyAutoMagically
      TommyAutoMagically almost 7 years
      Agreed! Why isn't this an option? Can a GNU-Master shed some light?
    • Magnetic_dud
      Magnetic_dud about 4 years
      Why do you need to have a "only hash" flag when you can trim the result with some regex? (I am sarcastic)
  • Roman Cheplyaka
    Roman Cheplyaka almost 14 years
    Nice. One note -- on the first line you don't need quotes around $() (although they do no harm) but certainly need them around ${}.
  • Gordon Davisson
    Gordon Davisson almost 14 years
    @Roman: yeah, I tend to habitually quote any expansion (unless there's a reason not to) -- it's easier than keeping track of the cases where it's safe to skip the quotes. (Although in this case, I left them off the actual filename... stand by for an edit.)
  • jyz
    jyz over 9 years
    Awesome. Just one question, I know the question is tagged bash, but can you tell me if array is a bash only feature or some shell standard?
  • Christophe De Troyer
    Christophe De Troyer over 9 years
    Or md5=`md5sum < ${my_iso_file}` However this still outputs " - " at the end. But for comparisons this should be enough.
  • Andy
    Andy almost 9 years
    the first line doesn't work inside the do section of a for loop...as a Bash newb I don't yet know why
  • Peter.O
    Peter.O almost 9 years
    @Andy: If you try this line of code (in the terminal, or in a script): echo>file; for i in file; do md5=($(md5sum file)); echo $md5; done - It should output 68b329da9893e34099c7d8ad5cb9c940
  • Andy
    Andy almost 9 years
    @Peter.O that does, I figured out that the problem was I had #!/bin/sh at the top of my script instead of #!/bin/bash. Thanks!
  • lkraav
    lkraav almost 9 years
    How come echo ($(echo -n foo | md5sum)) doesn't work? Errors out bash: syntax error near unexpected token $(echo -n foo | md5sum)'
  • Peter.O
    Peter.O almost 9 years
    @lkraav: The command echo -n foo | md5sum outputs 2 shell words to stdout: acbd18db4cc2f85cedef654fccc4a4d8 and - (the - indicates the source as stdin). – You must tell bash to capture those words into a string, using Command Substitution: $( command ). – The ( brackets ) produce a bash array with 2 elements. However, you must assign that array construct ( … ) to an variable name; hence, using md5 as the array name: md5=($(echo -n foo | md5sum)). You haven't assigned the array to a variable name
  • Shane
    Shane over 8 years
    This is a bash question, but note that this doesn't work in zsh. Instead you can echo $md5[1] to get only the hash (but this isn't portable to bash)...
  • Sridhar Sarnobat
    Sridhar Sarnobat over 7 years
    doesn't work on my Mac OS X 10.7. But thanks for posting, for whatever version this works on.
  • alper
    alper almost 6 years
    Wrong it gives following output on Mac MD5 (/Users/hello.txt) = 24811012be8faa36c8f487bbaaadeb71 and your code returns MD5.
  • alper
    alper almost 6 years
    You can get run of - by adding | awk '{print $1}' end of your code => md5sum < ${my_iso_file} | awk '{print $1}' @ChristopheDeTroyer
  • David Tabernero M.
    David Tabernero M. almost 6 years
    @CzarekTomczak True, but just by using this answer's method, you could reuse it with different hashing algorithms just by changing the command itself. md5sum -> sha256sum without remembering what amount of characters you need to "cut".
  • Anton Tarasenko
    Anton Tarasenko over 5 years
    Also gmd5sum from coreutils would work on MacOS like md5sum in other answers mentioned here.
  • mgutt
    mgutt almost 5 years
    SO: Searching how to create a md5 hash of a file, learning the shortcut access of the first array element. Thanks :)
  • Stepan Kolesnik
    Stepan Kolesnik about 4 years
    To make the script a bit more bulletproof make sure to set IFS explicitly in case it is set to some non-standard value: IFS=" " md5=($(md5sum file))
  • scrutari
    scrutari over 3 years
    This is a nice solution, but it doesn't work in Jenkins
  • MoonCactus
    MoonCactus over 3 years
    Why capture? You can simply delete the space and what is behind: sed 's: .*::' does the same thing!
  • Peter Mortensen
    Peter Mortensen about 3 years
    The question is tagged with Bash, but perhaps indicate how much of this is Bash-specific (e.g., would it work in Z shell (now allegedly the default shell in macOS v10.15 (Catalina) and later)).
  • Jhoan Manuel Muñoz Serrano
    Jhoan Manuel Muñoz Serrano almost 3 years
    It worked for me on version 11.5.2 but after removing the -q option
  • Cameron Hudson
    Cameron Hudson almost 3 years
    md5 is different from md5sum, which is what OP asked about.