cleanup script needed

Dan Siler dans at hplsla.HP.COM
Fri May 12 03:14:47 AEST 1989


This is a script that does various types of file cleanup. Be sure to
carefully review it before firing it up! I have cron launch this once a week.

 _________________________________________________________________________
|                                                                         |
| Dan Siler          unix: dans%hplsla at hplabs.hp.com hplabs!hplsla!dans   |
| Hewlett Packard                           hpdesk: DAN SILER/HPA100/15   |
| Lake Stevens Instrument Division, ms:380           at&t: (206) 335-2178 |
| 8600 Soper Hill Road;  Everett, WA 98205-1298        telnet: 1-335-2178 |
|_________________________________________________________________________|


#!/bin/sh
#---------------------------------
#  CONFIGURE THESE
# 
#  variable="true" implies true or yes--do this action
#  Anything else (i.e. "false" implies false, or no--don't do this action)

rm_catpages="true"   # rm nroffed cat pages (only if the un-nroffed page exists)
rm_junkfiles="false" # rm junk files listed in the variable $junkfiles
mailroot="true"      # Mail a summary of what was truncated/cleaned-up
script="$0"                            # Name of this script
subject="`hostname` `basename $0` ran" # Subject for mailing
to="root"                              # Who to mail to

files="
/etc/ptydaemonlog
/etc/vtdaemonlog
/usr/adm/inetd.log
/usr/adm/neterrlog
/usr/adm/nfslog
"

nochangefile="
/etc/wtmp
/etc/btmp
"

# Note: Any files matching these names will be removed from the system.
#       A very dangerous command.
junkfiles="
core
a.out
dead.letter
"
#
#---------------------------------

{
echo "Execution trace of $script..."
echo "1) assign mailroot variable to 'false' if this mail message is not desired."
echo "2) assign rm_catpages variable 'true' if removal of cat pages is desired."
echo "3) assign rm_junkfiles variable 'true' if removal of junk files is desired."
echo ""
for i in $files
do
  if [ -f "${i}" ]
  then
    echo "truncating $i..."
    echo "  - new is tail of old, old copy in ${i}.old"
    cp ${i} ${i}.old
  fi
  tail ${i}.old >${i}
  echo "" >> ${i}
  echo Log file beginning on `date "+%D at %r"` >> ${i}
  echo "" >> ${i}
  chmod 440 ${i}.old
done


if [ -f "/usr/adm/sulog" ]
then
  echo "truncating /usr/adm/sulog..."
  echo "  - new is tail of old, old copy in /usr/adm/OLDsulog"
  cp /usr/adm/sulog /usr/adm/OLDsulog
fi
tail /usr/adm/OLDsulog >/usr/adm/sulog
echo "" >> /usr/adm/sulog
echo Log file beginning on `date "+%D at %r"` >> /usr/adm/sulog
echo "" >> /usr/adm/sulog
chmod 440 /usr/adm/OLDsulog

if [ -f "/usr/lib/cron/log" ]
then
  echo "truncating /usr/lib/cron/log..."
  echo "  - new is tail of old, old copy in /usr/lib/cron/OLDlog"
  cp /usr/lib/cron/log /usr/lib/cron/OLDlog
fi
tail /usr/lib/cron/OLDlog >/usr/lib/cron/log
echo "" >> /usr/lib/cron/log
echo Log file beginning on `date "+%D at %r"` >> /usr/lib/cron/log
echo "" >> /usr/lib/cron/log
chmod 440 /usr/lib/cron/OLDlog

for i in $nochangefile
do
  if [ -f "${i}" ]
  then
    echo "truncating $i..."
    echo "  - old copy in $i.old"
    mv ${i} ${i}.old
  fi
  touch $i
  chmod 440 $i
done

if [ "${rm_catpages}" = "true" ]
then
  # remove old manual pages from /usr/man/cat1
  # (There only needs to be the nroff source file from /usr/man/man1)
  # files older than 14 days are removed.
  echo "looking for formatted manual pages..."
  manmandirs=`echo /usr/man/man* /usr/contrib/man/man* /usr/local/man/man*`
  dirlist=""
  for dir in ${manmandirs}
  do
    if [ -d ${dir} ]
    then
      dirlist="${dirlist}${dir} "
    fi
  done

  for dir in /usr/man /usr/contrib/man /usr/local/man
  do
    if [ -d "${dir}" ]
    then
      for i in 1 1m 2 3 4 5 6 7 8 9
      do
        if [ -d "${dir}/cat${i}" ]
        then
          cd "${dir}/cat${i}"
          for catpage in `find . -ctime +14 -print`
          do
            if [ -f "${dir}/man${i}/${catpage}" ]
            then
              echo "removing ${dir}/cat${i}/${catpage}..."
              rm ${dir}/cat${i}/${catpage}
            fi
          done
        fi
      done
    fi
  done
fi

# remove junk files

if [ "${rm_junkfiles}" = "true" ]
then
  for i in $junkfiles
  do
    echo "removing any $i junk files..."
    find / -name $i -exec rm {} \;
  done
fi 
} | if [ "${mailroot}" = "true" ]
    then
      mailx -s "${subject}" ${to}
    else
      while read line
      do
        echo "$line"
      done
    fi



More information about the Comp.unix.questions mailing list