Go The md5sum of Linux commands

Source: Internet
Author: User
Tags md5

Original: https://www.cnblogs.com/zhuxiaohou110908/p/5786893.html

1. Background

In the network transmission, transfer between devices, copying large files, etc., may occur before and after the transmission of data inconsistencies. This is likely to occur in a relatively unstable environment such as the network. Then verifying the integrity of the file is also imperative.

The md5sum command is used to generate and verify the MD5 value of the file. It checks the contents of the file bit by bits. Is the content of the file, regardless of the file name, which is the same as the MD5 value. The MD5 value is a 128-bit binary data that is converted to a 16 binary that is the binary value of the 32 (128/4) bit.

MD5 checksum, there is a small probability that different files generated by MD5 may be the same. A more secure check algorithm than MD5 is also available in the Sha* series.

In the network transmission, we verify the source file to obtain its md5sum, after the transfer is complete, verify its target file, and compare if the source and destination files MD5 consistent, it means that the file transfer no exception. Otherwise the description file is not transmitted correctly during transmission.


2. Important Options

    • -B reads the contents of the file into binary mode
    • -T reads the contents of the file into text mode
    • -C verifies an existing file based on the generated MD5 value
    • After the--status check is complete, the error or the correct prompt message is not generated and can be judged by the return value of the command.

3. Examples of Use

    • Generate file MD5 value
md5sum file   1: [[email protected] lianxi]# md5sum data       2:0a6de444981b68d6a049053296491e49  data
   1: [[email protected] lianxi]# md5sum *
2:0a6de444981b68d6a049053296491e49 Data
3:13df384c47dd2638fd923f60c40224c6 data2

Md5sum Checksum is the file content, regardless of the filename

    • Read and verify files in text or binary mode

-B reads content in binary mode
-T reads the contents of the file in text mode to verify
Although it is a different read-in mode, it is the same when it comes to MD5, because it is bit-wise.
The following text file, regardless of which mode reads MD5, is consistent.

1: [[email protected] lianxi]# file data   2:data:ascii text   3: [[email protected] lianxi]# md5sum data   4:0a6 de444981b68d6a049053296491e49  Data   5: [[email protected] lianxi]# md5sum-b data   6: 0a6de444981b68d6a049053296491e49 *data   7: [[email protected] lianxi]# md5sum-t data   8: 0a6de444981b68d6a049053296491e49  Data

    • MD5 value Redirection

Redirects the generated MD5 value to the specified file, usually with the extension of the file we are going to be named. MD5

   1: [[email protected] lianxi]# md5sum data > Data.md5   2: [[email protected] lianxi]# md5sum data   3:0a6de444981 b68d6a049053296491e49  Data   4: [[email protected] lianxi]# cat data.md5   5: 0a6de444981b68d6a049053296491e49  Data
    • REDIRECT MD5 of multiple files to the specified file

The md5 of each file becomes a row

   1: [[email protected] lianxi]# ls   2:data  data.bak  data.md5  d.md5   3: [[email protected] lianxi]# md5sum data* > D.md5   4: [[email protected] lianxi]# cat d.md5   5:0a6de444981b68d6a049053296491e49< C17/>data   6:0a6de444981b68d6a049053296491e49  data.bak   7:0bd94658869c53cdcdf35a0f7de93e01  Data.md5
    • REDIRECT Append

This adds the file LS, MD5 it, and appends its MD5 to the file.

   1: [[email protected] lianxi]# cp/bin/ls.   2: [[email protected] lianxi]# ls   3:data  data.bak  data.md5  d.md5  ls   4: [[email protected] lianxi]# md5sum ls >> d.md5   5: [[email protected] lianxi]# cat d.md5   6:0a6de444981b68d6a049053296491e49< C11/>data    7:0a6de444981b68d6a049053296491e49  data.bak   8:0bd94658869c53cdcdf35a0f7de93e01  DATA.MD5   9:c6337b20f3c159544bff5cf622391f9e  ls
    • MD5 check

-C option to verify the file MD5. Checksum, the checksum is based on the generated MD5. Generates the MD5 of the current file and compares it to the previously generated MD5, returns OK if it is consistent, or returns an error message
Md5sum–c D.MD5

   1: [[email protected] lianxi]# md5sum-c d.md5   2:data:ok   3:data.bak:ok   4:data.md5:ok    5:ls:ok

File MD5 changes after you modify the file

   1: [[email protected] lianxi]# ls   2:data   3: [[email protected] lianxi]# md5sum data   4:2360752c3368ca4f89169 f5ecc06e383  Data   5: [[email protected] lianxi]# md5sum data  > DATA.MD5   6: [[email protected] lianxi] # echo "LWG" >> data   7: [[email protected] lianxi]# md5sum data   8:287d237083a42f09785daa46a5fa3afe  Data   9: [[email protected] lianxi]# md5sum-c data.md5   10:data:failed   11:md5sum:warning:1 of 1 compute D Checksum did not match

--status, does not display the checksum information, the command returns the value to judge
Checksum return 0, inconsistent return 1

   1: [[email protected] lianxi]# md5sum-c data.md5   2:data:failed   3:md5sum:warning:1 of 1 computed checksum did Not match   4: [[email protected] lianxi]# md5sum-c--status data.md5   5: [[email protected] lianxi]# echo $?   6:1
    • Multiple file file checksums with grep

Filter out the right information with grep

   1: [[email protected] lianxi]# md5sum-c. /VALUE.MD5   2:acpid:ok   3:acpid.1:ok   4:anaconda.log:ok   5:anaconda.syslog:ok   6: Anaconda.xlog:OK   7:boot.log:ok   8:boot.log.1:ok   9:   ... Ten:   ... 11: Omit Middle part  : ...   :   ... 14:yum.log.2:ok  15:md5sum:warning:1 of computed checksums did not match     1: [[email protected] lianxi]# m D5sum-c. /VALUE.MD5 | Grep-v OK     2:md5sum:warning:1 of computed checksums did not match     3:cron.1:failed

Special Instructions
1) md5sum is verifying the contents of the file, regardless of the same file name

2) The Md5sum value is bit-wise, so the larger the file, the longer the check time.

Summarize
Verify the generated file checksum by md5sum, to discover the inconsistency of file content caused by file transfer (network transmission, replication, transmission between different devices in the local).


Working Practice Application Scenario:

Need to compare 2 rc1.tar.gz packages and rc2.tar.gz Two packages, change is not consistent with development said

1. Get the package and make sure the package is correct

After obtaining the package, verify the MD5 value of the package: Md5sum rc*.tar.gz

2. Unzip to the specified directory

Ensure that the corresponding directory exists

TAR-ZXVF rc1.tar.gz-c./test_rc1

TAR-ZXVF rc2.tar.gz-c./test_rc2

3. Recursive generation of MD5 values for each file

Find./test_rc1-type f-print0| xargs-0 md5sum >> Rc1_md5.txt

Find./test_rc2-type f-print0| xargs-0 md5sum >> Rc2_md5.txt

4. Compare the MD5 values of two files

Diff-c Rc1_md5.txt Rc2_md5.txt

or using Ultracompare Professional to compare the results.

Go The md5sum of Linux commands

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.