I. Source of the problem
Sometimes copy some important data, and then need to check with the original data whether the data is consistent. This requires a checksum.
Two. Analysis of the problem
There are several ways to verify the data, and the simplest is to use the file's MD5 code for verification. But a lot of files, manual each file for MD5 verification is obviously inappropriate, this need to write shell program resolution.
Three. Resolution of the problem
solve file checksum problem through shell program
1. Get the file MD5 value by command
For example, the Data folder to be
enter the following command in the Data folder
Find./-type F-print | Xargs md5sum >/tmp/md5.1cd/tmpcat md5.1 | Sort > MD5.1.1RM md5.1 mv md5.1.1 md5.1
2. Enter the following command in the Data folder in the original folder
Find./-type F-print | Xargs md5sum >/tmp/md5.2cd/tmpcat md5.2 | Sort > Md5.2.1rm md5.2mv md5.2.1 md5.2
3. Put the above md5.1 and md5.2 on the same server in the same directory, such as from server_1 SCP to server_2
SCP server_1:/tmp/md5.1/tmp/
4. Perform diff If the output is empty, the data is always, otherwise, the data is inconsistent
diff/tmp/md5.1/tmp/md5.2
Four. General Ideas
by getting the MD5 value of all the files under the original folder and in the new folder, and then sorting the MD5 values, and finally comparing the two MD5 values .
Five. Note
In fact, the contents of md5.1 (or md5.2) in the above operation are in the format "file name MD5 value".
Six. Fully Automatic shell program
For example, there are two machines server_1 and server_2, just from server_1 on the/home/longxibendi/under the SCP Data folder to server_2 /home/longxibendi/
The procedure is as follows:
#!/bin/bash#author longxibendi#function check file1 and file2 use their md5 #support foldercd /home/longxibendi/datafind ./ -type f -print | xargs md5sum > /tmp/md5.1cd /tmpcat md5.1 | sort > md5.1.1rm Md5.1 mv md5.1.1 md5.1ssh [email protected]_2cd /home/longxibendi/datafind ./ -type f -print | xargs md5sum > /tmp/md5.2cd /tmpcat md5.2 | sort > md5.2.1rm md5.2mv md5.2.1 md5.2scp server_1:/tmp/md5.1 /tmp/md5.2# if File1 and file2 is same ,then print it is ok # else if file1 and file2 is different ,then print they are differentif [ -z "' diff md5.1 md5.2 '" ] ;then echo "It is ok";else echo "They are different"; fi
This article from "11726068" blog, declined reprint!
Automatically verifies the folder and its contents Shell program (MD5 all data in the folder)