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

  1. From mipos3!intelca!oliveb!ames!husc6!necntc!ncoast!allbery Sat Jan 23 19:37:50 PST 1988
  2. Article 260 of comp.sources.misc:
  3. Path: td2cad!mipos3!intelca!oliveb!ames!husc6!necntc!ncoast!allbery
  4. From: athey@COD.NOSC.MIL (The Bit Butcher)
  5. Newsgroups: comp.sources.misc
  6. Subject: v02i011: Can - a good alias for "rm"
  7. Keywords: rm shellscript
  8. Message-ID: <7104@ncoast.UUCP>
  9. Date: 20 Jan 88 01:08:27 GMT
  10. Sender: allbery@ncoast.UUCP
  11. Organization: Naval Ocean Systems Center, San Diego
  12. Lines: 271
  13. Approved: allbery@ncoast.UUCP
  14. X-Archive: comp.sources.misc/8801/11
  15. Comp.sources.misc: Volume 2, Issue 11
  16. Submitted-By: The Bit Butcher <athey@COD.NOSC.MIL>
  17. Archive-Name: can
  18.  
  19. Comp.sources.misc: Volume 2, Issue 11
  20. Submitted-By: The Bit Butcher <athey@COD.NOSC.MIL>
  21. Archive-Name: can
  22.  
  23. This is meant to be a nice way of removing files so
  24. that they are not permanently gone for a week or so.
  25. Basically you should alias "rm" to "can" then everything
  26. that gets canned is put into a directory in your home
  27. called ".trashcan"  If you still don't understand,
  28. just unwrap it with the usual "sh filename"
  29.  
  30. #!/bin/sh
  31. # to extract, remove the header and type "sh filename"
  32. if `test ! -s ./ReadMe`
  33. then
  34. echo "writing ./ReadMe"
  35. cat > ./ReadMe << '\Rogue\Monster\'
  36. How to get CANned and still keep your job!!!
  37.  
  38. Yes, this is what you have all been waiting for or may
  39. already have.  A replacement for the overpowering "rm."
  40. If you are a system manager who is constantly having
  41. to deal with those users who fail to use "rm" correctly
  42. and destroy weeks of work, this just may be the ticket
  43. for your next meal.  If you are a frustrated user of
  44. "rm" simply talk to your system manager and show him
  45. this nifty stuff.  
  46.  
  47. I have done my best to make this a nearly complete package
  48. with man pages and all.  I do hope that you enjoy it.
  49. Now, you may be a bit more of a wizard with shell
  50. scripts so don't laugh too hard at my simpleton
  51. implementation.  In the emptytrash script, you will
  52. need to change it so that it looks for the appropriate
  53. trashcan files in the appropriate directories.
  54. In other words, you need to change it so that the
  55. location of the users directories is searched.
  56.  
  57. I only have a system V to test this on.  But from my vast
  58. (which isn't extreme, but does include sun3.4 and bsd4.3)
  59. this should be fairly mobile from machine to machine.
  60. I tried not to use to many system dependent things.
  61. You must think I am crazy, well ...
  62.  
  63. If you have any complaints or want to tell me that
  64. I am a complete fool, I will not object, just
  65. write me at nosc!athey.
  66.         -the bit butcher
  67.  
  68. \Rogue\Monster\
  69. else
  70.   echo "will not over write ./ReadMe"
  71. fi
  72. if `test ! -s ./can`
  73. then
  74. echo "writing ./can"
  75. cat > ./can << '\Rogue\Monster\'
  76. :
  77. # @(#) can v1.0  Maintain file trash can    Author:  Russ Sage
  78. # installed on MassComp by S. Luse 4/2/87
  79. # Rewritten by The Bit Butcher
  80.  
  81. CAN=$HOME/.trashcan
  82. USAGE="usage: can [lRr] file ..."
  83.  
  84. if [ ! -d $CAN ] 
  85.     then mkdir $CAN
  86. fi
  87.  
  88. set -- `getopt lR:r: $*`
  89.  
  90. if [ $? != 0 ]
  91. then
  92.     echo "$USAGE" >&2
  93.     exit 2
  94. fi
  95. for i in $*
  96. do
  97.     case $i in
  98.     -l)    echo "$CAN"
  99.         ls $CAN
  100.          exit 0;;
  101.     -R) for j in $@
  102.         do
  103.             case $j in
  104.             -*) ;;
  105.             *)    if [ -f $CAN/$j ]
  106.                 then
  107.                     cp $CAN/$j `pwd`
  108.                     echo "Recovered $j"
  109.                 else
  110.                     echo "$CAN/$j Not Found"
  111.                 fi;;
  112.             esac
  113.         done
  114.         exit 0;;
  115.     -r)    for j in $@
  116.         do
  117.             case $j in
  118.             -*)    ;;
  119.             *)    find $j -print | sort -r | while read FILE
  120.                 do
  121.                     if [ ! -d $FILE ]
  122.                     then 
  123.                         touch -c -a $FILE 
  124.                         mv $FILE $CAN
  125.                     else
  126.                         rmdir $FILE
  127.                     fi
  128.                 done
  129.             esac
  130.         done
  131.         exit 0;;
  132.     --) shift; break;;
  133.     esac
  134. done
  135.  
  136. if  [ -f $@ -o -d $@ ]
  137. then
  138.     find $@ -print | sort -r | while read FILE
  139.     do
  140.         if [ ! -d $FILE ]
  141.         then 
  142.             touch -c -a $FILE 
  143.             mv $FILE $CAN
  144.         else
  145.             echo "can: $FILE directory"
  146.         fi
  147.     done
  148. else
  149.     echo "$USAGE" >&2
  150.     exit 2
  151. fi
  152. \Rogue\Monster\
  153. else
  154.   echo "will not over write ./can"
  155. fi
  156. if `test ! -s ./can.1`
  157. then
  158. echo "writing ./can.1"
  159. cat > ./can.1 << '\Rogue\Monster\'
  160. .\" @(#)run.1    10.2 (MASSCOMP) 8/14/86
  161. .RL "local"
  162. .TH CAN 1
  163. .SH NAME
  164. can \- a replacement for rm that is safe
  165. .SH SYNOPSIS
  166. \fB can [ lRr ] <file | directory> ...
  167. .br
  168. .ns
  169. .SH DESCRIPTION
  170. .I Can
  171. is often an alias of
  172. .I rm(1).
  173. .I Can
  174. works similiarly, with the exception of putting things into a directory, in
  175. your home directory, called ".trashcan."
  176. The
  177. .B -l
  178. option will give you a listing of the "$HOME/.trashcan"
  179. directory.
  180. The
  181. .B -r
  182. option works recursively just the same as 
  183. .I rm(1).
  184. The 
  185. .B -R
  186. option will retrieve a file from the "$HOME/.trashcan" without the hassle
  187. of looking for it.  The
  188. .B -R
  189. option copies the file from the "$HOME/.trashcan" directory into the
  190. present working directory.
  191. .sp
  192. The trash gets dumped everyday but only gets rid of things that
  193. are more than a week old.  In other words, you have a week to get something
  194. back after you have
  195. .I can
  196. ned it.
  197. .SH FILES
  198. .TP 2.5i
  199. $HOME/.trashcan
  200. The reservoir of canned files
  201. .SH SEE ALSO
  202. .I
  203. emptytrash(8), rm(1)
  204. .SH BUGS
  205. The 
  206. .B -R
  207. option does not work on wild cards.  You have to know the
  208. exact name of a file in order to recover it with this command.
  209. .sp
  210. If further bugs are found please report them.
  211. .SH AUTHOR
  212. The Bit Butcher
  213. .br
  214. Inspired by the original
  215. .B can
  216. made available by Steph Luse, which he pirated from COD, which was written
  217. by a Russ Sage.
  218. \Rogue\Monster\
  219. else
  220.   echo "will not over write ./can.1"
  221. fi
  222. if `test ! -s ./emptytrash`
  223. then
  224. echo "writing ./emptytrash"
  225. cat > ./emptytrash << '\Rogue\Monster\'
  226. : /bin/sh
  227.  
  228. # EMPTYTRASH 
  229. # Executed from root crontab file every night.
  230. # It finds all files in all users .traschan directories and gets
  231. # rid of any file that has not been accessed or modified for more
  232. # than 7 days.  Note:  this works in conjunction with can.  can
  233. # changes modifies the access time for a file when it moves the  
  234. # file to the user's .trashcan diretory.
  235.  
  236. find /cd441/*/.trashcan -atime +7 -print | while read FILE
  237. do 
  238. #       echo $FILE
  239.     rm $FILE
  240. done
  241. \Rogue\Monster\
  242. else
  243.   echo "will not over write ./emptytrash"
  244. fi
  245. if `test ! -s ./emptytrash.8`
  246. then
  247. echo "writing ./emptytrash.8"
  248. cat > ./emptytrash.8 << '\Rogue\Monster\'
  249. .\" @(#)run.1    10.2 (MASSCOMP) 8/14/86
  250. .RL "local"
  251. .TH EMPTYTRASH 8
  252. .SH NAME
  253. emptytrash \- the trash collector used with
  254. .I can
  255. .SH SYNOPSIS
  256. \fBemptytrash
  257. .br
  258. .ns
  259. .SH DESCRIPTION
  260. .I Emptytrash
  261. simply looks into each $HOME/.trashcan and checks the last access time
  262. which is usually set by
  263. .I can(1).
  264. Anything that it finds that is more than seven days old it permanently
  265. removes via 
  266. .I rm(1).
  267. This amount of time can be changed by changing the "7" in the script file.
  268. The best thing to do is to put this in your root crontab file and have it
  269. executed everyday.
  270. .SH FILES
  271. .TP 2.5i
  272. $HOME/.trashcan
  273. The reservoir of canned files
  274. .SH SEE ALSO
  275. .I
  276. can(1), rm(1)
  277. .SH BUGS
  278. Only that this was a quicky and could be made better by having it accept an
  279. argument that would determine the amount of elapsed time to check for.
  280. .SH AUTHOR
  281. The Bit Butcher
  282. .br
  283. With the help of Steph Luse.
  284. \Rogue\Monster\
  285. else
  286.   echo "will not over write ./emptytrash.8"
  287. fi
  288. echo "Finished archive 1 of 1"
  289. exit
  290.  
  291.  
  292.