~ overflow ~

Archive for June 5th, 2009

md5 File Comparsion PHP Script

by z3n on Jun.05, 2009, under Coding, Tips & Hints

Problem:

I had a bunch of files but i didn’t knew which ones were doubles and which ones were unique, they had different names and belong to different subfolders.

Solution:

A PHP script that reads and do a md5 hash on every file then do a comparsion.

<?
  1.  
  2. // (c) z3n – R1V1@090605 – z3n666@gmail.com – www.overflow.biz
  3.  
  4. define("is_debug",1);
  5. define("_dbp","_debug.log");
  6. define('_bf',512288); // debug buffer
  7. define('_dw',250); // maximum debug file size, _bf*_dw bytes
  8. define('_cr',chr(13).chr(10)); // CRLF definition
  9. define('_dt_sml','Y-m-d h:i:s'); // date small format (for debug)
  10. ini_set("memory_limit","128M"); // avoid happyness
  11.  
  12. // functions
  13.  
  14. function _o($x) { if (is_debug) { _dbg($x); } echo $x."\n"; }
  15. function _fw($fn,$txt,$m) { /* v1.5 with auto mkdir */ global $__fw,$__fwb;$__fw++;$__fwb+=strlen($txt);$x=1;$fn=str_replace("\\","/",$fn);if (strpos($fn,"/") !== false) { $p=substr($fn,0,strrpos($fn,"/"));if (!is_dir($p)) { _o("Mkdir: ".$p);mkdir($p,777,true); } } if ((is_writable($fn)) || (!file_exists($fn))) { if (!$fp=fopen($fn,$m)) { $x=0;break; } if (!fwrite($fp,$txt)) { $x=0;break; } fclose($fp); } _o("Wrote: ".$fn);return $x; }
  16. function _dbg($x,$f=0) { /* v1.9 */ global $_bg,$_dw;if ((is_debug) &amp;&amp; ($_dw &lt; _dw)) { if ($x != "") { $_bg.=date(_dt_sml).":".$x._cr; } if ((strlen($_bg) &gt; _bf) || ($f)) { $x=$_bg;$_dw++;$_bg="";_fw(_dbp,$x,"a"); } } }
  17. function rddir($dir) { /* v2.11 */
  18. global $tfile,$tdir,$ij;$i=0;$j=count($tdir);
  19. if (is_dir($dir)) {
  20. if ($dh=opendir($dir)) {
  21. while (($file=readdir($dh)) !== false) {
  22. if (is_dir($dir.$file)) {
  23. if (($file != ".") &amp;&amp; ($file != "..")) {
  24. $tdir[$j]=$dir.$file;$j++;
  25. } } else { $tfile[$i]=$file;$i++;
  26. } } closedir($dh); } } $ij=$i; }
  27.  
  28. // main
  29.  
  30. $_bg="";$_dw=$__fw=$__fwb=0;$_x=array();$_f=array();
  31.  
  32. if (!isset($argv[1])) { _o("Usage: ".$_SERVER['PHP_SELF']." &lt;path/to/&gt;");die; }
  33.  
  34. for ($tdir[0]=$argv[1],$k=$i=0,rddir($tdir[$i]);$i&lt;count($tdir);$i++) {
  35.  $tdir[$i].="/";
  36.  for ($j=0,$m=count($tfile),_o("Found: ".$m." files @ ".$tdir[$i]."\nBuilding md5 hash…");$j&lt;$m;$j++) {
  37.   if (($tfile[$j] != ".") &amp;&amp; ($tfile[$j] != "..")) {
  38.    $h=md5(file_get_contents($tdir[$i].$tfile[$j]));
  39.    $_x[$k]=$h;
  40.    $_f[$k]=$tdir[$i].$tfile[$j];
  41.    $x=array_keys($_x,$h);
  42.    for ($o=0,$p=count($x);$o&lt;$p;$o++) {
  43.     if ($x[$o] != $k) {
  44.      _o($_f[$o]." = ".$_f[$k]." (".$h." – ".$j.")");
  45.     }
  46.    }
  47.    $k++;
  48.   }
  49.  }
  50. }
  51.  
  52. _dbg("",1);_o("Done!");
  53.  
  54. ?&gt;
Leave a Comment :, , , , more...

Real VNC Viewer on Windows + VNC Server on Linux = Accent Issues

by z3n on Jun.05, 2009, under Linux Happyness

Problem:

Accents (á,à,ã,etc) not working on vnc viewer, for example:

instead of  á it shows  ”a (double ‘)

Solution:

I’ve been looking around for a long time to fix this issue, i haven’t found any relevant information about it.

So i decided to install a different VNC client program, turns out that TightVNC Fixed the issue.

Note that it will not work if you don’t configure your keyboard proprietly on linux, I write in portuguese and english, so as keyboard i have Generic International keyboard, as layout Brazil / Brazil.

Leave a Comment :, , , , , , , , more...

Mass moving files from a folder to another by bash

by z3n on Jun.05, 2009, under Linux Happyness

Problem:

How to move files from multiple subfolders using bash?

Solution:

for b in `find /path/to/search/ -depth -name filename_or_wildcard`;do mv $b /path/to/destination/${b##*/};done;

Example:

for b in `find ./ -depth -name *.tah`;do mv $b ../../TAH/${b##*/};done;

This will move all .tah files to ../../TAH/ no matter how depth on the search path they are they will be put all into the same folder, the main complication here is to get the basename of the filename, when you do a find it will show the whole path as the result, so this is where ${b##*/} come in, it will strip out the path and leave on the basename.

Source:

Linux Gazette Issue 18

Leave a Comment :, , more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!