rmsecure - source for a safe "rm"

sources-request at panda.UUCP sources-request at panda.UUCP
Thu Feb 6 09:46:45 AEST 1986


Mod.sources:  Volume 3, Issue 103
Submitted by: DIDELOT Andre <talcott!seismo!mcvax!cui!andre>


Below is the presentation and the code of a command which replace "rm";
it works exactly the same, except that it just asks for confirmation when
its arguments use some wild cards.

[ This ONLY works with csh, because it uses aliases, and it may only work
  with the BSD4.2 or better csh      - John P. Nelson, moderator mod.sources
]

I wrote it long ago, after having had some bad experiments with conjonction
of misstyping and wild cards in the use of the usual "rm" command.
These accidents turned sometimes to drama, especially when I was learning
the (difficult?:-) job of the superuser.
Months along, other users too experimented such personnal disasters and the
safe "rm" gained popularity on our site. We have now used it without problems
over a year.

I would be pleased if it could be of some interest for other (super)users.

	Andre DIDELOT				CHUNET: andre at cui.unige.chunet

MAIL:	Centre Universitaire d'Informatique	UUCP:	mcvax!cernvax!cui!andre
	Universite de Geneve				mcvax!cernvax!cui!root
	Rue du General Dufour 24
	CH - 1211 GENEVE 4			BITNET: DIDELOT at CGEUGE51
	SWITZERLAND					SYSTEM at CGEUGE51


===============================================================================
#! /bin/sh
# This is a shell archive, meaning:
# 1. Remove everything above the #! /bin/sh line.
# 2. Save the resulting text in a file.
# 3. Execute the file with /bin/sh (not csh) to create the files:
#	rmsecure.doc
#	rmsecure.csh
# This archive created: Wed Feb  5 18:39:13 1986
export PATH; PATH=/bin:$PATH
echo shar: extracting "'rmsecure.doc'" '(1893 characters)'
if test -f 'rmsecure.doc'
then
	echo shar: will not over-write existing file "'rmsecure.doc'"
else
cat << \SHAR_EOF > 'rmsecure.doc'
Adding security when removing files:
------------------------------------

DESCRIPTION:
------------
With the standard "rm", no confirmation is asked when using wild cards;
you surely once experienced that this may be really catastrophic.
If not, think about a misstyping like "rm * BAK" instead of "rm *BAK" !!!

A new command, "rmsecure", may prevent this by asking for a confirmation
before removing any file whose name issued from a wild card expansion.

This command works a bit slower than the usual "rm", but is really safer !


USE:
---
To use it, you MUST create a special alias like this one:

	alias rm '(set noglob; rmsecure \!*)'

in order to prevent former wild cards expansion by the shell.

When used, any list of files resulting from a wild card expansion will be
asked globally for confirmation prior to deletion; if answer is negative,
each file in the expanded list will be prompted separatly, like the "-i"
option of usual "rm".

As reel deletion take place only just before completion of the command,
this one can be safely interrupted at any time if necessary; in this case,
no file will be removed.


WARNING:
--------
Files whose name is completely specified won't be asked for confirmation,
even if they appear between other arguments using wild cards.


LIMITS:
-------
 - as all expanded file names are concatened within one unique string,
   command can fail due to limitation of shell variable length.
   To overcome this, just select narrowed range.

 - due to successive evaluations, any quotes or double quotes will be lost;
   removing files with "strange" names should be done with the standard "rm"
   command using the "-i" option with a "*" wild card.

 - the following version is running perfectly with Cshell of 4.2 bsd;
   problems were encountered trying to run it on Unisoft System III,
   probably due to some limitation of the Cshell implemented.

SHAR_EOF
if test 1893 -ne "`wc -c < 'rmsecure.doc'`"
then
	echo shar: error transmitting "'rmsecure.doc'" '(should have been 1893 characters)'
fi
fi
echo shar: extracting "'rmsecure.csh'" '(1066 characters)'
if test -f 'rmsecure.csh'
then
	echo shar: will not over-write existing file "'rmsecure.csh'"
else
cat << \SHAR_EOF > 'rmsecure.csh'
#! /bin/csh -f
# csh script ; secure remove of files
onintr out
set interact
set force
set recur
set minus
while ("$1" == "-" || "$1" =~ -[f,i,r])
    switch ($1)
	case -i: 
	    set interact=-i
	    shift
	    breaksw
	case -f: 
	    set force=-f
	    shift
	    breaksw
	case -r: 
	    set recur=-r
	    shift
	    breaksw
	case  -: 
	    set minus=-
	    shift
	    break
    endsw
end
if ($interact == -i) then
    /bin/rm -i $force $recur $minus $*
    exit($status)
endif
set f1=`echo $0`
set f1=$f1:t" : remove"
set r
while ("$1" != '')
    set b=`echo $1`
    if ("$b" != '') then
	set noglob
	set a=$1
	unset noglob
	if ("$a" == "$b") then
	    set r="$r $b"
	else
	    if ($#b == 1) then
		echo -n $f1 $b '? '
		if ( $< == "y" ) then
		    set r="$r $b"
		endif
	    else
		echo -n $f1 $b '? '| fold
		if ( $< == "y" ) then
		    set r="$r $b"
		else
		    foreach n ($b)
			echo -n $f1 $n '? '
			if ( $< == "y" ) then
			    set r="$r $n"
			endif
		    end
		endif
	    endif
	endif
    endif
    shift
end
/bin/rm $force $recur $minus $r
out: 
exit(0)
SHAR_EOF
if test 1066 -ne "`wc -c < 'rmsecure.csh'`"
then
	echo shar: error transmitting "'rmsecure.csh'" '(should have been 1066 characters)'
fi
fi
exit 0
#	End of shell archive



More information about the Mod.sources mailing list