Old-Schcool UNIX Script: Clean House
The hard drives on the Silicone Graphics IRIX systems that I used to administer were always filling up. We had plenty of hard disk space, for the time, but the systems were used in ways that quickly ate up any available space. Some of the workstations were used for running engineering analyses that could generate humongous result files, while others ran CAD applications which left behind many medium-sized temporary files. To keep the systems running it was necessary frequently clean up the drives, and since manually cleaning up disks can be error prone I wrote a script to do the work for me. Since I wrote the script to do some of my work for I had a little free time to add a nifty progress display, which makes the program slightly more complex.
#!/bin/sh
#
# /xyzuser/root/bin/clean-users
#
# Script to clean ALL users home directories
# August 2002, CSTOLL
#
echoprogress () {
if [ -n "$1" ]; then
echo -n "[00;33m"
echo -n "$1"
echo -n "[00m"
fi
}
echodone () {
echo -n "[00;32m"
echo "done"
echo -n "[00m"
}
echofailed () {
echo -n "[00;31m"
echo "failed"
echo -n "[00m"
}
rmfiles () {
if [ -n "$1" ]; then
echoprogress "$2"
if [ -f "$1/$2" ]; then
rm $1/$2
fi
type="$2"
slength=${#type}
if [ "$slength" -eq 1 ]; then
echo -n " "
fi
if [ "$slength" -eq 2 ]; then
echo -n " "
fi
if [ "$slength" -eq 3 ]; then
echo -n " "
fi
if [ "$slength" -eq 4 ]; then
echo -n " "
fi
if [ "$slength" -eq 5 ]; then
echo -n " "
fi
if [ "$slength" -eq 6 ]; then
echo -n " "
fi
if [ "$slength" -eq 7 ]; then
echo -n " "
fi
if [ "$slength" -eq 8 ]; then
echo -n " "
fi
if [ "$slength" -eq 9 ]; then
echo -n " "
fi
if [ "$slength" -eq 10 ]; then
echo -n " "
fi
if [ "$slength" -eq 16 ]; then
echo -n " "
fi
fi
}
rmdircontents () {
if [ -n "$1" ]; then
if [ -e "$1/$2" ]; then
rm -R $1/$2
fi
fi
}
username=`whoami`
if [ "$username" != 'root' ]; then
echo "Sorry $username, this procedure is only for root."
exit 1
fi
thishost=`hostname`
if [ "$thishost" != 'cae1.na.xyz.com' ]; then
echo "This script can only be run on cae1.na.xyz.com, not $thishost."
exit 1
fi
if [ "$username" == 'root' ]; then
echo ""
echo "This procedure will remove all temporary files,"
echo "clear the web cache, empty the dumpster, and"
echo "remove old I-DEAS files **FOR ALL USERS**."
echo ""
echo -n "Continue? (y/n) [n]: "
continue=`line`
if [ "$continue" != 'y' ]; then
exit 1
fi
echo ""
echo "Do you wish to remove I-DEAS .STOP* files?"
echo "This should only be done when you are sure"
echo "that no one is using I-DEAS! It will cause"
echo "a running session of I-DEAS to crash!"
echo ""
echo -n "Remove I-DEAS STOP files? (y/n) [n]: "
continue=`line`
if [ "$continue" = 'y' ]; then
rmideasstop="y"
else
rmideasstop="n"
fi
xyzUUSE1=`df -h | awk '$7=="/xyzuser" {print $5}'`
awk -F: 'tolower($2)!~/closed/ {print $6}' /etc/passwd.nis > /xyzuser/root/clean-home.users
set -A homedirs `cat /xyzuser/root/clean-home.users`
((i=0))
while [ ${homedirs[i]} ]
do
userdir="${homedirs[i]}"
if [ -d "$userdir" ]; then
echo -n "($userdir) Removing temporary files ... "
rmfiles "$userdir" "*.000"
rmfiles "$userdir" "*.BAK"
rmfiles "$userdir" "*.bak"
rmfiles "$userdir" "*.CGM"
rmfiles "$userdir" "*.cgm"
rmfiles "$userdir" "*.HPL"
rmfiles "$userdir" "*.hpl"
rmfiles "$userdir" "*.TMP"
rmfiles "$userdir" "*.tmp"
echodone
echo -n "($userdir) Clearing Web cache ... "
rmdircontents "$userdir" ".netscape/cache/*"
rmdircontents "$userdir" "webcache/*"
echodone
echo -n "($userdir) Emptying dumpster ... "
rmdircontents "$userdir" "dumpster/*"
echodone
if [ "$rmideasstop" = "y" ]; then
echo -n "($userdir) Removing .STOP* files ... "
rmfiles "$userdir" ".STOP*"
echodone
fi
echo -n "($userdir) Removing I-DEAS temporary files ... "
if [ ! -e $userdir/.STOP* ]; then
rmfiles "$userdir" "*.AUX"
rmfiles "$userdir" "*.aux"
rmfiles "$userdir" "*.LCK"
rmfiles "$userdir" "*.lck"
rmfiles "$userdir" "*.OUT"
rmfiles "$userdir" "*.out"
rmfiles "$userdir" "*.PSF"
rmfiles "$userdir" "*.psf"
rmfiles "$userdir" "*.UND"
rmfiles "$userdir" "*.und"
echodone
echo -n "($userdir) Clearing I-DEAS printsubmit ... "
rmdircontents "$userdir" "printsubmit/*"
echodone
else
echofailed
echo "($userdir) [00;31m###################################[00m"
echo "($userdir) [00;31mCan't remove I-DEAS related files.[00m"
echo "($userdir) [00;31mUser may be running I-DEAS, or has[00m"
echo "($userdir) [00;31man old .STOP* file in his directory[00m"
echo "($userdir) [00;31m###################################[00m"
fi
fi
((i=i+1))
done
echo ""
echo -n "Cleaning up temporary files ... "
rmfiles "/xyzuser/root" "clean-home.users"
echodone
echo ""
xyzUUSE2=`df -h | awk '$7=="/xyzuser" {print $5}'`
echo "Available disk space before cleaning: $xyzUUSE1"
echo " now: $xyzUUSE2"
echo ""
fi