I have a script:
DAYS=10
print_usage() {
echo "\n"
echo "Usage: $PROG [-days] [-file|-dir] dir1 [ dir2 ... dirN]"
echo " This program will delete all file/directories that are beyond "
echo " 'x days' old."
echo " -days - This flag allows you to define x days [default:10]"
echo " -file|-dir - This flag allows one to remove files [default: directories]"
echo "\n"
}
while [ ! -z "$1" ]
do
case $1 in
-file* | -f*)
TYPE="f"
;;
-link* | -l*)
TYPE="l"
;;
-dir* | -d*)
TYPE="d"
;;
-1* | -2* | -3* | -4* | -5* | -6* | -7* | -8* | -9* | -0* )
DAYS=`echo $1 | awk -F- '{printf("%d",$2)}'`
;;
-* )
print_usage
exit -3
;;
* )
if [ -d $1 ]
then
if [ "$TYPE" = "f" or "$TYPE" = "l" ]
then
echo "Searching $1 for files that are $DAYS days old"
find $1 -type $TYPE -mtime +$DAYS -print -exec /usr/bin/rm -f {} \;
else
echo "Searching $1 for directories that are $DAYS days old"
TMP_LOG=$LOG_HOME/delete.$$
find $1/* -type $TYPE -mtime +$DAYS -print > ${TMP_LOG}
cat $TMP_LOG
exit;
for rm_file in `cat $TMP_LOG | grep -v 'log$'`
do
if [ -d ${rm_file} ]
then
/usr/bin/rm -rf ${rm_file}
else
echo "Skipping ${rm_file} because, either root dir is removed or it is not a directories"
fi
done
/usr/bin/rm -f $TMP_LOG
fi
else
echo "Following is NOT a DIRECTORY: $1"
fi
esac
We call this script from another like this:
/export/applications/dte/sh/clean_disk.sh -60 /export/applications/dte/web/release
/export/applications/dte/sh/clean_disk.sh -14 -file ${DTE_WDATA_DIR}/log
/export/applications/dte/sh/clean_disk.sh -30 -link /export/applications/dte/web/data/outfiles
I don't understand, why case we check $1
and try to set up type as file, link, dir, if $1
is always DAYS
And why script is checking $1
as directory, when we definitely know that it is a number?
Thanks for contribution