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}"
Related videos on Youtube
![John Doe](https://i.stack.imgur.com/uZFzR.png?s=256&g=1)
Author by
John Doe
Updated on July 29, 2021Comments
-
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 over 7 yearsVery surprising this isn't an option for md5sum.
-
TommyAutoMagically almost 7 yearsAgreed! Why isn't this an option? Can a GNU-Master shed some light?
-
Magnetic_dud about 4 yearsWhy do you need to have a "only hash" flag when you can trim the result with some regex? (I am sarcastic)
-
-
Roman Cheplyaka almost 14 yearsNice. One note -- on the first line you don't need quotes around
$()
(although they do no harm) but certainly need them around${}
. -
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 over 9 yearsAwesome. 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 over 9 yearsOr
md5=`md5sum < ${my_iso_file}`
However this still outputs " - " at the end. But for comparisons this should be enough. -
Andy almost 9 yearsthe 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 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 output68b329da9893e34099c7d8ad5cb9c940
-
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 almost 9 yearsHow come
echo ($(echo -n foo | md5sum))
doesn't work? Errors outbash: syntax error near unexpected token $(echo -n foo | md5sum)'
-
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 over 8 yearsThis 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 over 7 yearsdoesn't work on my Mac OS X 10.7. But thanks for posting, for whatever version this works on.
-
alper almost 6 yearsWrong it gives following output on Mac
MD5 (/Users/hello.txt) = 24811012be8faa36c8f487bbaaadeb71
and your code returnsMD5
. -
alper almost 6 yearsYou can get run of
-
by adding| awk '{print $1}'
end of your code =>md5sum < ${my_iso_file} | awk '{print $1}'
@ChristopheDeTroyer -
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 over 5 yearsAlso
gmd5sum
fromcoreutils
would work on MacOS likemd5sum
in other answers mentioned here. -
mgutt almost 5 yearsSO: Searching how to create a md5 hash of a file, learning the shortcut access of the first array element. Thanks :)
-
Stepan Kolesnik about 4 yearsTo 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 over 3 yearsThis is a nice solution, but it doesn't work in Jenkins
-
MoonCactus over 3 yearsWhy capture? You can simply delete the space and what is behind:
sed 's: .*::'
does the same thing! -
Peter Mortensen about 3 yearsThe 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 almost 3 yearsIt worked for me on version
11.5.2
but after removing the-q
option -
Cameron Hudson almost 3 years
md5
is different frommd5sum
, which is what OP asked about.