OK after a fiddle, it supports lots of juicy stuff.
I've split the awk script into a separate file, so gedit colors it nicely.
Rename the awk script dsize_awk. Or, you may want to recombine them (here's why: http://wooledge.org:8000/BashFAQ/028 Notice my fugly workaround: AWKFILE="/usr/bin/dsize_awk"
Enjoy ... & send me a thanks if it helps you (boost my UF karma
)
Love n light
Sam
Code:
#!/bin/bash
# THANKS TO:
# irc.freenode.net#linux
# irc.freenode.net#awk
# http://wooledge.org:8000/BashFAQ/035 for handling commandline args
# ${DIR%/}/ explained at: http://wooledge.org:8000/BashFAQ/073
AWKFILE="/usr/bin/dsize_awk"
RECURSE="NO"
MINSIZE="512K"
LINES="20"
show_help()
{
echo 'dsizer - Get Directory Size (Recursable)'
echo ' Tool for showing sizes of contained directories'
echo ' Useful for flushing out the garbage when your HDD is hitting 99%'
echo ' First bash script by spud (yay) sunfish7@gmail.com Dec 08'
echo ''
echo 'USAGE: dsizer [OPTION]... [PATH]'
echo 'OPTIONS:'
echo ' -h -? --help for help'
echo ' -r --recurse to recurse. Default:'$RECURSE
echo ' -n --lines x to show only x lines (use -0 to show all). Default:'$LINES
echo ' -m --minsize 4K only reports dirs > 4K. Can use 6.7g 800M etc. Default:'$MINSIZE
}
while [[ $1 == -* ]]; do
case "$1" in
-h|--help|-\?) show_help; exit 0;;
-r|--recurse) RECURSE="YES"; shift;;
-m|--minsize) MINSIZE=$2; shift 2;;
-n|--lines) LINES=$2; shift 2;;
-*) echo "invalid option: $1"; show_help; exit 1;;
esac
done
# if DIR was specified, make sure it finishes with a / (add one if necessary)
DIR=$1
if [[ "$DIR" ]]
then DIR=${DIR%/}/
fi
if [[ "$RECURSE" == "YES" ]]
then DU_PARAMS='-c '$DIR'*/'
else DU_PARAMS='-s '$DIR'*/ '$DIR'.'
fi
# ensures * reports dir's beginning with .
shopt -s dotglob
echo 'whirr...'
du $DU_PARAMS $DIR \
| sort -rn \
| awk -v minsize="$MINSIZE" -f $AWKFILE \
| head -n $LINES
exit 0
and dsize_awk:
Code:
#!/usr/bin/awk -f
# - Typical line from du might be
# 14816 chords/
#
# - The awk script replaces eg 4784 with 4K, and filters
# lines where the bytecount < $MINSIZE
#
# - Run this script with '-v minsize="$MINSIZE"' where $MINSIZE=4K, 3.7g, etc
#
# - Script based on:
# http://awk.freeshell.org/FormatFileSizes
BEGIN {
u[0]="K"
u[1]="M"
u[2]="G"
}
{
# Get filesize and filename from paramlist
# cannot use $1 and $2 as filename may contain spaces
size=$1 # grab the number
sub(/^[^\t]+\t+/, "") # Remove it from param list, and following spaces
dirname=$0 # what is left is the name
minK = Convert_xK_to_x(minsize)
if (size < minK)
exit
for (i=3; i>=0; --i)
if (size > 1024^i)
{
# can sub %d with eg %.2f instead for 2dp precision
printf "%d%s \t %s \n", (size / 1024^i), u[i], dirname
next
}
}
# takes eg 4K or 6.5m and outputs 4, or w/e 6.5Megs is in k
function Convert_xK_to_x(x)
{
s_in = x
letter = substr(s_in, length(s_in), 1)
num = s_in
sub("K", "", num)
for (i=0; i<=2; i++)
if (toupper(letter)==u[i])
num*=1024^i
s_out = num
return s_out
}
Bookmarks