| 1 | #!/bin/bash |
| 2 | # tree2.sh |
| 3 | |
| 4 | # Lightly modified/reformatted by ABS Guide author. |
| 5 | # Included in ABS Guide with permission of script author (thanks!). |
| 6 | |
| 7 | ## Recursive file/dirsize checking script, by Patsie |
| 8 | ## |
| 9 | ## This script builds a list of files/directories and their size (du -akx) |
| 10 | ## and processes this list to a human readable tree shape |
| 11 | ## The 'du -akx' is only as good as the permissions the owner has. |
| 12 | ## So preferably run as root* to get the best results, or use only on |
| 13 | ## directories for which you have read permissions. Anything you can't |
| 14 | ## read is not in the list. |
| 15 | |
| 16 | #* ABS Guide author advises caution when running scripts as root! |
| 17 | |
| 18 | |
| 19 | ########## THIS IS CONFIGURABLE ########## |
| 20 | |
| 21 | TOP=5 # Top 5 biggest (sub)directories. |
| 22 | MAXRECURS=5 # Max 5 subdirectories/recursions deep. |
| 23 | E_BL=80 # Blank line already returned. |
| 24 | E_DIR=81 # Directory not specified. |
| 25 | |
| 26 | |
| 27 | ########## DON'T CHANGE ANYTHING BELOW THIS LINE ########## |
| 28 | |
| 29 | PID=$$ # Our own process ID. |
| 30 | SELF=`basename $0` # Our own program name. |
| 31 | TMP="/tmp/${SELF}.${PID}.tmp" # Temporary 'du' result. |
| 32 | |
| 33 | # Convert number to dotted thousand. |
| 34 | function dot { echo " $*" | |
| 35 | sed -e :a -e 's/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/;ta' | |
| 36 | tail -c 12; } |
| 37 | |
| 38 | # Usage: tree <recursion> <indent prefix> <min size> <directory> |
| 39 | function tree { |
| 40 | recurs="$1" # How deep nested are we? |
| 41 | prefix="$2" # What do we display before file/dirname? |
| 42 | minsize="$3" # What is the minumum file/dirsize? |
| 43 | dirname="$4" # Which directory are we checking? |
| 44 | |
| 45 | # Get ($TOP) biggest subdirs/subfiles from TMP file. |
| 46 | LIST=`egrep "[[:space:]]${dirname}/[^/]*$" "$TMP" | |
| 47 | awk '{if($1>'$minsize') print;}' | sort -nr | head -$TOP` |
| 48 | [ -z "$LIST" ] && return # Empty list, then go back. |
| 49 | |
| 50 | cnt=0 |
| 51 | num=`echo "$LIST" | wc -l` # How many entries in the list. |
| 52 | |
| 53 | ## Main loop |
| 54 | echo "$LIST" | while read size name; do |
| 55 | ((cnt+=1)) # Count entry number. |
| 56 | bname=`basename "$name"` # We only need a basename of the entry. |
| 57 | [ -d "$name" ] && bname="$bname/" |
| 58 | # If it's a directory, append a slash. |
| 59 | echo "`dot $size`$prefix +-$bname" |
| 60 | # Display the result. |
| 61 | # Call ourself recursively if it's a directory |
| 62 | #+ and we're not nested too deep ($MAXRECURS). |
| 63 | # The recursion goes up: $((recurs+1)) |
| 64 | # The prefix gets a space if it's the last entry, |
| 65 | #+ or a pipe if there are more entries. |
| 66 | # The minimum file/dirsize becomes |
| 67 | #+ a tenth of his parent: $((size/10)). |
| 68 | # Last argument is the full directory name to check. |
| 69 | if [ -d "$name" -a $recurs -lt $MAXRECURS ]; then |
| 70 | [ $cnt -lt $num ] \ |
| 71 | || (tree $((recurs+1)) "$prefix " $((size/10)) "$name") \ |
| 72 | && (tree $((recurs+1)) "$prefix |" $((size/10)) "$name") |
| 73 | fi |
| 74 | done |
| 75 | |
| 76 | [ $? -eq 0 ] && echo " $prefix" |
| 77 | # Every time we jump back add a 'blank' line. |
| 78 | return $E_BL |
| 79 | # We return 80 to tell we added a blank line already. |
| 80 | } |
| 81 | |
| 82 | ### ### |
| 83 | ### main program ### |
| 84 | ### ### |
| 85 | |
| 86 | rootdir="$@" |
| 87 | [ -d "$rootdir" ] || |
| 88 | { echo "$SELF: Usage: $SELF <directory>" >&2; exit $E_DIR; } |
| 89 | # We should be called with a directory name. |
| 90 | |
| 91 | echo "Building inventory list, please wait ..." |
| 92 | # Show "please wait" message. |
| 93 | du -akx "$rootdir" 1>"$TMP" 2>/dev/null |
| 94 | # Build a temporary list of all files/dirs and their size. |
| 95 | size=`tail -1 "$TMP" | awk '{print $1}'` |
| 96 | # What is our rootdirectory's size? |
| 97 | echo "`dot $size` $rootdir" |
| 98 | # Display rootdirectory's entry. |
| 99 | tree 0 "" 0 "$rootdir" |
| 100 | # Display the tree below our rootdirectory. |
| 101 | |
| 102 | rm "$TMP" 2>/dev/null |
| 103 | # Clean up TMP file. |
| 104 | |
| 105 | exit $? |