我需要一种方法(一个脚本)将用户邮件池 (/var/spool/mail/theuser) 中除最后 100 条消息之外的所有消息移动到用户邮件目录 (/home/theuser/mail) 中的文件/mbox。有没有简单的方法可以做到这一点?
答案1
这是一个快速而粗糙的开始。没有错误检查,而且它实际上并没有“移动”任何东西。
#!/usr/local/bin/bash
#
# DIRTY hack to append all but the last 100 messages from each file
# in a dir of mbox files to like named files in /tmp
#
# if an mbox has less than 101 messages, skip it
#
# requires formail (from the procmail installation..)
#
# we're writing in tmp - to write in the user's dir move this
# inside the for loop and use $USERMAIL to write the base path
DESTDIR="/tmp"
for USERMAIL in `ls -1`
do
MESSAGECOUNT=`cat $USERMAIL | formail -q -s wc | wc -l | sed 's/^[ \t]*//'`
COPYTHISMANY=`expr $MESSAGECOUNT - 100`
if [ $COPYTHISMANY -gt 0 ]
then
echo "Copying $COPYTHISMANY messages from $USERMAIL to /tmp/$USERMAIL.allbutlast100"
cat $USERMAIL | formail -$COPYTHISMANY -s cat >> $DESTDIR/$USERMAIL.allbutlast100
# copy the last 100 to a new file
cat $USERMAIL | formail +$COPYTHISMANY -s cat >> $DESTDIR/$USERMAIL.last100
# to replace the original source mbox do something like
# mv /tmp/$USERMAIL.last100 $USERMAIL
fi
done