Non Destructive Version of rm

Mathieu Federspiel mcf at statware.UUCP
Fri May 3 08:35:59 AEST 1991


In article <144 at larry.UUCP> stockett at larry.UUCP (Jeff Stockett) writes:
>
>I'm looking for a version of rm (or a script) that will move deleted files to a
>temporary location like .wastebasket, so that novice users who accidentally
>delete files, can redeem themselves.  I've considered writing a script to
>do this, but I thought one might already exist.
>

  Following are Bourne shell scripts I implemented on our systems.
I install the scripts in /usr/local/bin, and then give everyone an
alias of "rm" to this script.
  What happens is, say, you "rm testfile".  The script moves
"testfile" to ".#testfile".  You then have a period of time to
"unrm testfile" to get the file back.  The period of time is
determined by the system administrator, who sets up a job to run
periodically to remove all files with names starting with ".#".
  For this removing process, the administrator must, of course,
warn users not to name files as ".#".  Since this is a hidden file,
there should be no problem.  Note that this preserves the directory
structure of files, which makes life easier than moving everything
to ".wastebasket".  Also note that directories will be moved, and
special handling of directories in your removing job may be
required.
  Enjoy!

--
Mathieu Federspiel                  mcf%statware.uucp at cs.orst.edu
Statware                            orstcs!statware!mcf
260 SW Madison Avenue, Suite 109    503-753-5382
Corvallis  OR  97333  USA           503-758-4666 FAX

#---------------------------------- cut here ----------------------------------
# This is a shell archive.  Remove anything before this line,
# then unpack it by saving it in a file and typing "sh file".
#
# Wrapped by Mathieu Federspiel <mcf at statware> on Tue Jun 12 17:17:26 1990
#
# This archive contains:
#	rm#.1	rmrm#.1	rm	lsrm	rmrm	unrm	
#
# Modification/access file times will be preserved.
# Error checking via wc(1) will be performed.

unset LANG

echo x - rm\#.1
cat >rm\#.1 <<'@EOF'
.TH RM 1 "LOCAL"
.tr ^
.SH NAME
rm, lsrm, unrm, rmrm \-
temporary file removal system
.SH SYNOPSIS
.B rm
[rm options] files

.B lsrm
[ls options]

.B unrm
files

.B rmrm
directory

.SH DESCRIPTION
The temporary file removal system will use
.B mv(1)
to move the specified \fBfiles\fR to the same name with the
prefix \fB.#\fR.
Files with this prefix are deleted from the system after they are
one day old.
.B Unrm
may be used to restore temporarily removed files, if they have not been
deleted.
.B Lsrm
is used to list files in the current directory which are temporarily removed.
.B Rmrm
is used to remove all files which begin with \fB.#\fR from the
directory which is specified, and all directories thereunder.
.B Find(1)
is used to identify and remove those files.

.SH CAVEATS
The use of options with rm will cause different results.
The rm(1) option -i is recognized and the script will prompt for a "y"
or "Y" response before moving the file specified.
Any other response will not move the file.
Options other than -i are not recognized by rm.
The use of any option other than -i will result in the option and
the list of files being passed to rm(1) unchanged.
Only the first item in the command list is checked for "-i".

While \fBlsrm\fR will correctly use \fBls(1)\fR options,
it will only list
temporarily removed files in the current directory.

.SH "SEE ALSO"
rm(1),
ls(1),
mv(1)

.SH AUTHOR
Mathieu Federspiel, Statware.
@EOF
set `wc -lwc <rm\#.1`
if test $1$2$3 != 572511434
then
	echo ERROR: wc results of rm\#.1 are $* should be 57 251 1434
fi

touch -m 1219172089 rm\#.1
touch -a 0605040190 rm\#.1
chmod 664 rm\#.1

echo x - rmrm\#.1
cat >rmrm\#.1 <<'@EOF'
.TH RM 1 "LOCAL"
.tr ^
.SH NAME
rm, lsrm, unrm, rmrm \-
temporary file removal system
.SH SYNOPSIS
.B rm
[rm options] files

.B lsrm
[ls options]

.B unrm
files

.B rmrm
directory

.SH DESCRIPTION
The temporary file removal system will use
.B mv(1)
to move the specified \fBfiles\fR to the same name with the
prefix \fB.#\fR.
Files with this prefix are deleted from the system after they are
one day old.
.B Unrm
may be used to restore temporarily removed files, if they have not been
deleted.
.B Lsrm
is used to list files in the current directory which are temporarily removed.
.B Rmrm
is used to remove all files which begin with \fB.#\fR from the
directory which is specified, and all directories thereunder.
.B Find(1)
is used to identify and remove those files.

.SH CAVEATS
The use of options with rm will cause different results.
The rm(1) option -i is recognized and the script will prompt for a "y"
or "Y" response before moving the file specified.
Any other response will not move the file.
Options other than -i are not recognized by rm.
The use of any option other than -i will result in the option and
the list of files being passed to rm(1) unchanged.
Only the first item in the command list is checked for "-i".

While \fBlsrm\fR will correctly use \fBls(1)\fR options,
it will only list
temporarily removed files in the current directory.

.SH "SEE ALSO"
rm(1),
ls(1),
mv(1)

.SH AUTHOR
Mathieu Federspiel, Statware.
@EOF
set `wc -lwc <rmrm\#.1`
if test $1$2$3 != 572511434
then
	echo ERROR: wc results of rmrm\#.1 are $* should be 57 251 1434
fi

touch -m 1219172089 rmrm\#.1
touch -a 0612171790 rmrm\#.1
chmod 664 rmrm\#.1

echo x - rm
cat >rm <<'@EOF'
#!/bin/sh
#   rm temporary
#   script to do an mv rather than rm to .# file
#   By Mathieu Federspiel, 1987
#   
#   recognizes -i option
#
# Modified to print if file deleted/not with -i.	--- MCF, Jan 1989
# Modified to test for write permission.	--- MCF, Aug 1989
# Modified to touch saved file.  This helps with backups.	--- MCF, Aug 1989

case "$1" in
-i)	shift
	for arg in $*
	do
		if [ \( -f $arg -o -d $arg \) -a -w $arg ]
		then
			echo "$arg [yn](n) ? \c"
			read yesno
			if [ "$yesno" = "y" -o "$yesno" = "Y" ]
			then
				 base=`basename $arg`
				 dir=`dirname $arg`
				 mv $arg ${dir}/.#$base && touch ${dir}/.#$base
				 echo "$arg removed."
			else
				 echo "$arg not removed."
			fi
		else
			echo "$arg: write permission denied"
		fi
	done ;;
-*)	/bin/rm "$@" ;;
*)	for arg
	do
		if [ -w $arg ]
		then
			base=`basename $arg`
			dir=`dirname $arg`
			mv $arg ${dir}/.#$base && touch ${dir}/.#$base
		else
			echo "$arg: write permission denied"
		fi
	done ;;
esac
@EOF
set `wc -lwc <rm`
if test $1$2$3 != 45169984
then
	echo ERROR: wc results of rm are $* should be 45 169 984
fi

touch -m 0824171389 rm
touch -a 0612163190 rm
chmod 775 rm

echo x - lsrm
cat >lsrm <<'@EOF'
ls -a $* .#*
@EOF
set `wc -lwc <lsrm`
if test $1$2$3 != 1413
then
	echo ERROR: wc results of lsrm are $* should be 1 4 13
fi

touch -m 0825120387 lsrm
touch -a 0611201390 lsrm
chmod 775 lsrm

echo x - rmrm
cat >rmrm <<'@EOF'
 #
#  rmrm#: to rm files moved with rm#

USAGE="Usage:  $0 <directory>"
case $# in
1 ) ;;
* ) 
echo $USAGE >&2 ; exit 1 ;;
esac

find $1 -name '.#*' -exec /bin/rm -f {} \;
@EOF
set `wc -lwc <rmrm`
if test $1$2$3 != 1137172
then
	echo ERROR: wc results of rmrm are $* should be 11 37 172
fi

touch -m 0415135888 rmrm
touch -a 0604074090 rmrm
chmod 775 rmrm

echo x - unrm
cat >unrm <<'@EOF'
for arg
  do
     base=`basename $arg`
     dir=`dirname $arg`
     mv ${dir}/.#$base $arg
done
@EOF
set `wc -lwc <unrm`
if test $1$2$3 != 61196
then
	echo ERROR: wc results of unrm are $* should be 6 11 96
fi

touch -m 0825120587 unrm
touch -a 0517152390 unrm
chmod 775 unrm

exit 0
-- 
Mathieu Federspiel                  mcf%statware.uucp at cs.orst.edu
Statware                            orstcs!statware!mcf
260 SW Madison Avenue, Suite 109    503-753-5382
Corvallis  OR  97333  USA           503-758-4666 FAX



More information about the Comp.unix.admin mailing list