#!/usr/bin/env php <?php clearstatcache(); $delete = !empty($argv[1]) && $argv[1] === 'delete'; /** * @param string $folder * @param string|null $pattern * * @return \Generator */ function rsearch(string $folder, ?string $pattern = null): Generator { $iti = new RecursiveDirectoryIterator($folder); foreach (new RecursiveIteratorIterator($iti) as $file) { $file_name = $file->getPathname(); if ($file->isDir() || ($pattern !== null && !preg_match($pattern, $file_name))) { continue; } yield $file_name; } } $pwd = $_SERVER['PWD']; $checksum = []; if (!is_dir($pwd)) { die($pwd . ' should be directory'); } $paths = rsearch($pwd, '/.* ([0-9]+)\..*$/su'); foreach ($paths as $path) { $action = 'skipped'; $path_sha1 = sha1_file($path); preg_match('/(.*)( (?:[0-9]+)\.)([^ ]+)$/su', $path, $matches); $path_org = $matches[1] . '.' . $matches[3]; if (file_exists($path_org)) { $path_org_sha1 = sha1_file($path_org); if ($path_org_sha1 === $path_sha1) { if ($delete === true) { unlink($path); $action = 'deleted'; } else { $action = 'found'; } } } echo $action . ' ' . $path . PHP_EOL; }