Replacing part of a filename using bash scripting

My brother has a dovecot mail server that every now and again the cache would corrupt on it. He would just go in, check the logs and rename the file affected. He just asked if I could script the process. So this little script will search through a log, extract the filename, alter its name and move the file to the new location:

#!/bin/bash

QFILE=test.log

grep "Error: Maildir filename has wrong W value:" $QFILE | while read line
do
fileNo=$(echo "$line" | awk '{split($0,a,","); for (idx in a) { split(a[idx], b, "="); if (b[1] == "W") print b[2]; }}')
fileName=$(echo "$line" | awk '{split($0,a," /"); print a[2]}')
echo "/$fileName"
echo "$fileNo"
fileNameFixed=${fileName//,W=$fileNo}
echo "/$fileNameFixed"
mv /$fileName /$fileNameFixed
done

Comments

Popular posts from this blog

Windows Server and the Task Scheduler Error Code 0x3

The living wage failure

IDE's and speed