· Tutorial · 2 min read
Declutter Your Command Line: Master Bash History Deduplication
Tired of Bash history clutter? This tutorial shows you how to deduplicate your command history with a simple script, boosting your terminal efficiency.

Reclaim Your Terminal: The History Cleanup
Is your Bash history feeling more like a chaotic junk drawer than a helpful command log? If you are spending precious seconds (which add up!) scrolling through repetitive commands just to find the one you need, it is time to bring order to your terminal history with Bash history deduplication!
Why Bother Deduplicating? The Productivity Payoff
If you are like me and use tab auto-complete to save time while typing in a terminal, a clean and deduplicated shell history isn’t just about aesthetics – it’s a real boost to your command-line productivity:
- Efficient Command Retrieval: Reduces time spent searching for commands by providing instant access to unique, frequently used entries.
- Marginal Performance Enhancement: Decreases history file size, potentially leading to slight improvements in shell startup and history loading speeds.
- Streamlined Script Creation: Facilitates the use of clean, non-redundant command examples when copying from history into scripts.
Bash History Deduplication: The Script for a Streamlined Terminal
Let’s get down to business and explore a Bash script to deduplicate your history.
#!/bin/bash
# --- Deduplicate Bash History Script ---
# 1. Prepare: Number and Sort History
nl ~/.bash_history | sort -k 2 -k 1,1nr > unduped_history_temp
# 2. Deduplicate: Identify and Keep Unique Commands
uniq -f 1 < unduped_history_temp > unduped_history_stage
# 3. Re-sort: Restore Original Chronological Order (Within Duplicates)
sort -n < unduped_history_stage > unduped_history_final
# 4. Clean Up: Replace and Remove Temporary Files
cp unduped_history_final ~/.bash_history
rm -f unduped_history_temp unduped_history_stage unduped_history_final
echo "Bash history deduplication complete! Your history is now leaner and meaner."

