Requirements:
You can only use the command line to compare the differences between two folders, including file differences.
Thinking:
Although there is diff under linux. . . . Let’s use PHP. Code modification is easy and fast. The comparison of .svn directories is excluded below
The file needs to be compared with md5 checksum
Thinking:
1) Use the first path as the standard path and list the files or folders that are in the first path but not in the second path, or are different files.
2) Then, list the files and folders that exist in the second path but do not exist in the first path.
Call example:
php compare_folder.php /home/temp/2 /home/temp/55
//Reprinted from JAVAEYE
- /**
- * Tool file
- * The purpose is to recursively compare two folders
- *
- * Calling example
- * php compare_folder.php /home/temp/2 /home/temp/55
- *
- */
-
- //Parameter determination
- if (count($argv) > 1 )
- $dir1 = del_postfix($argv[1]);
- else
- $dir1 = '/';
-
- if (count($argv) > 2 )
- $dir2 = del_postfix($argv[2]);
- else
- $dir2 = '/';
-
- //Check the first One path has it, the latter doesn't or has the wrong method.
- process_compare($dir1, $dir2, 0);
- echo "====================================== ========================n";
-
- //Check the second path for extra folders or files
- process_compare($dir2, $dir1 , 1);
- echo "all OKn";
-
-
-
- /**
- * Remove the / at the end of the path and make sure it is an absolute path
- *
- * @param unknown_type $dir
- * @return unknown
- */
- function del_postfix($dir)
- {
- if (!preg_match('#^/#', $dir)) {
- throw new Exception('The parameter must be an absolute path');
- }
- $dir = preg_replace('#/$#', '', $dir);
- return $dir;
- }
-
-
- /**
- * Public function, which will call a recursive method to implement comparison
- *
- * @param string $dir1 as the standard path
- * @param string $dir2 The path used for comparison
- * @param int $only_check_has 1 means no file differences will be compared , a value of 0 means that the md5 checksum of the file must also be compared
- */
- function process_compare($dir1, $dir2, $only_check_has){
- compare_file_folder($dir1, $dir1, $dir2, $only_check_has);
- }
-
- /**
- * Real function, private function
- *
- * @param string $dir1 Path 1, which is the standard
- * @param string $base_dir1 Unchanged parameter path 2
- * @param string $base_dir2 Unchanged path 2 to be compared
- * @param int $only_check_has If it is 1, it means not to compare the file differences. If it is 0, it means the md5 checksum of the file should also be compared.
- *
- */
- function compare_file_folder( $dir1, $base_dir1, $base_dir2, $only_check_has=0){
- if (is_dir($dir1)) {
- $handle = dir($dir1);
- if ($dh = opendir($dir1)) {
- while ($entry = $handle->read()) {
- if (($entry != ".") && ($entry != "..") && ($entry != ".svn")){
- $new = $dir1."/".$entry;
- //echo 'compare: ' . $new . "n";
- $other = preg_replace('#^'. $base_dir1 .'#' , $base_dir2 , $new);
- if(is_dir($new)) {
- //Compare
- if (!is_dir($other)) {
- echo '!!not found direction: '. $other. ' (' . $new .")n";
- }
- compare_file_folder($new, $base_dir1,$base_dir2, $only_check_has) ;
- } else { //If 1 is a file, then 2 should also be a file
- if (!is_file($other) ) {
- echo '!!not found file: '. $other. ' ('.$new .")n";
- }elseif ($only_check_has ==0 && ( md5_file($other) != md5_file($new ) ) ){
- echo '!!file md5 error: '. $other. ' ('.$new .")n";
- }
- }
- }
- }
- closedir($dh);
- }
-
- }
- }
-
- ?>
Copy code
|