Last active
December 16, 2015 00:19
-
-
Save kelchm/5347099 to your computer and use it in GitHub Desktop.
Simple bash script to truncate lines from the beginning of a file. Useful for dealing with large log files.
This file contains 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
#!/bin/bash | |
function log_output { | |
logger -p daemon.warning $1 | |
echo $1 | |
} | |
function truncate_start { | |
FILE_PATH="$1" | |
SAVE_LINES=$2 | |
FILE_LINECOUNT=($(wc -l ($FILE_PATH) | awk '{print $1}')) | |
if [[ $SAVE_LINES -ge $FILE_LINECOUNT ]] | |
then | |
> $FILE_PATH | |
log_output "$FILE_PATH - file truncated to null" | |
elif [[ $SAVE_LINES -gt 0 ]] | |
then | |
NEW_START_LINE=$(($FILE_LINECOUNT-$SAVE_LINES+1)) | |
sed -ie "1,${NEW_START_LINE}d" $FILE_PATH | |
log_output "$FILE_PATH - first $NEW_START_LINE lines removed" | |
else | |
log_output "$FILE_PATH - invalid argument to keep '$SAVE_LINES' lines" | |
fi | |
} | |
function find_file_matches { | |
BASE_PATH="$1" | |
FILENAME="$2" | |
MIN_FILESIZE=$3 | |
SAVE_LINES=$4 | |
FILE_LIST=($(find $BASE_PATH -type f -name $FILENAME -size +$MIN_FILESIZE 2>/dev/null)) | |
for FILE in "${FILE_LIST[@]}" | |
do | |
truncate_start $FILE $SAVE_LINES | |
done | |
} | |
# Let's look for some common types of logs that should be made smaller | |
# find_file_matches [base path] [filename] [min filesize] | |
# brcm-iscsi.log | |
#find_file_matches "/var/logs" "brcm-iscsi.log" "10M" | |
# error_log | |
#find_file_matches "/home" "error_log" "10M" | |
find_file_matches "/Users/kelchm/Downloads" "error_log" "1k" "10" | |
find_file_matches "/Users/kelchm/Downloads" "foo" "1k" "100" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment