[comp.sources.misc] v02i011: Can - a good alias for "rm"

athey@COD.NOSC.MIL (The Bit Butcher) (01/20/88)

Comp.sources.misc: Volume 2, Issue 11
Submitted-By: The Bit Butcher <athey@COD.NOSC.MIL>
Archive-Name: can

This is meant to be a nice way of removing files so
that they are not permanently gone for a week or so.
Basically you should alias "rm" to "can" then everything
that gets canned is put into a directory in your home
called ".trashcan"  If you still don't understand,
just unwrap it with the usual "sh filename"

#!/bin/sh
# to extract, remove the header and type "sh filename"
if `test ! -s ./ReadMe`
then
echo "writing ./ReadMe"
cat > ./ReadMe << '\Rogue\Monster\'
How to get CANned and still keep your job!!!

Yes, this is what you have all been waiting for or may
already have.  A replacement for the overpowering "rm."
If you are a system manager who is constantly having
to deal with those users who fail to use "rm" correctly
and destroy weeks of work, this just may be the ticket
for your next meal.  If you are a frustrated user of
"rm" simply talk to your system manager and show him
this nifty stuff.  

I have done my best to make this a nearly complete package
with man pages and all.  I do hope that you enjoy it.
Now, you may be a bit more of a wizard with shell
scripts so don't laugh too hard at my simpleton
implementation.  In the emptytrash script, you will
need to change it so that it looks for the appropriate
trashcan files in the appropriate directories.
In other words, you need to change it so that the
location of the users directories is searched.

I only have a system V to test this on.  But from my vast
(which isn't extreme, but does include sun3.4 and bsd4.3)
this should be fairly mobile from machine to machine.
I tried not to use to many system dependent things.
You must think I am crazy, well ...

If you have any complaints or want to tell me that
I am a complete fool, I will not object, just
write me at nosc!athey.
		-the bit butcher

\Rogue\Monster\
else
  echo "will not over write ./ReadMe"
fi
if `test ! -s ./can`
then
echo "writing ./can"
cat > ./can << '\Rogue\Monster\'
:
# @(#) can v1.0  Maintain file trash can    Author:  Russ Sage
# installed on MassComp by S. Luse 4/2/87
# Rewritten by The Bit Butcher

CAN=$HOME/.trashcan
USAGE="usage: can [lRr] file ..."

if [ ! -d $CAN ] 
	then mkdir $CAN
fi

set -- `getopt lR:r: $*`

if [ $? != 0 ]
then
	echo "$USAGE" >&2
	exit 2
fi
for i in $*
do
	case $i in
	-l)	echo "$CAN"
		ls $CAN
	 	exit 0;;
	-R) for j in $@
		do
			case $j in
			-*) ;;
			*)	if [ -f $CAN/$j ]
				then
					cp $CAN/$j `pwd`
					echo "Recovered $j"
				else
					echo "$CAN/$j Not Found"
				fi;;
			esac
		done
		exit 0;;
	-r)	for j in $@
		do
			case $j in
			-*)	;;
			*)	find $j -print | sort -r | while read FILE
				do
					if [ ! -d $FILE ]
					then 
						touch -c -a $FILE 
						mv $FILE $CAN
					else
						rmdir $FILE
					fi
				done
			esac
		done
		exit 0;;
	--) shift; break;;
	esac
done

if  [ -f $@ -o -d $@ ]
then
	find $@ -print | sort -r | while read FILE
	do
		if [ ! -d $FILE ]
		then 
			touch -c -a $FILE 
			mv $FILE $CAN
		else
			echo "can: $FILE directory"
		fi
	done
else
	echo "$USAGE" >&2
	exit 2
fi
\Rogue\Monster\
else
  echo "will not over write ./can"
fi
if `test ! -s ./can.1`
then
echo "writing ./can.1"
cat > ./can.1 << '\Rogue\Monster\'
.\" @(#)run.1	10.2 (MASSCOMP) 8/14/86
.RL "local"
.TH CAN 1
.SH NAME
can \- a replacement for rm that is safe
.SH SYNOPSIS
\fB can [ lRr ] <file | directory> ...
.br
.ns
.SH DESCRIPTION
.I Can
is often an alias of
.I rm(1).
.I Can
works similiarly, with the exception of putting things into a directory, in
your home directory, called ".trashcan."
The
.B -l
option will give you a listing of the "$HOME/.trashcan"
directory.
The
.B -r
option works recursively just the same as 
.I rm(1).
The 
.B -R
option will retrieve a file from the "$HOME/.trashcan" without the hassle
of looking for it.  The
.B -R
option copies the file from the "$HOME/.trashcan" directory into the
present working directory.
.sp
The trash gets dumped everyday but only gets rid of things that
are more than a week old.  In other words, you have a week to get something
back after you have
.I can
ned it.
.SH FILES
.TP 2.5i
$HOME/.trashcan
The reservoir of canned files
.SH SEE ALSO
.I
emptytrash(8), rm(1)
.SH BUGS
The 
.B -R
option does not work on wild cards.  You have to know the
exact name of a file in order to recover it with this command.
.sp
If further bugs are found please report them.
.SH AUTHOR
The Bit Butcher
.br
Inspired by the original
.B can
made available by Steph Luse, which he pirated from COD, which was written
by a Russ Sage.
\Rogue\Monster\
else
  echo "will not over write ./can.1"
fi
if `test ! -s ./emptytrash`
then
echo "writing ./emptytrash"
cat > ./emptytrash << '\Rogue\Monster\'
: /bin/sh

# EMPTYTRASH 
# Executed from root crontab file every night.
# It finds all files in all users .traschan directories and gets
# rid of any file that has not been accessed or modified for more
# than 7 days.  Note:  this works in conjunction with can.  can
# changes modifies the access time for a file when it moves the  
# file to the user's .trashcan diretory.

find /cd441/*/.trashcan -atime +7 -print | while read FILE
do 
#       echo $FILE
	rm $FILE
done
\Rogue\Monster\
else
  echo "will not over write ./emptytrash"
fi
if `test ! -s ./emptytrash.8`
then
echo "writing ./emptytrash.8"
cat > ./emptytrash.8 << '\Rogue\Monster\'
.\" @(#)run.1	10.2 (MASSCOMP) 8/14/86
.RL "local"
.TH EMPTYTRASH 8
.SH NAME
emptytrash \- the trash collector used with
.I can
.SH SYNOPSIS
\fBemptytrash
.br
.ns
.SH DESCRIPTION
.I Emptytrash
simply looks into each $HOME/.trashcan and checks the last access time
which is usually set by
.I can(1).
Anything that it finds that is more than seven days old it permanently
removes via 
.I rm(1).
This amount of time can be changed by changing the "7" in the script file.
The best thing to do is to put this in your root crontab file and have it
executed everyday.
.SH FILES
.TP 2.5i
$HOME/.trashcan
The reservoir of canned files
.SH SEE ALSO
.I
can(1), rm(1)
.SH BUGS
Only that this was a quicky and could be made better by having it accept an
argument that would determine the amount of elapsed time to check for.
.SH AUTHOR
The Bit Butcher
.br
With the help of Steph Luse.
\Rogue\Monster\
else
  echo "will not over write ./emptytrash.8"
fi
echo "Finished archive 1 of 1"
exit