Newtun
Storage is nice, especially if it doesn't rotate
Greetings, all.
"At work", I recently got an Email that a 1 TB file system was 96% full on a UNIX server, and I had one of the largest 10 files. But it was way under 1% of the total space.
It occurred to me that instead of the system admins reporting a few real big files, it might be better for them to report directories that use a lot of space (though individual files therein may not be top-10).
So I started writing a script to try to do that, and yesterday, it found one directory that was using about 6% of the disk. That directory's owner needs to do some cleanup, IMHO.
Anyway, the (korn-shell) script takes a file-name parameter (defaulting to the current directory), finds the file-system directory it's in, gets the files there (which should be immediate subdirectories for the most part), computes their total space usage, sorts by that, and reports the 20 biggest "space hogs". I do some checking to see that the subdirectories are in the same file system and that their total space computation only includes files in that file system (using the -x option of du).
If anyone has suggestions about how to improve the script, please let me know (the seds just add thousands-delimiting commas):
"At work", I recently got an Email that a 1 TB file system was 96% full on a UNIX server, and I had one of the largest 10 files. But it was way under 1% of the total space.
It occurred to me that instead of the system admins reporting a few real big files, it might be better for them to report directories that use a lot of space (though individual files therein may not be top-10).
So I started writing a script to try to do that, and yesterday, it found one directory that was using about 6% of the disk. That directory's owner needs to do some cleanup, IMHO.
Anyway, the (korn-shell) script takes a file-name parameter (defaulting to the current directory), finds the file-system directory it's in, gets the files there (which should be immediate subdirectories for the most part), computes their total space usage, sorts by that, and reports the 20 biggest "space hogs". I do some checking to see that the subdirectories are in the same file system and that their total space computation only includes files in that file system (using the -x option of du).
If anyone has suggestions about how to improve the script, please let me know (the seds just add thousands-delimiting commas):
Code:
if test $# -eq 0
then DIR=$PWD
else DIR=$1
fi
df -k $DIR | sed 's/ *(/ (/' | sed -e :a -e 's/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/;ta'
df -k $DIR|head -1|sed 's/ *)/)/'|read MNTPNT DEV COLON KBTOTALLOC REST
cd $MNTPNT
echo "Top 20 Space Report for $MNTPNT: KB used by subdirectory"
echo " KB Disk Pct of subdir owner Group Owner"
echo " Space Tot Alloc name ID ID Name"
for DIR in *
do
if test -z "`grep \" $MNTPNT/$DIR \" /etc/mnttab`"
then #### SAME FILE SYSTEM / MOUNT POINT !!!!
du -xks $DIR 2>&-
fi
done | sort -rn |
while read SPACE DIR
do
ls -dl $DIR | read PERMS DUMMY OWNER GROUP REST
NAME=`grep "^$OWNER:" /etc/passwd | cut -d: -f5 | cut -d "," -f1`
PCT=`echo 2 k $SPACE $KBTOTALLOC 100 / / .005 + p | dc`
SPACEY=`echo $SPACE | sed -e :a -e 's/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/;ta'`
printf "%12s%9.2f%% %-12s%-12s%-12s%-s\n" $SPACEY $PCT $DIR $OWNER $GROUP "$NAME"
done | head -20