home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Usenet 1994 October
/
usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso
/
misc
/
volume39
/
trm
/
part01
next >
Wrap
Text File
|
1993-08-29
|
16KB
|
509 lines
Newsgroups: comp.sources.misc
From: tpaquette@ita.lgc.com (Trevor Paquette)
Subject: v39i059: trm - rm replacement, does remove and recover, Part01/01
Message-ID: <1993Aug29.164417.24371@sparky.sterling.com>
X-Md4-Signature: 7dd49b82c5de9e96d2581c0e3c64cf5e
Sender: kent@sparky.sterling.com (Kent Landfield)
Reply-To: tpaquette@ita.lgc.com
Organization: Landmark/ITA
Date: Sun, 29 Aug 1993 16:44:17 GMT
Approved: kent@sparky.sterling.com
Submitted-by: tpaquette@ita.lgc.com (Trevor Paquette)
Posting-number: Volume 39, Issue 59
Archive-name: trm/part01
Environment: UNIX
I wrote the following after accidently deleting some files that I did not
want to delete.. Hope someone else finds it as useful as I have.
Trevor's rm. Version 1.04
It's 'rm' but more... It can handle removing directories.. recovering
'removed' files and undoing the last 'remove'.. read on..
Don't forget to chmod 755 this file after installing it.
trm file [file..]
will MOVE the named files to the $TRMTMP directory
If the file being moved already has a version under $TRMTMP then
the version number is incremented and the file is moved.
EX: trm gm.c gm.c is currently not in TRMTMP)
TRMTMP would now contail a file called gm.c,01
This is the first version of gm.c under TRMTMP
trm gm.c there already is a 'gm.c' under TRMTMP. This
file would be moved to gm.c,02. It is the second
version of gm.c
trm -list | -l [file..]
will list the named files that are in the $TRMTMP directory
This will list all the versions of the named files.
If no file is given then the entire contents of TRMTMP are listed
trm -recover | -rc file [file..]
will recover the biggest version of the named files from the
$TRMTMP directory to `pwd`
EX: trm -rc gm.c There is are two versions of gm.c in TRMTMP.
gm.c,01 and gm.c,02. trm will grab gm.c,02
and recover it as gm.c
trm -undo | -un
will undo the last rm
meaning you accidently removed a bunch of files and instead of
listing them one by one and recovering each.. this will do all of
them for you in one shot..
NOTE: THIS WILL NOT AUTOMAGICALLY PUT THE FILES BACK FROM WHENCE
THEY CAME. IT WILL PUT THE FILES TO `pwd`
trm -listundo | -lun
will list the undo files
trm -purge | -p [file..]
will REMOVE ALL VERSIONS of the named files that are in the
$TRMTMP directory
[no files named will remove the $TRMTMP directory completly and then
remake it]
---------
#! /bin/sh
# This is a shell archive. Remove anything before this line, then feed it
# into a shell via "sh file" or similar. To overwrite existing files,
# type "sh file -c".
# Contents: trm
# Wrapped by kent@sparky on Sun Aug 29 11:38:40 1993
PATH=/bin:/usr/bin:/usr/ucb:/usr/local/bin:/usr/lbin ; export PATH
echo If this archive is complete, you will see the following message:
echo ' "shar: End of archive 1 (of 1)."'
if test -f 'trm' -a "${1}" != "-c" ; then
echo shar: Will not clobber existing file \"'trm'\"
else
echo shar: Extracting \"'trm'\" \(11745 characters\)
sed "s/^X//" >'trm' <<'END_OF_FILE'
X#!/bin/sh
X#
X# Trevor's rm. Version 1.04
X#
X# It's 'rm' but more... It can handle removeing directories.. recovering
X# 'removed' files and undoing the last 'remove'.. read on..
X#
X# dont't forget to chmod 755 this file after installing it
X#
X# trm file [file..]
X# will MOVE the named files to the $TRMTMP directory
X# If the file being moved already has a version under $TRMTMP then
X# the version number is incremented and the file is moved.
X# EX: trm gm.c gm.c is currently not in TRMTMP)
X# TRMTMP would now contail a file called gm.c,01
X# This is the first version of gm.c under TRMTMP
X#
X# trm gm.c there already is a 'gm.c' under TRMTMP. This
X# file would be moved to gm.c,02. It is the second
X# version of gm.c
X#
X# trm -list | -l [file..]
X# will list the named files that are in the $TRMTMP directory
X# This will list all the versions of the named files.
X# If no file is given then the entire contents of TRMTMP are listed
X#
X# trm -recover | -rc file [file..]
X# will recover the biggest version of the named files from the
X# $TRMTMP directory to `pwd`
X# EX: trm -rc gm.c There is are two versions of gm.c in TRMTMP.
X# gm.c,01 and gm.c,02. trm will grab gm.c,02
X# and recover it as gm.c
X#
X# trm -undo | -un
X# will undo the last rm
X# meaning you accidently removed a bunch of files and instead of
X# listing them one by one and recovering each.. this will do all of
X# them for you in one shot..
X# NOTE: THIS WILL NOT AUTOMAGICALLY PUT THE FILES BACK FROM WHENCE
X# THEY CAME. IT WILL PUT THE FILES TO `pwd`
X#
X# trm -listundo | -lun
X# will list the undo files
X#
X# trm -purge | -p [file..]
X# will REMOVE ALL VERSIONS of the named files that are in the
X# $TRMTMP directory
X# [no files named will remove the $TRMTMP directory completly and then
X# remake it]
X#
X# ---------------------------------------------------------------------------
X# Version 1.04 Aug 26 1993
X#
X# Bug Fix. when removeing a dir that already exists it would try to use the last
X# entry in the dir as the version number.. bad. Use ls -1d, not ls -1
X#
X# Version 1.03 Aug 25 1993
X#
X# Add version information.
X# Thanx to montanaro@ausable.crd.ge.com (Skip Montanaro) for this.
X# Removing a dir/file that already exists in $TRMTMP, will cause it
X# to be set to the next 'version' of that file. Recovering a file/dir
X# will grab the biggest version.
X# Add basename $0 for getting the program name
X#
X# Version 1.02 Aug 25 1993
X#
X# Add short form options
X# Bug Fix. enclose TRMDEBUG in quotes wherever needed
X#
X# Version 1.01 Aug 23 1993
X#
X# - add TRMDEBUG to set -x and -v for debugging
X# - add -purge option
X# - add check for moving a file ontop of existing one (ONLY if moved failed)
X#
X# Version 1.00 July 22 1993
X#
X# Initial release
X#
X# ===========================================================================
X# How to Setup
X# ===========================================================================
X# I put the following in my .cshrc to create the trmlast and trmtmp
X# variables for each shell, only if interactive
X
X# if ( $?prompt ) then
X# # create trm variables
X# # NOTE: TRMLOCK is created in the .login
X# setenv TRMLAST $HOME/.trmtmp/.trmlast.`hostname`.$$
X# setenv TRMTMP $HOME/.trmtmp
X# fi
X
X# You may also want to put the following remove, recover and listing
X# aliases in your .cshrc
X
X# alias rm 'echo "Use trm instead"'
X# alias rc 'trm -recover \!*'
X# alias prc 'trm -purge \!*'
X# alias lsrc 'trm -list \!* | more'
X# alias rcu 'trm -undo'
X# alias lsrcu 'trm -listundo | more'
X
X# ---------------------------------------------------------------------------
X# I also put the following in my .login
X
X# # create recover directory.
X# # Only the process on the machine that created the directory should be able
X# # to remove it
X# # NOTE: $TRMTMP and $TRMLAST are created in the .cshrc
X# setenv TRMLOCK $HOME/.trmlock.`hostname`.$$
X
X# if !( -d $TRMTMP ) then
X# mkdir $TRMTMP
X# chmod 700 $TRMTMP
X# echo "$TRMTMP created at `date`. Lockfile is $TRMLOCK" > $TRMLOCK
X# fi
X
X# ---------------------------------------------------------------------------
X# I also put the following in my .logout
X
X# if ( -f $TRMLOCK ) then
X# /bin/rm -rf $TRMTMP
X# /bin/rm -f $TRMLOCK
X# fi
X
X# ===========================================================================
X# start of trm
X# ===========================================================================
X
X# get program name
Xprog=`basename $0`
X
X# setenv TRMDEBUG to 1 to display verbose debugging info
Xif [ "$TRMDEBUG" = "1" ]; then
X set -x
X set -v
Xfi
X
X# if $TRMTMP is not set then look under $home/.trmtmp
Xif [ "$TRMTMP" = "" ]; then
X TRMTMP=$HOME/.trmtmp
Xfi
X
X# make sure $TRMTMP is a directory
Xif [ ! -d $TRMTMP ]; then
X echo $prog: ERROR: $TRMTMP is not a directory or does not exist
X exit 1
Xfi
X
X# if no arguments passed then return
Xif [ $# -eq 0 ]; then
X exit 0
Xfi
X
X# ---------------------------------------------------------------------------
X# handle listing
X# ---------------------------------------------------------------------------
Xif [ "$1" = "-list" -o "$1" = "-l" ]; then
X
X # shift out -list from the arg list
X shift
X
X # if no futher args then list the directory
X if [ $# -eq 0 ]; then
X ls -laug $TRMTMP
X exit 0
X fi
X
X # cd to the directory and list each argument individually
X cd $TRMTMP
X for i in $*; do
X ls -laug $i,*
X done
X
X exit 0
Xfi
X
X# ---------------------------------------------------------------------------
X# handle recover
X# ---------------------------------------------------------------------------
Xif [ "$1" = "-recover" -o "$1" = "-rc" ]; then
X
X # shift out -recover from the arg list
X shift
X
X # if no futher args then exit
X if [ $# -eq 0 ]; then
X exit 0
X fi
X
X # the destination directory is where you are now
X destdir=`pwd`
X
X # cd to the TRMTMP directory
X cd $TRMTMP
X
X # for each arg passed try to 'mv' it to the destination dir
X for i in $*; do
X
X # get back the biggest version
X rev=`(ls -1d $TRMTMP/$i,* 2>/dev/null) |\
X sort |\
X tail -1 |\
X sed -e 's/.*,\([0-9]*\)/\1/'`
X
X /bin/mv -f $i,$rev $destdir/$i > /dev/null 2>&1
X
X if [ $? != 0 ]; then
X # the move failed...
X
X # probably tried to remove a file that already exists in $TRMTMP
X if [ -d $destdir/$i ]; then
X echo $prog: ${i} already exists in $destdir as a dir, move if needed.
X elif [ -f $destdir/$i ]; then
X echo $prog: ${i} already exists in $destdir as a file, move if needed.
X elif [ -d $i ]; then
X # tried to move a directory.. most likely failure is trying to move
X # a directory across filesystems.. sooo lets tar up the whole thing
X # and then untar it in the destination directory
X tar -cBf - $i,$rev | ( cd $destdir ; tar -xBpf - )
X mv $destdir/$i,$rev $destdir/$i
X
X # now remove the directory from $TRMTMP
X /bin/rm -rf $i,$rev
X
X fi
X fi
X done
X exit 0
Xfi
X
X# ---------------------------------------------------------------------------
X# handle undo
X# ---------------------------------------------------------------------------
Xif [ "$1" = "-undo" -o "$1" = "-un" ]; then
X
X # the destination directory is where you are now
X destdir=`pwd`
X
X # cd to the TRMTMP directory
X cd $TRMTMP
X
X # if $TRMLAST is not set then we have nothing to do
X if [ "$TRMLAST" = "" ]; then
X exit 0
X fi
X
X # if $TRMLAST does not exist then we have nothing to do
X if [ ! -f $TRMLAST ]; then
X exit 0
X fi
X
X # foreach file in the $TRMLAST list try to 'mv' it to the destination dir
X for i in `cat $TRMLAST`; do
X
X # get back the biggest version
X rev=`(ls -1d $TRMTMP/$i,* 2>/dev/null) |\
X sort |\
X tail -1 |\
X sed -e 's/.*,\([0-9]*\)/\1/'`
X
X /bin/mv -f $i,$rev $destdir/$i > /dev/null 2>&1
X
X if [ $? != 0 ]; then
X # the move failed...
X
X # probably tried to remove a file that already exists in $TRMTMP
X if [ -d $destdir/$i ]; then
X echo $prog: ${i} already exists in $destdir as a dir, move if needed.
X elif [ -f $destdir/$i ]; then
X echo $prog: ${i} already exists in $destdir as a file, move if needed.
X elif [ -d $i ]; then
X # tried to move a directory.. most likely failure is trying to move
X # a directory across filesystems.. sooo lets tar up the whole thing
X # and then untar it in the destination directory
X tar -cBf - $i,$rev | ( cd $destdir ; tar -xBpf - )
X mv $destdir/$i,$rev $destdir/$i
X
X # now remove the directory from $TRMTMP
X /bin/rm -rf $i,$rev
X
X fi
X fi
X done
X
X # at this point we want to reset the $TRMLAST file list
X /bin/rm -f $TRMLAST
X echo > $TRMLAST
X
X exit 0
Xfi
X
X# ---------------------------------------------------------------------------
X# handle listundo
X# ---------------------------------------------------------------------------
Xif [ "$1" = "-listundo" -o "$1" = "-lun" ]; then
X
X # if $TRMLAST is not set then we have nothing to do
X if [ "$TRMLAST" = "" ]; then
X exit 0
X fi
X
X # if $TRMLAST does not exist then we have nothing to do
X if [ ! -f $TRMLAST ]; then
X exit 0
X fi
X
X # cat the $TRMLAST file
X cat $TRMLAST
X
X exit 0
Xfi
X
X# ---------------------------------------------------------------------------
X# handle purge
X# ---------------------------------------------------------------------------
Xif [ "$1" = "-purge" -o "$1" = "-p" ]; then
X
X # shift out -purge from the arg list
X shift
X
X # if no futher args then remove $TRMTMP and remake it
X if [ $# -eq 0 ]; then
X /bin/rm -rf $TRMTMP
X mkdir $TRMTMP
X exit 0
X fi
X
X # cd to the directory and remove each argument individually
X cd $TRMTMP
X for i in $*; do
X /bin/rm -rf $i,*
X done
X
X exit 0
Xfi
X
X# ---------------------------------------------------------------------------
X# handle remove
X# ---------------------------------------------------------------------------
X
X# at this point we want to reset the old $TRMLAST file list
X if [ "$TRMLAST" != "" ]; then
X /bin/rm -f $TRMLAST
X fi
X
Xfor i in $*; do
X
X # if the filename passed does not exist then echo what 'rm' would
X # for now only deal with files and dirs..
X if [ ! -f $i -a ! -d $i ]; then
X echo $prog: ${i}: No such file or directory
X else
X # figure out a version number to move the file to
X rev=`(ls -1d $TRMTMP/$i,* 2>/dev/null) |\
X sort |\
X tail -1 |\
X sed -e 's/.*,\([0-9]*\)/\1/'`
X if [ "$rev" = "" ] ; then
X rev=1;
X else
X rev=`expr $rev + 1`
X fi
X if [ "$rev" = "" ]; then
X rev=01
X fi
X rev=`echo $rev | awk '{if(length($1)==1){print 0$1}else{print $1}}'`
X if [ $rev -gt 99 ]; then
X echo $prog: ${i} already has 99 versions, staying at 99
X rev=99
X fi
X
X # try to move the file to $TRMTMP
X /bin/mv -f $i $TRMTMP/$i,$rev > /dev/null 2>&1
X
X if [ $? != 0 ]; then
X # the move failed
X
X # probably tried to remove a file that already exists in $TRMTMP
X if [ -d $i ]; then
X # tried to move a directory.. most likely failure is trying to move
X # a directory across filesystems.. sooo lets tar up the whole thing
X # and then untar it in the $TRMTMP directory
X tar -cBf - $i | ( cd $TRMTMP ; tar -xBf - ) > /dev/null 2>&1
X mv $TRMTMP/$i $TRMTMP/$i,$rev
X
X # now remove the directory
X /bin/rm -rf $i
X fi
X
X fi
X
X # at this point the 'remove' was succesfull. Add the filename to the
X # $TRMLAST file for possible undoing.
X if [ "$TRMLAST" != "" ]; then
X echo $i >> $TRMLAST
X fi
X
X fi
X
Xdone
X
END_OF_FILE
if test 11745 -ne `wc -c <'trm'`; then
echo shar: \"'trm'\" unpacked with wrong size!
fi
chmod +x 'trm'
# end of 'trm'
fi
echo shar: End of archive 1 \(of 1\).
cp /dev/null ark1isdone
MISSING=""
for I in 1 ; do
if test ! -f ark${I}isdone ; then
MISSING="${MISSING} ${I}"
fi
done
if test "${MISSING}" = "" ; then
echo You have the archive.
rm -f ark[1-9]isdone
else
echo You still must unpack the following archives:
echo " " ${MISSING}
fi
exit 0
exit 0 # Just in case...