How to use the Linux md5sum command

How to use the Linux md5sum command

01. Command Overview

md5sum - Calculate and verify the MD5 verification code

The md5sum command uses the MD5 message digest algorithm (128 bits) to calculate and check the checksum of a file. Generally speaking, after installing Linux, there will be a tool called md5sum, which can be run directly in the command line terminal.

The MD5 algorithm is often used to verify the integrity of network file transmission and prevent files from being tampered with. MD5 stands for Message-Digest Algorithm 5. This algorithm calculates information of any length bit by bit to generate a "fingerprint" (or "message digest") with a binary length of 128 bits (hexadecimal length is 32 bits). The possibility of different files generating the same message digest is very, very small.

02. Command format

Usage: md5sum [options]... [files]...

03. Common options

Display or verify the MD5 verification code. If there is no FILE or FILE is -,
Read from standard input.

-b, --binary
Read the file in binary mode (default under DOS/Windows)
-c, --check
Verify the MD5 verification code of the given file
-t, --text
Read the file in text mode (default)
The following two options are only used when checking the verification code:
--status
No output is displayed, and the return code indicates success or failure
-w, --warn
Warn about incorrect MD5 checksum line format
--help show this help and exit
--version
Display version information and exit

04. Reference examples

4.1 Generate file md5 value

[deng@localhost test]$ md5sum /etc/passwd
134edeaf80dc359ed33dc53eb8967920 /etc/passwd
[deng@localhost test]$

4.2 Generate multiple file md5 values

[deng@localhost test]$ md5sum * 
134edeaf80dc359ed33dc53eb8967920 passwd
134edeaf80dc359ed33dc53eb8967920 passwd1
[deng@localhost test]$

Note: md5sum verifies the file content and has nothing to do with the file name**

4.3 Calculate the md5 value of the same file in different modes

[deng@localhost test]$ file passwd 
passwd: ASCII text
[deng@localhost test]$ md5sum passwd
134edeaf80dc359ed33dc53eb8967920 passwd
[deng@localhost test]$ md5sum -b passwd
134edeaf80dc359ed33dc53eb8967920 *passwd
[deng@localhost test]$ md5sum -t passwd
134edeaf80dc359ed33dc53eb8967920 passwd
[deng@localhost test]$

Although they are different reading modes, they are the same when calculating md5 because they are checked bit by bit. The following text file has the same md5 no matter which mode is used to read it.

4.4 md5 redirection

Redirect the generated md5 value to the specified file, usually the file extension is .md5

[deng@localhost test]$ md5sum passwd > passwd.md5
[deng@localhost test]$ md5sum passwd
134edeaf80dc359ed33dc53eb8967920 passwd
[deng@localhost test]$ cat passwd.md5 
134edeaf80dc359ed33dc53eb8967920 passwd
[deng@localhost test]$ 

4.5 Redirect the md5 of multiple files to a specified file

[deng@localhost test]$ md5sum * > d.md5
[deng@localhost test]$ cat d.md5 
134edeaf80dc359ed33dc53eb8967920 passwd
134edeaf80dc359ed33dc53eb8967920 passwd1
8b7e9d66d329c74071b8a01800f4deb9 passwd.md5
[deng@localhost test]$ 

4.6 Redirection Append

Here, add a new file ls, calculate its md5 separately, and append its md5 to the file

[deng@localhost test]$ md5sum /bin/ls >> d.md5
[deng@localhost test]$ cat d.md5
134edeaf80dc359ed33dc53eb8967920 passwd
134edeaf80dc359ed33dc53eb8967920 passwd1
8b7e9d66d329c74071b8a01800f4deb9 passwd.md5
a78c13d806e594dc4014d145d689f23d /bin/ls
[deng@localhost test]$ 

4.7 md5 verification

-c option to verify the file md5. During verification, the verification is performed based on the generated md5. Generate the md5 of the current file and compare it with the previously generated md5. If they are consistent, it returns OK, otherwise it returns an error message

[deng@localhost test]$ md5sum -c d.md5 
passwd: OK passwd1: OK passwd.md5: OK /bin/ls: OK [deng@localhost test]$ 

After modifying the file, the file md5 changes

[deng@localhost test]$ vim passwd
[deng@localhost test]$ md5sum -c d.md5 
passwd: failed passwd1: ok passwd.md5: ok /bin/ls: ok md5sum: warning: 1 checksum mismatch [deng@localhost test]$ 

4.8 Do not display any output, use the return code to indicate success or failure

–status, do not display verification information, judge based on command return value. If the verification is consistent, it returns 0; if it is inconsistent, it returns 1

[deng@localhost test]$ md5sum -c --status d.md5 
[deng@localhost test]$ echo $?
1
[deng@localhost test]$ 

4.9 Filter out files that have been verified to be OK

[deng@localhost test]$ md5sum -c d.md5 | grep -v "OK"
md5sum: warning: 1 checksum mismatch passwd: failed [deng@localhost test]$

Special Instructions
1) md5sum verifies the file contents and has nothing to do with whether the file name is the same

2) The md5sum value is verified bit by bit, so the larger the file, the longer the verification time.

Summarize

Use md5sum to generate a file verification code to detect file content inconsistencies caused by abnormal file transfer (network transmission, copying, and transmission between different local devices).

Work practice application scenarios:

You need to compare the rc1.tar.gz package and the rc2.tar.gz package to see if the changes are consistent with what the developer said.

1. Get the package and make sure it is correct

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

2. Unzip to the specified directory

Make sure the corresponding directory exists

tar -zxvf rc1.tar.gz -C ./test_rc1
tar -zxvf rc2.tar.gz -C ./test_rc2

3. Recursively generate 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 use UltraCompare Professional to compare the results

The above is the detailed usage of Linux md5sum command. For more information about Linux md5sum command, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Ruby iterate through folders while calculating md5sum of files
  • Using md5sum in Python to check the same files in the directory code sharing

<<:  A simple example of how to implement fuzzy query in Vue

>>:  Detailed steps to install mysql in Win

Recommend

Use Grafana+Prometheus to monitor MySQL service performance

Prometheus (also called Prometheus) official webs...

Vue project code splitting solution

Table of contents background Purpose Before split...

Multi-service image packaging operation of Dockerfile under supervisor

Writing a Dockerfile Configure yum source cd /tmp...

Understand CSS3 FlexBox elastic layout in 10 minutes

Basic Introduction Features Flexbox is a CSS disp...

Vue ElementUI Form form validation

Form validation is one of the most commonly used ...

How to turn a jar package into a docker container

How to turn a jar package into a docker container...

A very detailed explanation of the Linux DHCP service

Table of contents 1. DHCP Service (Dynamic Host C...

Detailed steps and problem solving methods for installing MySQL 8.0.19 on Linux

I recently bought a Tencent Cloud server and buil...

Improvements to the web server to improve website performance

<br />In the first section of this series, w...

Detailed explanation of mysql5.6 master-slave setup and asynchronous issues

Table of contents 1. MySQL master-slave replicati...

How to install setup.py program in linux

First execute the command: [root@mini61 setuptool...

Complete the search function in the html page

Recently I've been working on a framework tha...

Nginx proxy forwarding implementation code uploaded by Alibaba Cloud OSS

Preface Because the mini program upload requires ...

How to install mysql6 initialization installation password under centos7

1. Stop the database server first service mysqld ...