1. 程式人生 > >【linux】md5sum 命令詳解

【linux】md5sum 命令詳解

1、命令詳解
$ md5sum --help
Usage: md5sum [OPTION]... [FILE]...
Print or check MD5 (128-bit) checksums.
With no FILE, or when FILE is -, read standard input.

  -b, --binary         read in binary mode
  -c, --check          read MD5 sums from the FILEs and check them
      --tag            create a BSD-style checksum
  -t, --text           read in text mode (default)

The following three options are useful only when verifying checksums:
      --quiet          don't print OK for each successfully verified file
      --status         don't output anything, status code shows success
  -w, --warn           warn about improperly formatted checksum lines

      --strict         with --check, exit non-zero for any invalid input
      --help     display this help and exit
      --version  output version information and exit

The sums are computed as described in RFC 1321.  When checking, the input
should be a former output of this program.  The default mode is to print
a line with checksum, a character indicating input mode ('*' for binary,
space for text), and name for each FILE.

Report md5sum bugs to 
[email protected]
GNU coreutils home page: <http://www.gnu.org/software/coreutils/> General help using GNU software: <http://www.gnu.org/gethelp/> For complete documentation, run: info coreutils 'md5sum invocation'

-b, --binary:以二進位制方式讀取檔案
-t, --text :預設配置,以文字方式讀取檔案
這兩種方式的輸出結果相同,測試例子如下:

$ md5sum hello.c 
6a29375b3e989a04355ecee250448c6e  hello.c
$ md5sum -b hello.c 
6a29375b3e989a04355ecee250448c6e *hello.c
$ md5sum a.out 
40a9477fa25a9df86361571dd60feffc  a.out
$ md5sum -b a.out 
40a9477fa25a9df86361571dd60feffc *a.out

-c, --check:從指定文字中讀取md5值,然後檢測MD5值對應的檔案是否完整,這個“指定文字”的格式如下:

40a9477fa25a9df86361571dd60feffc  a.out

就是在計算檔案MD5值時的輸出結果,使用例子如下:

$ md5sum a.out > md5.txt
$ cat md5.txt 
40a9477fa25a9df86361571dd60feffc  a.out
$ md5sum -c md5.txt 
a.out: OK

當檢查出錯誤時的列印資訊如下

$ md5sum -c md5.txt 
a.out: FAILED
md5sum: WARNING: 1 computed checksum did NOT match

–tag:安裝BSD樣式輸出結果

$ md5sum --tag a.out 
MD5 (a.out) = 40a9477fa25a9df86361571dd60feffc

下面三個選項,只有在檢查MD5值(-c)時有效,即和-c一起使用,否則會報錯:

$ md5sum --quiet  md5.txt 
md5sum: the --quiet option is meaningful only when verifying checksums
Try 'md5sum --help' for more information.

–quiet:如果檢查MD5值成功,不再列印OK


$ md5sum -c md5.txt 
a.out: OK
//成功時沒有輸出:
$ md5sum --quiet -c md5.txt 
//失敗才打印,錯誤資訊
$ md5sum -c md5.txt 
a.out: FAILED
md5sum: WARNING: 1 computed checksum did NOT match
$ md5sum --quiet -c md5.txt 
a.out: FAILED
md5sum: WARNING: 1 computed checksum did NOT match

–status:不列印任何資訊,執行結果以狀態碼形式輸出

//失敗情況
$ md5sum --status -c md5.txt 
$ echo $?
1
//成功情況
$ md5sum --status -c md5.txt 
$ echo $?
0

-w, --warn :如果檢測文字中有,非法的行,不符合md5sum -c需要的格式,則打印出警告資訊

$ md5sum --warn -c md5.txt 
a.out: OK
md5sum: md5.txt: 2: improperly formatted MD5 checksum line
md5sum: WARNING: 1 line is improperly formatted

–strict:對於無效的輸入,都返回非零狀態(不知道怎麼用)
–help:幫助資訊
–version:版本資訊