Skip to content

Instantly share code, notes, and snippets.

@psolru
Created October 24, 2019 15:26
Show Gist options
  • Save psolru/a3a81dffc6eff456abfd30be638fa320 to your computer and use it in GitHub Desktop.
Save psolru/a3a81dffc6eff456abfd30be638fa320 to your computer and use it in GitHub Desktop.
quick and dirty php script for some interval clean up
#!/usr/bin/php
<?php
chdir(__DIR__);
function se($str) {
$str = preg_replace('/[\\x0-\\x1F]/', '', $str);
$str = preg_replace('/\\x7F/', '', $str);
$str = str_replace("'", '\'"\'"\'', $str);
return "'".$str."'";
}
$dirs = [
'/home/patrick/.temp/',
'/home/patrick/.local/share/Trash/files/',
];
foreach ($dirs as $dir) {
$dir = rtrim($dir, '/');
$interval = trim(file_get_contents($dir.'/__AUTODELETE'));
if ($interval) {
var_dump($dir.'/{,.}[!.,!..]*');
foreach (glob($dir.'/{,.}[!.,!..]*', GLOB_MARK|GLOB_BRACE) as $el) {
if (basename($el) == '__AUTODELETE') continue;
echo "- ".$el;
if (filemtime($el) <= strtotime("-".$interval." days")) {
shell_exec('rm -rf '.se($el));
echo "delete";
}
echo "\n";
}
}
}
@psolru
Copy link
Author

psolru commented Oct 24, 2019

must create __AUTODELETE in temp deleted directory with content: [0-9]+

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment