#!/bin/sh # the next line restarts using tclsh \ exec tclsh "$0" "$@" # Tree Size Watcher. When run with a directory as argument, produces # output indicating the growth of files and subtrees in the directory # compared to the previous run. Creates a .treesize file in the directory # to store previous results when called with the -r/--remember option. # 2006-07-09 JH More positions for growth percentage. # 2003-08-01 JH Changed the -noupdate option to -r/--remember. # 2003-05-13 JH Initial version. ##### main ############################################################### if {([lindex $argv 0]=="--remember") || ([lindex $argv 0]=="-r")} { # Write the .treesize results file. set remember 1 set argv [lindex $argv 1] } else { set remember 0 } if {[llength $argv]==0} { puts "Usage: treesize \[-r/--remember\] /full/directory/path" puts " -r/--remember creates a .treesize file in the given dir." exit } # Work here. cd [lindex $argv 0] # Load previous results, if any. array set oldSize {} if {[file exists ".treesize"]} { set oldDate [file mtime ".treesize"] set c [open ".treesize" r] array set oldSize [gets $c] close $c } # Get current tree sizes and process them. array set newSize {} set du [eval exec du -s [glob *]] set first 1 foreach {size tree} $du { if {[info exists oldSize($tree)]} { # Known tree. Growth exceeds 1%? if {$oldSize($tree)==0} { set growth 0 } else { set growth [expr ($size.0-$oldSize($tree).0)/$oldSize($tree).0*100] } if {$growth>1} { # First match? Then announce the date of the previous size etc. if {$first==1} { puts "" puts "-----------------------------------------------------------" puts "Results from [lindex $argv 0]" puts "Previous sizes from [clock format $oldDate]" puts "Current sizes from [clock format [clock seconds]]" puts "" puts " tree current prev growth" puts "-----------------------------------------------------------" set first 0 } puts [format "%30s %8d %8d %5.0f%%" \ $tree $size $oldSize($tree) $growth] } # Erase the old tree size for later reference to dropped trees. unset oldSize($tree) } else { # Unknown tree, assume new. puts "[format "%30s %8d NEW" $tree $size]" } # Append to cache file. lappend cache $tree $size } # If enabled, write cache file. if {$remember==1} { set c [open ".treesize" w] puts $c $cache close $c } # Signal trees that have disappeared. foreach tree [array names oldSize] { puts [format "%30s DELETED" $tree] }