home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume7 / scavenge.ksh < prev    next >
Encoding:
Text File  |  1989-06-03  |  8.4 KB  |  279 lines

  1. Newsgroups: comp.sources.misc
  2. Subject: v07i014: scavenge.ksh: clean up old files in /tmp (MKS)
  3. From: allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc)
  4. Reply-To: naz@hslrswi.UUCP (Norman H. Azadian)
  5.  
  6. Posting-number: Volume 7, Issue 14
  7. Submitted-by: naz@hslrswi.UUCP (Norman H. Azadian)
  8. Archive-name: scavenge.ksh
  9.  
  10. Here is a tool I use on my PC running under the MKS Toolkit.
  11. This shell script could presumably be adapted quite simply to run
  12. under any unix, although the need for it would be less on a real unix.
  13.  
  14. [You think.  Of course, most people on the Usenet are intelligent enough
  15. not to install applications in /tmp; the people *I* have to hand-hold aren't.
  16. Cleaning up /tmp is a real project on *their* systems.  ++bsa]
  17.  
  18. Being the cautious type, I generally put unwanted files in /tmp.  Later,
  19. when I need disk space, I run this tool to actually delete the oldest
  20. of those files.  It also knows enough to look in other places where I
  21. keep old, expendable, files.
  22.  
  23. As a cautious type, I've built in all manner of safeguards and options
  24. to allow me to see what's going to be deleted, and to give me a veto.
  25. The basic operation of the tool, however, is quite simple.  When
  26. invoked with no argument, it reports the number of KBytes which
  27. could be recovered by deleting old files.  When invoked with a number,
  28. it will delete just enough old files to increase the available disk
  29. space by that many KBytes.
  30.  
  31. The -L option allows you to review the list of termination candidates,
  32. using the pager named in your PAGER environment variable.  Failing
  33. such a variable, it will use a default.  I currently have the default
  34. set to my own hacked version of less.  A more reasonable choice for
  35. most people would be the pg.exe program that comes with the toolkit.
  36.  
  37. BUG NOTE:  It is supposed to delete the files in chronological order.
  38. In my version 2.2d of the Toolkit, the -t option of ls does not
  39. work correctly, and so files are not deleted in chronological order.
  40. If you have a newer version, it might work for you.
  41.  
  42. #! /bin/sh
  43. # This is a shell archive, meaning:
  44. # 1. Remove everything above the #! /bin/sh line.
  45. # 2. Save the resulting text in a file.
  46. # 3. Execute the file with /bin/sh (not csh) to create the files:
  47. #    scavenge.ksh
  48. # This archive created: Thu May 25 14:28:15 1989
  49. # By:    Norman H. Azadian (Hasler AG)
  50. export PATH; PATH=/bin:$PATH
  51. echo shar: extracting "'scavenge.ksh'" '(5625 characters)'
  52. if test -f 'scavenge.ksh'
  53. then
  54.     echo shar: will not over-write existing file "'scavenge.ksh'"
  55. else
  56. cat << \SHAR_EOF > 'scavenge.ksh'
  57. #    /usr/bin/scavenge.ksh    890523    NHA
  58. # With no parameters, simply reports the number of KBytes in expendable files.
  59. # With 1 parameter, prepares a list of temporary files to delete such that
  60. #  that number of KBytes will be freed up on the disk.
  61. # The -Fspec option allows the user to prepend filespecs to fileSpec.
  62. # The -I option makes the selection process Interactive.
  63. # The -L option shows the list of files that can/will be deleted.
  64. # The -V option makes all output verbose.
  65. # Exit status is always 0 for success, otherwise 1.
  66. # At present only the last filespec suggested by the user will be processed.
  67.  
  68. # Files are selected for deletion in line with the following priorities:
  69. #   1) minimize the number of directories (filespecs) affected.
  70. #   2) maximize the age of the files deleted.
  71. ##NOTE THAT IN VERSION 2.2D OF THE TOOLKIT, THE -T OPTION OF LS IS BROKEN.
  72. ##THEREFORE THE ORDER OF DELETION MIGHT NOT BE OPTIMAL.
  73.  
  74.  
  75. # User-definable constants
  76. ##typeset defaultPager='pg'
  77. ##typeset defaultPrompt='-p__h_for_help,___q_to_continue_______________Page_%d'
  78. typeset defaultPager='less -aVh_for_help,___v_to_edit,___q_to_continue'
  79. typeset defaultPrompt='-aVh_for_help,___v_to_edit,___q_to_continue'
  80. #filespecs must be put in an array, one spec per array element.
  81. typeset fileSpec
  82. fileSpec[1]='/tmp/*'
  83. fileSpec[2]='/file[0-9][0-9][0-9][0-9].chk'
  84. fileSpec[3]='/usr/obsolete/*'
  85.  
  86.  
  87. # Internal constants and variables
  88. typeset -i blocksPerKByte=2                    #2 blocks per KByte
  89. typeset -i Vopt=0 Iopt=0 Lopt=0                # -V and -I and -L option flags
  90. typeset masterList=/tmp/__scav__.tmp        #master list of gleanable files
  91. typeset tmpFile=/tmp/__temp__.tmp            #temporary file
  92. typeset usrFileSpec=" "                        #user filespec gets stashed here
  93.  
  94.  
  95. #parse command line
  96. typeset -i requested=0
  97. for arg
  98. do
  99.     case $arg in
  100.         -[iI])    Iopt=1
  101.                 ;;
  102.         -[vV])    Vopt=1
  103.                 ;;
  104.         -[fF]*)    usrFileSpec=${arg#-[fF]}
  105.                 ;;
  106.         -[lL]*)    Lopt=1
  107.                 ;;
  108.         [0-9]*)    requested=$arg
  109.                 ;;
  110.         *)        cat <<-\FOOBIE_BLETCH
  111.                 scavenge -- free up disk space by deleting temporary files
  112.                 usage:  scavenge  [-Fspec]  [-I]  [-L]  [-V]   [kbytes]
  113.                 -Fspec    spec is pre-pended to list of expendable filespecs
  114.                 -I      Interactive file selection
  115.                 -L      List of eligible files is shown
  116.                 -V      Verbose outputs
  117.                 kbytes  number of KBytes to recover.  default 0
  118.  
  119.                 If  kbytes  is not given, nothing will be deleted.
  120.                 FOOBIE_BLETCH
  121.                 exit 1
  122.                 ;;
  123.     esac
  124. done
  125. ##echo "Iopt=$Iopt   Lopt=$Lopt   Vopt=$Vopt   requested=$requested"
  126.  
  127.  
  128. #initialize master listing file
  129. if test   $Lopt -ne 0   -a   $Vopt -ne 0
  130. then
  131.     echo 'BLKS PERMISSION LNK BYTES  DATE  TIME  FILENAME'  >$masterList
  132.     echo '==== ========== == ====== ====== ===== ========================================' >>$masterList
  133. else
  134.     rm  -f  $masterList
  135. fi
  136.  
  137.  
  138. #Draw up the master list of candidates.
  139. #    "If one day it should happen that a victim must be found,"
  140. #    "I've got a little list..."     -- Lord High Executioner from "The Mikado"
  141. # first the user-suggested filespec
  142. if test -r  `echo $usrFileSpec | cut -d" " -f1`
  143. then
  144.     #build sorted list  |  remove "total" line  |  remove directories
  145.     ls.exe -trlsd $usrFileSpec  |  egrep -v '^total [0-9]+'  | \
  146.                                         egrep '^[\ 0-9]+\ \-'  >>$masterList
  147. fi
  148. # then all the built-in filespecs
  149. typeset -i i=1
  150. while (( i < ${#fileSpec[*]} ))
  151. do
  152.     if test -r  `echo ${fileSpec[i]} | cut -d" " -f1`
  153.     then
  154.         #build sorted list  |  remove "total" line  |  remove directories
  155.         ls.exe -trlsd ${fileSpec[i]}  |  egrep -v '^total [0-9]+'  |  \
  156.                                         egrep '^[\ 0-9]+\ \-'  >>$masterList
  157.     fi
  158.     let i=i+1
  159. done
  160.  
  161.  
  162. #Handle the -L option
  163. if test $Lopt -ne 0
  164. then
  165.     if test -z "$PAGER"
  166.     then
  167.         $defaultPager  $defaultPrompt   $masterList
  168.     else
  169.         $PAGER  $masterList
  170.     fi
  171.     if test $Vopt -ne 0
  172.     then                                        #get rid of header lines
  173.         tail +2 $masterList  >$tmpFile
  174.         mv  $tmpFile  $masterList
  175.     fi
  176. fi
  177.     
  178.  
  179. typeset blocks permissions links       month     timeYear name reply
  180. typeset -R8 bytes
  181. typeset -R2 day
  182. typeset -i neededBlocks neededKB
  183.  
  184.  
  185. #Handle case where only a report is wanted.
  186. if ((requested == 0))
  187. then
  188.     while read -r blocks permissions links bytes month day timeYear name
  189.     do
  190.         ((neededBlocks = neededBlocks + blocks))
  191.     done <$masterList
  192.     ((neededKB = neededBlocks / blocksPerKByte))
  193.     if ((Vopt == 0))
  194.     then
  195.         echo $neededKB
  196.     else
  197.         echo "$neededKB KBytes are eligible for scavenging."
  198.     fi
  199.     exit 0
  200. fi
  201.  
  202.  
  203. #bail out if there's nothing available
  204. if test ! -s $masterList
  205. then
  206.     if test $Vopt -ne 0
  207.     then
  208.         echo "scavenge: no files to scavenge."
  209.     fi
  210.     exit 1
  211. fi
  212.  
  213.  
  214. #Now for the delicate task of deciding who gets the axe.
  215. neededBlocks=`expr $requested '*' $blocksPerKByte`
  216. while test $neededBlocks -gt 0
  217. do
  218.     while read -r blocks permissions links bytes month day timeYear name
  219.     do
  220.         if test $Iopt -ne 0
  221.         then
  222.             if test $Vopt -ne 0
  223.             then
  224.                 echo "Delete $bytes $month $day $timeYear $name\t[Nyq] ? \c"
  225.             else
  226.                 echo "$name\t[Nyq] ? \c"
  227.             fi
  228.             reply=`line` </dev/con
  229.             reply=${reply:=No}                #empty line means "No"
  230.         else
  231.             reply=Yes
  232.         fi
  233.         if test -z "${reply##[qQ]*}"
  234.         then
  235.             break;                            #bail out
  236.         fi
  237.         if test -z "${reply##[yY]*}"
  238.         then
  239.             ((neededBlocks = neededBlocks - blocks))
  240.             if test $Vopt -ne 0
  241.             then
  242.                 let 'neededKB = neededBlocks / blocksPerKByte'
  243.                 echo "deleting $name -- $neededKB KBytes to go"
  244.             fi
  245.             rm $name
  246.             if test $neededBlocks -le 0
  247.             then
  248.                 break
  249.             fi
  250.         fi
  251.     done <$masterList
  252.  
  253.     if test $Vopt -ne 0   -a   $neededBlocks -gt 0
  254.     then
  255.         neededKB=`expr $neededBlocks '/' $blocksPerKByte`
  256.         echo "scavenge: still need to eliminate $neededKB KBytes"
  257.         echo "scavenge: hit carriage return to continue, ^C to quit"
  258.         line
  259.     else
  260.         exit 0
  261.     fi
  262. done
  263. SHAR_EOF
  264. if test 5625 -ne "`wc -c < 'scavenge.ksh'`"
  265. then
  266.     echo shar: error transmitting "'scavenge.ksh'" '(should have been 5625 characters)'
  267. fi
  268. fi # end of overwriting check
  269. #    End of shell archive
  270. exit 0
  271.  
  272. NHA
  273. ===
  274. PAPER:  Norman Azadian; Hasler AG; Belpstrasse 23; 3000 Berne 14; Switzerland
  275. X.400:  naz@hslrswi.hasler
  276. UUCP:   ...{uunet,ukc,mcvax,...}!cernvax!hslrswi!azadian
  277. VOICE:  +41 31 63 2178            BITNET: naz%hslrswi.UUCP@cernvax.BITNET
  278.  
  279.