Created
April 17, 2026 14:28
-
-
Save NQevxvEtg/2d1425ee3d06e6afcb59c236c438f8fe 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
| #!/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