#!/usr/bin/python # -*- coding: iso-8859-1 -*- # Calculate the diskusage by counting the filesizes. # The GNU du command can not read the filenames from stdin. # That's why I wrote this in Python. # (c) March 2007 Thomas Guettler http://www.thomas-guettler.de # This script is in the public domain # Python Imports import os import sys import getopt def usage(): print """Usage: %s [-a] [ - | file1 file2 ...] Calculate diskuage. This script count the size of the files. The size on disk can be greater (if you have many small files). -a: Print size for every file. Not only at the end. Example: find ... -type f | %s -a | sort -rn | head """ % ( os.path.basename(sys.argv[0]), os.path.basename(sys.argv[0]) ) def main(): try: opts, args = getopt.getopt(sys.argv[1:], "a", []) except getopt.GetoptError, e: print e usage() sys.exit(2) verbose=False for o, a in opts: if o=="-a": verbose=True else: assert False, "Unhandled: %s %s" % (o, a) if not args: usage() sys.exit(2) if args[0]=="-": filegetter=sys.stdin else: filegetter=args size=0 filecount=0 for file in filegetter: file=file.rstrip() assert os.path.isfile(file), "Not a file: %s" % file mysize=os.path.getsize(file) size+=mysize filecount+=1 if verbose: print "%d %s" % (mysize, file) print "%s bytes %s files" % ( size, filecount) if __name__=="__main__": main()