Skip to content

Instantly share code, notes, and snippets.

@rgorsuch
Last active March 16, 2016 17:02
Show Gist options
  • Save rgorsuch/6818a3b06d09b704be8c to your computer and use it in GitHub Desktop.
Save rgorsuch/6818a3b06d09b704be8c to your computer and use it in GitHub Desktop.
function trunclog {
# Truncate a log file and leave N lines of tail remaining.
if [ "$*" = "" ]
then
echo "Usage: trunclog [lines] [log-file]"
else
REQUESTED_LINES=$1
LOG=$2
TEMP=/tmp/`basename $LOG`.tmp.$$
tail -$REQUESTED_LINES $LOG > $TEMP
ACTUAL_LINES=`wc -l $TEMP | awk '{print $1'}`
if [[ $ACTUAL_LINES == $REQUESTED_LINES || $((ACTUAL_LINES+1)) == $REQUESTED_LINES ]]
then
rm $LOG
mv -f $TEMP $LOG
echo "Truncated $LOG to $REQUESTED_LINES lines"
else
echo "Found fewer lines. Check actual lines with "wc -l $LOG" and check disk space on /tmp with "df /tmp"."
fi
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment