#!/bin/ksh
#******************************************************************************
#
# find files with size range.(min, max) to delete for disk space
#
# optins:
# -------------------------------------------------------------
# -a: for all folders under /a/psft
# -r: root folder (default: /a/psft)
# -s: sort by size (default: date)
# -u: unit (K=KB, M=MB, G=GB, default: M)
#
# folders:
# --------------------------------------------------------------
# default to exclude following folders
# "/vol1/psft/aaa/*"
# "/vol2/psft/bbb/*"
# "/vol1/psft/Oracle/*"
#
#******************************************************************************
tmp=$0.$$
trap "rm $tmp 2>/dev/null" 0 1 2 3 15
#default options:
root=/a/psft; min=100; max=99999; tmp=0; unit="M"; all=0; sort=d;
while getopts :asr:u: args
do
case $args in
a) all=1;
;;
s) sort=s;
;;
r) root="$OPTARG";
;;
u) unit="$OPTARG";
;;
?) echo 'invalid option: '$OPTARG
echo 'syntax: ff -arsu [min] [max]'
echo 'min, max: size'
echo '-a: all folders'
echo '-r: starting folder'
echo '-s: sort by size'
echo '-u: unit (K/M/G)'
return 1;;
esac
done
shift "$(($OPTIND -1))"
if [ $# -gt 2 ]; then
echo 'Incorrect # pf parameters'
return 1
fi
if [ $# -eq 1 ]; then
min=$1
else
if [ $# -eq 2 ]; then
min=$1
max=$2
fi
fi
#****** fix unit
case $unit in
k|K) unit="k"; out="K"
;;
m|M) unit="M"; out="M"
;;
g|G) unit="G"
;;
?) echo 'Invalid unit: '$unit
return 1;;
esac
#****** check numeric
int='^[0-9]+$'
dec='^0.[0-9]+$'
if ! [[ $min =~ $int ]] && ! [[ $min =~ $dec ]]; then
echo "error: $min Not a number" >&2; return 1
fi
if ! [[ $max =~ $int ]] && ! [[ $max =~ $dec ]]; then
echo "error: $max Not a number" >&2; return 1
fi
#****** check range
if [[ "$(($min - $max ))" -gt 0 ]]; then
tmp=$max; max=$min; min=$tmp;
fi
#****** execute
echo "options: all=$all, min=$min, max=$max, unit=$unit, sort=$sort"
if [ $all -eq 1 ]; then
find $root -type f -size +"$min"$unit -size -"$max"$unit -exec ls -lh --time-style='+%Y_%m_%d %H:%M' {} \; > $tmp
else
# exclude folders and files
find $root -path /a/psft/lost+found -prune -o -type f -size +"$min"$unit -size -"$max"$unit \
-not -path "/vol1/psft/aaa/*" \
-not -path "/vol2/psft/bbb/**" \
-not -path "/vol1/psft/Oracle/*" \
! -name "stderr" \
! -name "stdout" \
-exec ls -lh --time-style='+%Y_%m_%d %H:%M' {} ";" > $tmp
fi
if [ $sort = 'd' ]; then
# sort by date
cat $tmp | sort -k6,7
else
# sort by size, convert to unit
awk -v unit="$unit" -v out="$out" '{
if (unit == "k") {
if ( $5 ~ /G/) {
# convert GB to MB
size=$5;
sub(/G/,"",$size);
size=size*1024;
$5=size"M"; }
if ( $5 ~ /M/) {
# conver MB to KB
size=$5;
sub(/M/,"",$size);
size=size*1024;
$5=size out; }
}
if (unit == "M") {
if ( $5 ~ /G/) {
# convert GB to MB
size=$5;
sub(/G/,"",$size);
size=size*1024;
$5=size out;
}
}
print $0;
} ' $tmp | sort -n -k5
fi