Last active
December 31, 2020 10:07
-
-
Save mitsh/ae62e6819ab0080f335a541a38d6b606 to your computer and use it in GitHub Desktop.
I wrote this script for iCloud sync problem. all my documents and desktop folders are duplicated because of the problem. This script uses '* 2.*' pattern to find and check the original file if they are same it will delete if you add 'delete' and of the command. copy this file under /usr/local/bin/.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment