1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
#!/bin/sh
###########################################################################
# Shell script to remove all but the arguments on the commandline,
# assuming that you're removing stuff from the current dir.
VER=1.03
PROG=`basename $0`
# print info and die
usage ()
{
cat << ENDUSAGE
ICCE Directory Cleaner $VER
Copyright (c) K. Kubat (karel@icce.rug.nl) / ICCE 1995. All rights reserved.
Another MegaHard Production!
Usage: keep [-t] [-v] [-rf] file(s)
where:
-t : (optional) flag, tryout mode -- must be first argument
-v : (optional) flag, increases verbosity
-r : (optional) flag, specifying recursive removal of files
-f : (optional) flag, specifying forced removal of files
file(s) : files to keep, others are removed
the files must be in the current directory
ENDUSAGE
exit 1
}
# print error msg and die
error ()
{
echo $PROG: $*
exit 1
}
# verbose message
chat ()
{
if [ "$VERBOSE" != "" ] ; then
echo keep: $@
fi
}
############################################################################
# main starts here
# assume options are off
RECURSIVE=""
FORCED=""
TRYOUT=no
VERBOSE=""
# parse flags
moreargs=yes
while [ $moreargs = yes ] ; do
case $1 in
-t )
TRYOUT=yes
chat "tryout mode"
shift
;;
-rf )
RECURSIVE=-r
FORCED=-f
chat "recursive and forced removal"
shift
;;
-fr )
RECURSIVE=-r
FORCED=-f
chat "recursive and forced removal"
shift
;;
-r )
RECURSIVE=-r
chat "recursive removal"
shift
;;
-f )
FORCED=-f
chat "forced removal"
shift
;;
-v )
VERBOSE=yes
shift
;;
-* )
error no such flag $1 defined, -r -f -fr -rf -v supported only
;;
* )
moreargs=no
;;
esac
done
# any arguments?
if [ "$1" = "" ] ; then
usage
fi
# build list of files]
for f in $* ; do
FILES=`echo $FILES $f | sed 's/\///'`
done
chat "all files or directories to keep: $FILES"
# check that all files are real ones
for f in $FILES ; do
if [ ! -f $f -a ! -d $f ] ; then
error specified file or directory $f not found, cannot keep it
fi
done
# get a listing of cur dir
LST=`ls -1`
# if not recursive removal: keep only true files in the list
if [ "$RECURSIVE" != "-r" ] ; then
for f in $LST ; do
if [ -f $f ] ; then
NEWLIST=`echo $NEWLIST $f`
fi
done
LST=$NEWLIST
fi
for f in $FILES ; do
LST=`echo $LST | tr ' ' '\n' | grep -v "^$f$"`
done
chat "list of files to remove: $LST"
if [ "$LST" = "" ] ; then
error "no files to remove"
fi
if [ "$TRYOUT" = "yes" ] ; then
echo 'Files that would be removed:'
echo "$LST"
else
rm $RECURSIVE $FORCED `echo "$LST"` \
|| error 'problem while removing files'
fi
exit 0
|