Using grep to match md5 hashes

前端 未结 4 721
独厮守ぢ
独厮守ぢ 2021-02-08 06:21

How can I match md5 hashes with the grep command?

In php I used this regular expression pattern in the past:

/^[0-9a-f]{32}$/i

But I t

4条回答
  •  天涯浪人
    2021-02-08 06:27

    A little one-liner which works cross platform on Linux and OSX, only returning the MD5 hash value (replace YOURFILE with your filename):

    [ "$(uname)" = "Darwin" ] && { MD5CMD=md5; } || { MD5CMD=md5sum; } \
        && { ${MD5CMD} YOURFILE | grep -o "[a-fA-F0-9]\{32\}"; }
    

    Example:

    $ touch YOURFILE
    $ [ "$(uname)" = "Darwin" ] && { MD5CMD=md5; } || { MD5CMD=md5sum; } && { ${MD5CMD} YOURFILE | grep -o "[a-fA-F0-9]\{32\}"; }
    d41d8cd98f00b204e9800998ecf8427e
    

提交回复
热议问题