Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!think.com!mintaka!ogicse!orstcs!statware!mcf From: mcf@statware.UUCP ( Mathieu Federspiel) Newsgroups: comp.unix.admin Subject: Re: Non Destructive Version of rm Summary: safe rm scripts Keywords: rm, undelete Message-ID: <11283@statware.UUCP> Date: 2 May 91 22:35:59 GMT Article-I.D.: statware.11283 References: <144@larry.UUCP> Reply-To: mcf@statware.UUCP ( Mathieu Federspiel) Distribution: na Organization: Statware, Corvallis, Oregon Lines: 306 In article <144@larry.UUCP> stockett@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@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 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 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 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 lsrm <<'@EOF' ls -a $* .#* @EOF set `wc -lwc rmrm <<'@EOF' # # rmrm#: to rm files moved with rm# USAGE="Usage: $0 " case $# in 1 ) ;; * ) echo $USAGE >&2 ; exit 1 ;; esac find $1 -name '.#*' -exec /bin/rm -f {} \; @EOF set `wc -lwc unrm <<'@EOF' for arg do base=`basename $arg` dir=`dirname $arg` mv ${dir}/.#$base $arg done @EOF set `wc -lwc