Sie sind auf Seite 1von 3

#!

/bin/sh
# Name : Lanka Bogoda
# Std No : S3224908
# This shell script is written by Lanka Bogoda as an assignment for Unix for sys
tem adminstrators in the
# Computing and Information Systems Engineering school of RMIT University.The sc
ript can be used to
# automatically removes files with names that end in .bak, .BAK, ~, # and core.
# You require a dirctory named "Backup" in the current directory to archive the
above files
# The t days old .bak, .BAK, ~, # files are archived with 'DD-MM-YYYY.tar'forma
t and delete them afterwards[if -a arg is passed].
# The t days old .bak, .BAK, ~, # files are deleted [if -a arg is not passed].
# The t days old core files and archive with 'DD-MM-YYYY.core.tar'format and del
ete them afterwards.
# Date created 02-04-2010
PRINTF=/usr/bin/printf
# Definition of memory variables for
# -d <directory> is the directory to be cleaned.
# -t <days old> is a "day old" threshold to remove or archive old files.
# -a archives and compresses the old files before deleting.
# -c only removes core files from system derived binaries.
# -h show proper command usage options.
dflag=
tflag=
cflag=
aflag=
hflag=
# Passing command line argument for memory varialbles
while getopts d:t:cah name
do
case $name in # Begin case
d) dflag=1
dval="$OPTARG";; #Location of the directoory to be cleane
d
t) tflag=1
tval="$OPTARG";; #Days threshold to remove files
c) cflag=1;;
a) aflag=1;;
h) hflag=1;;
?) $PRINTF "Usage: %s: [-d value] [-t value] [-c] [-a] [-h ]arg
s\n" $0
exit 2;;
esac #End case
done
if [ ! -z "$tflag" ]; then # validate the -t value for no of days
$PRINTF '%s days old ' "$tval"
fi
# verify arguments for directory(-d) and archive(-a)
if [ ! -z "$dflag" ] && [ ! -z "$aflag" ]; then
$PRINTF 'temporarly files in %s folder are archived at 'Backup' directo
ry and deleted \n' "$dval"
cd $dval
# Find the t days old temporaroy files and archive and delete them.
find -name "*.bak" -o -name "*.BAK" -o -name "*~" -o -name "*#" -type f
-mtime -$tval | xargs tar -cvf /home/Lanka/Unix/Backup/$(date +%d-%m-%Y).tar | x
args rm
fi
# verify arguments for directory(-d) and null for archive(-a)
if [ ! -z "$dflag" ] && [ -z "$aflag" ]; then
$PRINTF 'temporarly files in %s folder are deleted \n' "$dval"
cd $dval
# Find the t days old temporaroy files and delete
find -name "*.bak" -o -name "*.BAK" -o -name "*~" -o -name "*#" -type f
-mtime -$tval | xargs rm
fi
#-----------------------------------------------To be tested in a core file envi
ronment------------------------------------------------------------

#if [ ! -z "$cflag" ]; then


## verify arguments for directory(-d) and archive(-a)
# if [ ! -z "$dflag" ] && [ ! -z "$aflag" ] ; then
# $PRINTF 'temporarly files in %s folder are archived and deleted \n' "$d
val"
# cd $dval
## Find the t days old core files and archive with 'DD-MM-YYYY.core.tar'format a
nd delete them.
# find . -name core -type f -mtime -$tval | xargs file | grep "core file"
| sed -e 's/^.*from\ //' -e s/\'//g | xargs tar -cvf /home/Lanka/Unix/Backup/$(d
ate +%d-%m-%Y).core.tar | xargs rm
# fi
## verify arguments for directory(-d) and null for archive(-a)
# if [ ! -z "$dflag" ] && [ -z "$aflag" ]; then
# $PRINTF 'temporarly files in %s folder are deleted \n' "$dval"
# cd $dval
## Find the t days old core files and delete them.
# find . -name core -type f -mtime -$tval | xargs file | grep "core file"
| sed -e 's/^.*from\ //' -e s/\'//g | xargs rm
# fi
#fi

#-------------------------------------------------------------------------------
----------------------------
if [ ! -z "$hflag" ]; then
$PRINTF "Usage%s: [-d value] [-t value] [-c] [-a] [-h ]\n"
fi
shift `expr $OPTIND - 1`
if [ -z "$*" ]; then
$PRINTF ""
else
$PRINTF "Remaining arguments are: %s\n" "$*"
fi
cd /home/Lanka/Unix # go back to Unix directory

Das könnte Ihnen auch gefallen