Improve trace eraser history cleaning efficiency and reliability
Changes: - Replace chained grep -v with single grep -Ev for efficiency - Fix critical bug: history -w was overwriting cleaned file - Use history -r instead of history -w to reload cleaned history - Single-pass filtering instead of 5 separate grep processes - Better user messaging about other terminal sessions Technical improvements: - Escaped regex metacharacters in pattern (git\.mull\.lol) - Use 3988207 for unique temp file names - More efficient: 1 process vs 5 processes Tested: ✓ Removes all toolkit commands regardless of position ✓ Preserves normal commands ✓ No temp file errors ✓ History properly reloaded into memory ✓ 7 toolkit entries removed from 20-line test history
This commit is contained in:
@@ -148,36 +148,35 @@ if [ "$CLEAN_HISTORY" = true ] && [ -f ~/.bash_history ]; then
|
|||||||
# Disable history recording for this session to prevent re-adding commands
|
# Disable history recording for this session to prevent re-adding commands
|
||||||
set +o history
|
set +o history
|
||||||
|
|
||||||
# Remove toolkit-related entries from history file
|
# Build single grep pattern for efficiency (single-pass filtering)
|
||||||
cp ~/.bash_history ~/.bash_history.tmp
|
GREP_PATTERN="git\.mull\.lol|linux-server-management-toolkit|server-toolkit|launcher\.sh|erase-toolkit-traces"
|
||||||
|
|
||||||
# Remove lines containing toolkit-related patterns
|
# Backup and clean in one efficient pass
|
||||||
grep -v "git.mull.lol" ~/.bash_history.tmp > ~/.bash_history.tmp2 2>/dev/null || true
|
cp ~/.bash_history ~/.bash_history.bak.$$
|
||||||
grep -v "linux-server-management-toolkit" ~/.bash_history.tmp2 > ~/.bash_history.tmp3 2>/dev/null || true
|
grep -Ev "$GREP_PATTERN" ~/.bash_history.bak.$$ > ~/.bash_history.cleaned.$$ 2>/dev/null || true
|
||||||
grep -v "server-toolkit" ~/.bash_history.tmp3 > ~/.bash_history.tmp4 2>/dev/null || true
|
|
||||||
grep -v "launcher.sh" ~/.bash_history.tmp4 > ~/.bash_history.tmp5 2>/dev/null || true
|
|
||||||
grep -v "erase-toolkit-traces" ~/.bash_history.tmp5 > ~/.bash_history 2>/dev/null || true
|
|
||||||
|
|
||||||
# Calculate lines removed before deleting temp files
|
# Calculate lines removed
|
||||||
lines_before=$(wc -l < ~/.bash_history.tmp 2>/dev/null || echo 0)
|
lines_before=$(wc -l < ~/.bash_history.bak.$$ 2>/dev/null || echo 0)
|
||||||
lines_after=$(wc -l < ~/.bash_history 2>/dev/null || echo 0)
|
lines_after=$(wc -l < ~/.bash_history.cleaned.$$ 2>/dev/null || echo 0)
|
||||||
lines_removed=$((lines_before - lines_after))
|
lines_removed=$((lines_before - lines_after))
|
||||||
|
|
||||||
# Clean up temp files
|
# Replace history file with cleaned version
|
||||||
rm -f ~/.bash_history.tmp*
|
mv ~/.bash_history.cleaned.$$ ~/.bash_history
|
||||||
|
|
||||||
|
# Clean up backup
|
||||||
|
rm -f ~/.bash_history.bak.$$
|
||||||
|
|
||||||
echo " ✓ Removed $lines_removed toolkit-related history entries"
|
echo " ✓ Removed $lines_removed toolkit-related history entries"
|
||||||
|
|
||||||
# Clear in-memory history completely
|
# Reload cleaned history into current session
|
||||||
history -c
|
history -c # Clear in-memory history
|
||||||
|
history -r # Reload from cleaned file
|
||||||
# Write the empty history to file
|
|
||||||
history -w
|
|
||||||
|
|
||||||
|
echo " ✓ In-memory history reloaded from cleaned file"
|
||||||
echo ""
|
echo ""
|
||||||
echo " ✓ Bash history cleaned"
|
echo "NOTE: Other active terminal sessions may still have old history in memory."
|
||||||
echo ""
|
echo " Run 'exec bash' or 'history -c && history -r' in those terminals,"
|
||||||
echo "NOTE: Run 'exec bash' or logout/login to start fresh shell with clean history."
|
echo " or simply logout/login to start completely fresh."
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo ""
|
echo ""
|
||||||
|
|||||||
Reference in New Issue
Block a user