Creating a nondestructive 'rm'

Jerry Peek jerryp at cmx.npac.syr.edu
Mon Nov 28 23:48:35 AEST 1988


In article <67991 at felix.UUCP> tondu at felix.UUCP (Walter Tondu) writes:
- In article <4460006 at hpindda.HP.COM> burdick at hpindda.HP.COM (Matt Burdick) writes:
- Here's a solution which I use.  This comes from "Tricks of the UNIX
- Masters" by Russel G. Sage. Published by Howard W. Sams & Co.
- I have modified it a tad in order to fit my needs more exactly

- if [ "`echo \"$1\" | cut -c1`" = "-" ]
- then
-         case $1 in
-         -l)        echo "$CAN:"
-                 /bin/ls $CAN
-                 exit 0;;
-         -r)        echo "removing $CAN/*:"
-                 /bin/rm -rf $CAN/*
-                 exit 0;;
-         -f)        echo "force removal"
-                 /bin/rm -rf $@
-                 exit 0;;
-         -?)        echo "usage can [-l] [-r] file [file ...]" >&2
-                 exit 0;;
-         esac
- fi

Geez.  I don't have "Tricks of the UNIX Masters," and maybe that wasn't
the original script from the book, but it seems like that section of
the code would be a lot more efficient without the (redundant) echo/cut/test.
And it doesn't handle the -f case too well because the $@ picks up the -f.
Here's a quick hack (not tested, but should work) with a few more fixes, too:

case "$1" in
-l)        echo "$CAN:"
        /bin/ls $CAN
        ;;
-r)        echo "removing $CAN/*:"
        /bin/rm -rf $CAN/*
        ;;
-f)        echo "force removal"
        /bin/rm -r $@         # PASSES rm THE -f FROM $1
        ;;
-?)        echo "usage can [-l] [-f] [-r] file [file ...]" >&2
        exit 1
        ;;
esac
exit 0

Not to be picky here, but I think that a script that you use as much as "rm"
should be as efficient as you can make it...

--Jerry Peek, Northeast Parallel Architectures Center, Syracuse, NY
  jerryp at cmx.npac.syr.edu
  +1 315 443-1722



More information about the Comp.unix.questions mailing list