Linux using md5sum recursively to generate the entire directory MD5
today to use the md5sum operation directory, recursive generation of all the files in the directory MD5 value, the results found that it does not support recursive operations and then wrote a PHP script to process the
code:
<?php $path = '/data/www/bbs/source ';
$outfile = ' file.md5 ';
GET_FILE_MD5 ($path, $outfile);
function Get_file_md5 ($path, $outfile) {$path = RTrim ($path, '/');
if (function_exists (' Scandir ')) {$files = Scandir ($path);
foreach ($files as $v) {if ($v!= '. ' && $v!= ' ... ') {$file = $path. '
/'. $v;
if (Is_dir ($file)) {get_file_md5 ($file, $outfile); }else {file_put_contents ($outfile, Md5_file ($file). " ". $file."
\ n ", file_append);
}}}else {$files = Opendir ($path);
while ($f = Readdir ($files))!== false) {if ($f = = '. ' | | $f = = ' ... ')
Continue $file = $path. '
/'. $f;
if (Is_dir ($file)) {get_file_md5 ($file, $outfile); }else {file_put_contents ($outfIle, Md5_file ($file). " ". $file."
\ n ", file_append);
} closedir ($files);
}
}
Note: There are two spaces between the generated MD5 value and the file, or the following error occurs
Copy Code code as follows:
Md5sum:file1.md5:no properly formatted MD5 checksum lines
In a simpler, use Linux Find command a sentence to fix
Code:
Find/data/www/bbs/source-type f-print0 | xargs-0 md5sum > File2.md5
Test
Md5sum-c file1.md5
md5sum-c file2.md5
As shown in the figure
This prints all of the test results to the screen, and if the last one shows such information md5sum:warning:2 of 1147 computed checksums did not match indicates that 2 of the total 1147 articles are not compliant
And then we can
Md5sum-c FILE1.MD5 | grep FAILED
It's easy to know which files have been tampered with.