试试这个 bash 函数,它有两个参数:
来源:
function rotate () {
# minimum file size to rotate in MBi:
local MB="$1"
# filename to rotate (full path)
local F="$2"
local msize="$((1024*1024*${MB}))"
test -e "$F" || return 2
local D="$(dirname "$F")"
local E=${F##*.}
local B="$(basename "$F" ."$E")"
local s=
echo "rotate msize=$msize file=$F -> $D | $B | $E"
if [ "$(stat --printf %s "$F")" -ge $msize ] ; then
for i in 8 9 7 6 5 4 3 2 1 0; do
s="$D/$B-$i.$E"
test -e "$s" && mv $s "$D/$B-$((i+1)).$E"
# emtpy command is need to avoid exit iteration if test fails:
:;
done &&
mv $F $D/$B-0.$E
else
echo "rotate skip: $F < $msize, skip"
fi
return $?
}
Run Code Online (Sandbox Code Playgroud)