Skip to content

Instantly share code, notes, and snippets.

@abdulbadii
Created September 28, 2018 20:18
Show Gist options
  • Save abdulbadii/20aaa8cf37e065b29b40cf286b6df461 to your computer and use it in GitHub Desktop.
Save abdulbadii/20aaa8cf37e065b29b40cf286b6df461 to your computer and use it in GitHub Desktop.
filsz(){
x=;f=;d=;p=~+;
for o
{
if [[ $o =~ ^-x ]];then
x=${o//[-x=]/}
elif [[ $o =~ ^-d ]];then
o=${o//[-d=]/}
a=${o%,*};b=${o#*,}
a=${a//[!0-9]/}
b=${b//[!0-9]/}
o=${a:+"-mindepth $a "}${b:+"-maxdepth $b "}
elif [[ $o =~ ^/ ]]; then
if test -d "$o" ;then
p=$o
elif test -f $o ;then
p=${o%/*}
f=${o##*/}
else echo Directory or file does not exist;exit 1
fi
else
p=~+/$o
if test -f $p ;then
f=$o
elif test ! -d $p ;then echo Directory does not exist;exit 1
fi
fi
}
x=${x:-$'tmp|bak|old'}
xt="\.(${x//,/|})"
fn=${f:+`echo $f| sed -E 's/[{}\.$]/\\\&/g; s/\*/.*/g; s/\?/./'`}
fn=${fn:-'[^/]+'}
y=0;a=;b=;J=9
IFS=$'\r\n' # set it for Windows port-Msys : \r\n, Mac: \r
while((1))
do
u="find $p -noleaf $d -type f -regextype posix-extended -iregex \".*/$fn$xt\" -printf \"%p %s\n\" |head -n$J"
s=
for s in `eval $u`
{
f=${s% *}
e=`echo $f| sed -E "s'^(.*)$xt\$'\1'"`
z=${s##* }
if test "$e" = "$a" ;then
if test $z -lt $y ;then
rm -fv $f
elif test $z -gt $y ;then
mv -fv $f ${b:=$a}
y=$z
elif test `find ${e%/*} -noleaf -maxdepth 1 -type f -iname "${f##*/}" -newer "$b"` ;then
rm -fv $b
a=$e
b=$f
y=$z
else
rm -fv $f
fi
elif test -f "$e" ;then
x=`find ${e%/*} -noleaf -maxdepth 1 -type f -iname "${e##*/}" -printf %s`
if test $z -gt $x ;then
mv -fv $f $e
y=$z
elif test $z -lt $x ;then
rm -fv $f
y=$x
elif test `find ${e%/*} -noleaf -maxdepth 1 -type f -iname "${e##*/}" -newer "$f"` ;then
rm -fv $f
y=$x
else
mv -fv $f $e
y=$z
fi
a=$e
b=
else
mv -fv $f $e
a=$e
b=$f
y=$z
fi
}
test -z $s&&return
((J++))
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment