Linux系統遞迴組建目錄中檔案的md5的方法_php執行個體

來源:互聯網
上載者:User
linux下使用md5sum遞迴產生整個目錄的md5
今天要用md5sum操作目錄,遞迴組建目錄下所有檔案的md5值,結果發現它不支援遞迴操作於是寫了個php指令碼處理下
代碼:

  <?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);      }    } 

注意:產生的md5值和檔案之間是兩個空格,否則導致錯誤如下

複製代碼 代碼如下:md5sum: file1.md5: no properly formatted MD5 checksum lines found

在來個更簡單的,使用linux的find命令一句搞定
代碼:

  find /data/www/bbs/source -type f -print0 | xargs -0 md5sum > file2.md5 

測試

  md5sum -c file1.md5   md5sum -c file2.md5  


這樣把所有檢測結果輸出到螢幕上來了,如果最後一條顯示這樣的資訊 md5sum: WARNING: 2 of 1147 computed checksums did NOT match 則說明在總共1147條中有2條是不符合的
然後我們可以

  md5sum -c file1.md5 | grep FAILED 

就很容易知道是哪些檔案的篡改過

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.