Skip to content

Instantly share code, notes, and snippets.

@mexitek
Created July 9, 2013 05:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mexitek/5954939 to your computer and use it in GitHub Desktop.
Save mexitek/5954939 to your computer and use it in GitHub Desktop.
Run this PHP script via command line and see all the directories with contents over 1 KB.
<?php
// Base directory is where you run the script from
$currentDirectory = '.';
// Get all contents, minus pointers to . and ..
$baseLevelContents = array_diff( scandir( $currentDirectory ), array('..','.') );
// All directories
$baseDirs = array_filter( $baseLevelContents, "is_dir" );
// All base files
$baseFiles = array_diff( $baseLevelContents, $baseDirs );
// Iterate through the all the directories
foreach( $baseDirs as $dir ) {
// Recursively get the total file sizes
$totalFileSize = getFileSizeSum( $dir );
// If exceeds limit, do something
if( $totalFileSize/1024 > 1 ) {
echo $dir."/ seems to contain ".number_format($totalFileSize/1024)." KB of data.";
}
}
// Method that returns the sum of all file sizes
function getFileSizeSum( $dir ) {
$sum = 0;
// Get all contents, minus pointers to . and ..
$contents = array_diff( scandir( $dir ), array('..','.') );
// All directories
$dirs = array_filter( $contents, "is_dir" );
// All base files
$files = array_diff( $contents, $dirs );
// Sum all the files
foreach( $files as $file ) {
$sum += filesize( $dir."/".$file );
}
// Recusively call all sub directories
foreach( $dirs as $subDir ) {
$sum += getFileSizeSum( $dir."/".$subDir );
}
// Return the sum
return $sum;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment