Skip to content

Instantly share code, notes, and snippets.

@NQevxvEtg
Created April 17, 2026 14:28
Show Gist options
  • Select an option

  • Save NQevxvEtg/2d1425ee3d06e6afcb59c236c438f8fe to your computer and use it in GitHub Desktop.

Select an option

Save NQevxvEtg/2d1425ee3d06e6afcb59c236c438f8fe to your computer and use it in GitHub Desktop.
#!/bin/bash
# Check if a file path was provided
if [ -z "$1" ]; then
echo "Usage: $0 /path/to/your/logfile.log"
exit 1
fi
FILE=$1
# Check if the file exists
if [ ! -f "$FILE" ]; then
echo "Error: File '$FILE' not found."
exit 1
fi
# Calculate lines
TOTAL_LINES=$(wc -l < "$FILE")
HALF_LINES=$((TOTAL_LINES / 2))
echo "Total lines: $TOTAL_LINES. Keeping the last $HALF_LINES lines..."
# Use tail to grab the bottom half and write to a temp file
# Using a temp file on the same disk makes the 'mv' operation atomic
tail -n "$HALF_LINES" "$FILE" > "$FILE.tmp" && mv "$FILE.tmp" "$FILE"
echo "Done. Log truncated by 50%."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment