Use php to compare the similarities and differences between the two folders
Requirement: you can only use command lines to compare the differences between two folders, including the differences between files.
Thinking: Although there is diff in linux .... Use php. the code is easy to modify and the speed is fast. the comparison files in the. svn directory should compare the md5 checksum.
Idea: 1) use the first path as the standard path to list files or folders not found in 1st paths or different files. 2) then, list all the files and folders that do not exist in the 2nd paths.
Call example: php compare_folder.php/home/temp/2/home/temp/55 // reprinted from JAVAEYE
- /**
- * Tool files
- * The purpose is to recursively compare two folders
- *
- * Call example
- * Php compare_folder.php/home/temp/2/home/temp/55
- *
- */
- // Confirm the parameter
- If (count ($ argv)> 1)
- $ Dir1 = del_postfix ($ argv [1]);
- Else
- $ Dir1 = '/';
- If (count ($ argv)> 2)
- $ Dir2 = del_postfix ($ argv [2]);
- Else
- $ Dir2 = '/';
- // Check whether the first path has a method that does not exist or is incorrect.
- Process_compare ($ dir1, $ dir2, 0 );
- Echo "============================================== =====================================\ n ";
- // Check the excess folders or files in the 2nd paths
- Process_compare ($ dir2, $ dir1, 1 );
- Echo "all OK \ n ";
- /**
- * 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;
- }
- /**
- * A common function calls a recursive method for comparison.
- *
- * @ Param string $ dir1 as the standard path
- * @ Param string $ path used for comparison of dir2
- * @ Param int $ only_check_has: 1 indicates no Comparison of file differences. 0 indicates comparison of the md5 checksum of the file.
- */
- Function process_compare ($ dir1, $ dir2, $ only_check_has ){
- Compare_file_folder ($ dir1, $ dir1, $ dir2, $ only_check_has );
- }
- /**
- * Real functions, private functions
- *
- * @ Param string $ dir1 path 1, standard
- * @ Param string $ base_dir1 constant parameter path 2
- * @ Param string $ base_dir2: unchanged path to be compared 2
- * @ Param int $ only_check_has: 1 indicates no Comparison of file differences. 0 indicates comparison of the md5 checksum of the file.
- *
- */
- 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, 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 );
- }
- }
- }
- ?>
|