Skip to content

Instantly share code, notes, and snippets.

@fvdm fvdm/dud.php
Last active Dec 19, 2015

Embed
What would you like to do?
Mac OSX alternative script for `alias dud='du -ahd 1 | sort -hr | head -n 11'` on Debian. -- https://frankl.in/code/755-top-10-files-directories-by-size
#!/usr/bin/php
<?php
$results = 11; // add 1 for dir total
$cwd = getcwd();
$dir = scandir( $cwd );
foreach( $dir as $item ) {
if( $item === '..' ) { continue; }
$path = $cwd .'/'. $item;
if( is_dir( $path ) ) {
$items[ $item ] = dirsize( $path );
} elseif( is_file( $path ) ) {
$items[ $item ] = filesize( $path );
}
}
arsort( $items );
$r = $results;
foreach( $items as $name => $size ) {
if( $r == 0 ) { continue; }
$human = bytes2human( $size );
echo "$human\t$name\n";
$r--;
}
function dirsize( $d ) {
return human2bytes( trim( shell_exec( 'du -hs '. escapeshellarg($d) ) ) );
}
function bytes2human( $size ) {
$units = array('', 'K', 'M', 'G', 'T');
$unit = 0;
while( $size >= 1024 && $unit < 5 ) {
$size = $size / 1024;
$unit++;
}
return round( $size, 1 ) . $units[ $unit ];
}
function human2bytes( $size ) {
$units = array(
'K' => 1024,
'M' => 1048576,
'G' => 1073741824,
'T' => 1099511627776
);
preg_match( '/^([\d\.]+)([A-Z])\b/', $size, $s );
$unit = $units[ $s[2] ] ?: 1;
$size = $s[1];
$size = round( $size * $unit );
return $size;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.