[comp.sources.misc] v02i049: Can - better and a definite replacement

athey@cod.nosc.mil (The Bit Butcher) (02/11/88)

Comp.sources.misc: Volume 2, Issue 49
Submitted-By: "The Bit Butcher" <athey@cod.nosc.mil>
Archive-Name: can-v2

[The bug he *didn't* fix concerns files with the same basename... not to
mention links (especially across devices), etc.  ++bsa]

I put this thing called "can" out on the net just recently and
soon thereafter I discovered a horrid error so if you grabbed it 
(or you missed it) you better get this one because this one lacks that
very error I just spoke of.  The error was that it worked recursively even
when you didn't want it to.  Kind of a dumb mistake, but that was a result
of not examining test results very closely.  I saw the directories
weren't gone but forgot to look for the files which were gone.

Sorry to bore you with such digression, but if you were interested...
Here it is all over again complete with man pages.

If you missed the first posting, this is a great replacement for
rm because with a simple alias ("alias rm can") things are removed to
a directory in your home called ".trashcan" which is cleaned of files
that are more than a week old by your system manager installing 
"emptytrash" (also included with man page) 
into the proper crontab file ("root").

				-the bit butcher


----------------------------cut it off and let it bleed ------------
#!/bin/sh
# to extract, remove the header and type "sh filename"
if `test ! -s ./ReadMe`
then
echo "writing ./ReadMe"
cat > ./ReadMe << '\end\file\'
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

\end\file\
else
  echo "will not over write ./ReadMe"
fi
if `test ! -s ./can`
then
echo "writing ./can"
cat > ./can << '\end\file\'
:
# @(#) 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
	ls -d $@ | 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
\end\file\
else
  echo "will not over write ./can"
fi
if `test ! -s ./can.1`
then
echo "writing ./can.1"
cat > ./can.1 << '\end\file\'
.\" @(#)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.
\end\file\
else
  echo "will not over write ./can.1"
fi
if `test ! -s ./emptytrash`
then
echo "writing ./emptytrash"
cat > ./emptytrash << '\end\file\'
: /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
\end\file\
else
  echo "will not over write ./emptytrash"
fi
if `test ! -s ./emptytrash.8`
then
echo "writing ./emptytrash.8"
cat > ./emptytrash.8 << '\end\file\'
.\" @(#)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.
\end\file\
else
  echo "will not over write ./emptytrash.8"
fi
echo "Finished archive 1 of 1"
exit