Last active
March 16, 2016 17:02
-
-
Save rgorsuch/6818a3b06d09b704be8c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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