home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / compsrcs / misc / volume07 / scavenge.ksh < prev    next >
Encoding:
Internet Message Format  |  1991-08-27  |  8.8 KB

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