Skip to content

Instantly share code, notes, and snippets.

@cebe
Created October 5, 2016 20:20
Show Gist options
  • Save cebe/937c091e9ad071225d1ff1c707b054a7 to your computer and use it in GitHub Desktop.
Save cebe/937c091e9ad071225d1ff1c707b054a7 to your computer and use it in GitHub Desktop.
PHP Scripts that can help you debug and remove huge (order of 10million files) directories.
<?php
// get the file count, because `ls |wc -l` will not complete in reasonable time
// usage: php ls.php path/
$path = $argv[1];
$dir = opendir($path);
if (!$dir) {
echo "unable to open dir\n";
exit(1);
}
$i = 0;
while (false !== ($entry = readdir($dir))) {
++$i;
if ($i % 100 === 0)
echo "\r$i";
}
echo "\r$i\n";
closedir($dir);
<?php
// remove all files in the directory matching a pattern
// CAUTION: THIS HAS NOT BEEN TESTED WELL, USE AT OWN RISK, HAVE A BACKUP!
// usage: php rm.php path/
$path = $argv[1];
echo "deleting files in $path\n";
$dir = opendir($path);
if (!$dir) {
echo "unable to open dir\n";
exit(1);
}
$i = 0;
while (false !== ($entry = readdir($dir))) {
//if ($entry != "." && $entry != "..") {
// echo $entry . "\n";
//}
if (strpos($entry, 'sess_') === 0) {
unlink("$path/$entry");
++$i;
if ($i % 100 === 0) {
echo "\r$i";
usleep(100000);
}
}
if ($i % 1000 === 0) usleep(500000);
}
closedir($dir);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment