Skip to content

Instantly share code, notes, and snippets.

@Grendel7
Last active August 28, 2022 10:45
Show Gist options
  • Save Grendel7/84f120fccd93b050621f7c7b47ee7666 to your computer and use it in GitHub Desktop.
Save Grendel7/84f120fccd93b050621f7c7b47ee7666 to your computer and use it in GitHub Desktop.
PHP script to help identify high folders using many inodes
<?php
set_time_limit(0);
$directory = new RecursiveDirectoryIterator('../');
$counters = [];
foreach ($directory as $item) {
if (!is_dir($item)) {
continue;
}
$parts = explode('/', $item);
$basename = end($parts);
if (strpos($basename, '.') === 0) {
continue;
}
$itemIterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($item));
$counters[(string) $item] = iterator_count($itemIterator);
}
arsort($counters);
foreach ($counters as $path => $count) {
echo $path.': '.$count.'<br/>'.PHP_EOL;
}
echo "Total: ".array_sum($counters);
@Grendel7
Copy link
Author

Grendel7 commented Feb 6, 2022

There may be more efficient ways to do it, but you can get the total of all folders by adding this at the end:

echo "Total: ".array_sum($counters);

@dsl25
Copy link

dsl25 commented Feb 6, 2022

Many thanks for your feedback but I'm getting strange results that way:

../public_html: 173441
Total: 347313../www: 173441
Total: 347313../tmp: 201
Total: 347313../mail: 150
Total: 347313../ssl: 22
Total: 347313../logs: 17
Total: 347313../etc: 16
Total: 347313../ppdesign.cf: 9
Total: 347313../access-logs: 8
Total: 347313../public_ftp: 4
Total: 347313../wordpress-backups: 2
Total: 347313../lscache: 2
Total: 347313

This total is, in fact, the total of all of the above, including 2x 173441

@Grendel7
Copy link
Author

Grendel7 commented Feb 9, 2022

Are public_html and www perhaps the same folder? linked with a symlink? I know cPanel does that, and it would explain the identical file count.

I suppose it would be possible to folder out symlinks somehow, perhaps by tweaking the FilesystemIterator::FOLLOW_SYMLINKS flag on the RecursiveDirectoryIterator.

@dsl25
Copy link

dsl25 commented Feb 9, 2022

Yep, that's right, it's a symlink in cPanel and I resolved my issue by simply dividing the total by 2 as I just wanted to receive the total only by email with a cron. Many thanks for your help!

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