Fix trace eraser for HISTTIMEFORMAT-enabled systems
Changes: - Replace grep with awk to handle timestamp lines - Remove matching commands AND their preceding timestamp lines - Properly handle history format: #timestamp followed by command Issue: - Systems with HISTTIMEFORMAT set store timestamps as #<unix_time> - Simple grep only removed command lines, left orphaned timestamps - User's history showed toolkit commands still present (lines 990-1030) Solution: - awk script that tracks timestamp lines - Only prints timestamp if following command is kept - Removes both timestamp and command together atomically Tested: ✓ Removes 16 lines (8 commands + 8 timestamps) from 32-line test ✓ Preserves normal commands with their timestamps ✓ No toolkit patterns found after cleaning
This commit is contained in:
@@ -151,9 +151,27 @@ if [ "$CLEAN_HISTORY" = true ] && [ -f ~/.bash_history ]; then
|
|||||||
# Build single grep pattern for efficiency (single-pass filtering)
|
# Build single grep pattern for efficiency (single-pass filtering)
|
||||||
GREP_PATTERN="git\.mull\.lol|linux-server-management-toolkit|server-toolkit|launcher\.sh|erase-toolkit-traces"
|
GREP_PATTERN="git\.mull\.lol|linux-server-management-toolkit|server-toolkit|launcher\.sh|erase-toolkit-traces"
|
||||||
|
|
||||||
# Backup and clean in one efficient pass
|
# Backup and clean - handle HISTTIMEFORMAT timestamps
|
||||||
cp ~/.bash_history ~/.bash_history.bak.$$
|
cp ~/.bash_history ~/.bash_history.bak.$$
|
||||||
grep -Ev "$GREP_PATTERN" ~/.bash_history.bak.$$ > ~/.bash_history.cleaned.$$ 2>/dev/null || true
|
|
||||||
|
# Use awk to remove matching lines AND their preceding timestamp lines
|
||||||
|
awk -v pattern="$GREP_PATTERN" '
|
||||||
|
/^#[0-9]+$/ {
|
||||||
|
# This is a timestamp line, store it
|
||||||
|
timestamp = $0
|
||||||
|
next
|
||||||
|
}
|
||||||
|
{
|
||||||
|
# This is a command line
|
||||||
|
if ($0 !~ pattern) {
|
||||||
|
# Keep this command - print timestamp (if exists) and command
|
||||||
|
if (timestamp != "") print timestamp
|
||||||
|
print $0
|
||||||
|
}
|
||||||
|
# Reset timestamp for next iteration
|
||||||
|
timestamp = ""
|
||||||
|
}
|
||||||
|
' ~/.bash_history.bak.$$ > ~/.bash_history.cleaned.$$ 2>/dev/null || true
|
||||||
|
|
||||||
# Calculate lines removed
|
# Calculate lines removed
|
||||||
lines_before=$(wc -l < ~/.bash_history.bak.$$ 2>/dev/null || echo 0)
|
lines_before=$(wc -l < ~/.bash_history.bak.$$ 2>/dev/null || echo 0)
|
||||||
|
|||||||
Reference in New Issue
Block a user