Jonathan Ohcamac (setm_ctrll) wrote,
Jonathan Ohcamac
setm_ctrll

*sumdir

There are a couple of ways to find out how large a directory is, but
there's always room for three more.
import java.io.File;
import java.io.IOException;
public class SumDir{
    protected static int maxvisable = -1;
    public static void main(String [] args){
	if(args.length > 0){
	    File f = new File(args[0]);
	    try{
		maxvisable = Integer.parseInt(args[1]);
	    }catch(Exception e){
		maxvisable = -1;
	    }
	    if(f.exists()){
		calcSize(f, 0);
	    }else{
		System.out.println(f +" doesn't exist");
	    }
	}else{
	    System.out.println("Usage: java Sumdir <file> [max printed depth]");
	}
    }
    protected static long calcSize(File f, int depth){
	long size = 0;
	print(depth, "Scanning " + f);
	try{
	    f = f.getCanonicalFile();
	    File [] subs = f.listFiles();
	    if (subs != null){
		for(int k = 0; k < subs.length; k++){
		    File cur = subs[k].getCanonicalFile();
		    if(cur.compareTo(f) > 0){ //Get rid of symlinks
			if(cur.isDirectory()){
			    size += calcSize(cur, depth + 1);
			}else{
			    size += cur.length();
			}
		    }
		}
	    }else{
		System.out.println("Error: " + f + " is not a real dir.");
	    }
	}catch(IOException ioe){
	    System.out.println("Error: " + ioe.getMessage());
	}
	print(depth, "The total size of " + f + " is " + size);
	return size;
    }
    protected static StringBuffer padding  = new StringBuffer();
    protected static void print(int len, String text){
	if ((maxvisable < 0) || (len <= maxvisable && maxvisable > -1) ){
	    while(len >= padding.length()){
		padding.append(" ");
	    }
	    System.out.println(padding.substring(0, len) + text);
	}
    }
}




#!/usr/bin/perl -w
if ($#ARGV < 0 ){
    print "Usage psumdir <dir> <level>\n";
    exit 1;
}
$dir=shift @ARGV;
$depth=0;
$maxdepth = -1;
if ($#ARGV >= 0){
    $maxdepth = shift @ARGV;
}
use File::stat;
use Fcntl ':mode';
sub calcSize;
sub myprint {
    do {
	for( local $c = 0; $c < $depth; $c++){ print " ";}
	print @_;
    } unless $depth > $maxdepth && $maxdepth >= 0; 
}
sub calcSize {
    opendir( FILES, $_[0] ) or 
	do { print STDERR "Can't read $_[0]: $!\n"; return 0; };
    myprint "Scanning $_[0]\n";
    $depth++;
    local $size=0;
    local @items = readdir FILES;
    foreach $FILE (@items) {
	local $f="$_[0]/$FILE";
	local $t=stat $f or 
	    do { print STDERR "Can't stat $f: $!\n"; next; };
	if ( S_ISDIR $t->mode) {
	    #Avoid an infinite loop of ././././././
	    $size += calcSize $f unless $FILE =~ /^\.{1,2}$/;
	}else{
	    $size += $t->size;
	}
    }
    $depth--;
    myprint "The total size of $_[0] is $size\n";
    closedir FILES;
    return $size;
}
calcSize $dir;




#!/usr/bin/perl -w
if($#ARGV < 0){
    print "Usage: echo <list-of-files>|psumlist <name>\n";
    print "example: find ./ | grep .avi\$ | psumlist all media files\n";
    exit(1);
}
use File::stat;
$total = 0;
while(<STDIN>){
    #trim trailing newline
    $f = substr($_,0, length()-1);
    $st = stat($f) or do{
	print STDERR "Can't stat $f: $!\n";
	next;
    };
    $total += $st->size;
}
print "The total size of @ARGV is $total\n"



syntax highlighted by Code2HTML,
v. 0.9.
Update 2004/06/01: new and improved psumlist.
Another example: rpm -ql <some-package> |
psumlist <some-package>
Subscribe
  • Post a new comment

    Error

    default userpic
    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.
  • 1 comment