Skip to content

Instantly share code, notes, and snippets.

@mitio
Created June 28, 2010 16:43
Show Gist options
  • Save mitio/456074 to your computer and use it in GitHub Desktop.
Save mitio/456074 to your computer and use it in GitHub Desktop.
Simple command-line tool to print the total file size of files, newer than a given date.
#!/usr/bin/php
<?
if ($argc < 3) {
error_log("Usage:\n\t" . __FILE__ . " newer-than-date path [path [path...]]");
exit(1);
}
$accumulated_size_in_bytes = 0;
$newer_than_timestamp = strtotime($argv[1]);
echo "Looking for files, newer than " . date('Y-m-d H:i:s', $newer_than_timestamp) . "...\n";
for ($i = 2; $i < $argc; $i++) {
$accumulated_size_in_bytes += accumulate_file_size_for(rtrim($argv[$i], '/'), $newer_than_timestamp);
}
// produce a more human-friendly version of the file size (which is in bytes)
$human_readable_size = $accumulated_size_in_bytes;
foreach (array('B', 'K', 'M', 'G', 'T') as $type) {
if ($human_readable_size < 1024) {
$human_readable_size = round($human_readable_size, 2) . $type;
break;
}
$human_readable_size /= 1024.0;
}
echo "Size: $accumulated_size_in_bytes ($human_readable_size)\n";
function accumulate_file_size_for($path = '.', $newer_than_timestamp) {
$accumulated_size_in_bytes = 0;
if (is_dir($path)) {
$dh = opendir($path);
if ($dh !== false) {
while (($f = readdir($dh)) !== false) {
if ($f == '.' || $f == '..') continue;
$p = $path . '/' . $f;
$accumulated_size_in_bytes += accumulate_file_size_for($p, $newer_than_timestamp);
}
closedir($dh);
}
} else {
if (filemtime($path) >= $newer_than_timestamp) {
$accumulated_size_in_bytes += filesize($path);
}
}
return $accumulated_size_in_bytes;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment