Skip to content

Instantly share code, notes, and snippets.

@terrywang
Last active November 30, 2023 02:47
Show Gist options
  • Save terrywang/74d1617600dfc7c2fb4c6365c60b09db to your computer and use it in GitHub Desktop.
Save terrywang/74d1617600dfc7c2fb4c6365c60b09db to your computer and use it in GitHub Desktop.
Bash history HISTFILE deduplication while preserving order
# backup HISTFILE
cp ~/.bash_history{,.bak}
# deduplicate - deprecated
nl ~/.bash_history.bak | sort -k2,2 -k1,1nr | uniq -f1 | sort -k1,1n | cut -f2 > ~/.bash_history
# dedupelicate
cp ~/.bash_history{,.bak-$(date -I)}
tac $HISTFILE | awk '!x[$0]++' | tac | sponge $HISTFILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment