Make user history cleaning optional in trace eraser
User bash history cleaning is now optional with a prompt, since most users only work as root. Changes: • Added user count detection • Prompts: "Clean user bash histories too? (y/n) [n]" • Default is "no" (skip user histories) • If no users exist, automatically skips • Only cleans root history by default (faster, covers 99% of use cases) This makes the script faster and more sensible for typical usage where only root is used to run the toolkit.
This commit is contained in:
@@ -73,29 +73,47 @@ if [ -f ~/.bash_history ]; then
|
|||||||
history -w
|
history -w
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Clean bash history for all users
|
# Clean bash history for all users (optional - skip if only root is used)
|
||||||
echo "→ Checking user histories..."
|
echo "→ Checking user histories..."
|
||||||
|
user_count=0
|
||||||
for user_home in /home/*; do
|
for user_home in /home/*; do
|
||||||
if [ -f "$user_home/.bash_history" ]; then
|
[ -d "$user_home" ] && user_count=$((user_count + 1))
|
||||||
username=$(basename "$user_home")
|
|
||||||
echo " → Cleaning history for $username..."
|
|
||||||
|
|
||||||
# Remove last 50 lines from user history
|
|
||||||
total_lines=$(wc -l < "$user_home/.bash_history")
|
|
||||||
if [ "$total_lines" -gt 50 ]; then
|
|
||||||
lines_to_keep=$((total_lines - 50))
|
|
||||||
head -n "$lines_to_keep" "$user_home/.bash_history" > "$user_home/.bash_history.tmp"
|
|
||||||
mv "$user_home/.bash_history.tmp" "$user_home/.bash_history"
|
|
||||||
chown "$username:$username" "$user_home/.bash_history" 2>/dev/null
|
|
||||||
echo " ✓ Cleaned (removed last 50 entries)"
|
|
||||||
else
|
|
||||||
> "$user_home/.bash_history"
|
|
||||||
chown "$username:$username" "$user_home/.bash_history" 2>/dev/null
|
|
||||||
echo " ✓ Cleared (file had < 50 entries)"
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
done
|
done
|
||||||
|
|
||||||
|
if [ "$user_count" -eq 0 ]; then
|
||||||
|
echo " ✓ No user accounts found (skipped)"
|
||||||
|
else
|
||||||
|
echo " Found $user_count user account(s)"
|
||||||
|
echo ""
|
||||||
|
read -p " Clean user bash histories too? (y/n) [n]: " clean_users
|
||||||
|
|
||||||
|
if [ "$clean_users" = "y" ] || [ "$clean_users" = "Y" ]; then
|
||||||
|
for user_home in /home/*; do
|
||||||
|
if [ -f "$user_home/.bash_history" ]; then
|
||||||
|
username=$(basename "$user_home")
|
||||||
|
echo " → Cleaning history for $username..."
|
||||||
|
|
||||||
|
# Remove last 50 lines from user history
|
||||||
|
total_lines=$(wc -l < "$user_home/.bash_history")
|
||||||
|
if [ "$total_lines" -gt 50 ]; then
|
||||||
|
lines_to_keep=$((total_lines - 50))
|
||||||
|
head -n "$lines_to_keep" "$user_home/.bash_history" > "$user_home/.bash_history.tmp"
|
||||||
|
mv "$user_home/.bash_history.tmp" "$user_home/.bash_history"
|
||||||
|
chown "$username:$username" "$user_home/.bash_history" 2>/dev/null
|
||||||
|
echo " ✓ Cleaned (removed last 50 entries)"
|
||||||
|
else
|
||||||
|
> "$user_home/.bash_history"
|
||||||
|
chown "$username:$username" "$user_home/.bash_history" 2>/dev/null
|
||||||
|
echo " ✓ Cleared (file had < 50 entries)"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
else
|
||||||
|
echo " ✓ Skipped user histories (only root cleaned)"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
echo ""
|
||||||
|
|
||||||
# Clean system logs (pattern-based for logs, not history)
|
# Clean system logs (pattern-based for logs, not history)
|
||||||
echo "→ Cleaning system logs..."
|
echo "→ Cleaning system logs..."
|
||||||
if [ -f /var/log/messages ]; then
|
if [ -f /var/log/messages ]; then
|
||||||
|
|||||||
Reference in New Issue
Block a user