[comp.sources.misc] v12i006: Safe "rm" in C shell

@uunet.uu.net:flur%duke@gatech.edu@uunet.UU.NET (04/14/90)

Posting-number: Volume 12, Issue 6
Submitted-by: @uunet.uu.net:flur%duke@gatech.edu@uunet.UU.NET
Archive-name: safe-rm.csh/part01

I would like to submit the following dumb shell script;  hopefully
it will be useful to someone out there.
#! /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:
#	safe-rm
#	safe-rm.1
# This archive created: Mon Apr  9 10:02:39 1990
export PATH; PATH=/bin:$PATH
if test -f 'safe-rm'
then
	echo shar: will not over-write existing file "'safe-rm'"
else
cat << \SHAR_EOF > 'safe-rm'
#!/bin/csh -f
#
#  Sample Use:	safe-rm file1 file2 dir1 dir2
#
#  Author :	Peter W. Flur
#  	    	Georgia Institute of Technology
#    	    	School of Electrical Engineering
#	   	 
#  Address:	Box 32500
#  	    	Georgia Institute of Technology
#		Atlanta, GA  30332
#
#  Phone:	(404) 583-9355
#
#  E-mail:	flur@duke.gatech.edu
#
#  Description:	This script uses the csh to provide a trashcan 
#		analogy in unix.  A directory defined by the 
#		environment variable TRASH is created and used		
#		to copy files into.  A weekly script or something 
#		of that nature can be used to clean out the trashcan.
#  
#  Advice:	Probably should be used by aliasing the rm command to safe-rm.
#

set PATH=/bin
set TRASH=$HOME/.trash

if !( -d $TRASH ) then
#  echo "safe-rm:  Trash Can does not exist.  Creating a new can."
  mkdir $TRASH
endif

if ( $#argv <= 0 ) then
  echo "Usage: safe-rm [-r] <file> [<file> ...]"
  exit 0
endif

if ( "$argv[1]" == '-r' ) then
  set RECURSE=1
else
  set RECURSE=0
endif

while ( $#argv > 0 ) 

  if ( -f $argv[1] ) then

    mv $argv[1] $TRASH/$argv[1].$$

    if ($status) then
      tar cf - $argv[1] | (cd $TRASH; tar xf -)
      /bin/rm -fr $argv[1].$$
    endif

  else

    if ( $RECURSE == 1 ) then
      if ( -d $argv[1] ) then
        mv $argv[1] $argv[1].$$
        tar cf - $argv[1].$$ | (cd $TRASH; tar xf -)
        /bin/rm -fr $argv[1].$$
      endif
    else
      echo safe-rm: $argv[1] directory
    endif

  endif

  shift
end
SHAR_EOF
chmod +x 'safe-rm'
fi # end of overwriting check
if test -f 'safe-rm.1'
then
	echo shar: will not over-write existing file "'safe-rm.1'"
else
cat << \SHAR_EOF > 'safe-rm.1'
.\" Copyright (c) 1980 Regents of the University of California.
.\" All rights reserved.  The Berkeley software License Agreement
.\" specifies the terms and conditions for redistribution.
.\"
.\"	@(#)safe-rm.1	1.0 (Duke) 4/3/90
.\"
.TH SAFE-RM 1 "April 3, 1990"
.UC 4
.SH NAME
safe-rm  \- move files or directories into a trash can
.SH SYNOPSIS
.B safe-rm
file ...
.PP
.SH DESCRIPTION
.I Safe-rm
removes the entries for one or more files from a directory.
If an entry was the last link to the file, the file is destroyed.
Removal of a file requires write permission in its directory,
but neither read nor write permission on the file itself.
.PP
If a file has no write permission and the standard input is a terminal,
its permissions are printed and a line is read from the standard input.
If that line begins with `y' the file is deleted, otherwise the file remains.
.PP
.I Rmdir
removes entries for the named directories, which must be empty.
.SH "SEE ALSO"
rm(1), unlink(2), rmdir(2)
SHAR_EOF
chmod +x 'safe-rm.1'
fi # end of overwriting check
#	End of shell archive
exit 0