Skip to content

Instantly share code, notes, and snippets.

@iiey
Created December 27, 2018 09:22
Show Gist options
  • Save iiey/ec580314521d5d0b1baa501b7a20741c to your computer and use it in GitHub Desktop.
Save iiey/ec580314521d5d0b1baa501b7a20741c to your computer and use it in GitHub Desktop.
cleanup ~/.bash_history by removing repeated entries from top to bottom
#!/bin/bash
#@brief: shorten ~/.bash_history by removing repeated entries from top to bottom (keep last)
#this additionaly cleanup because below setting not work properly
#HISTCONTROL=ignoreboth:erasedups #ignoreboth(^space and consecutive duplicates), remove duplicates
#@note: this script not work with HISTTIMEFORMAT enable (command with timestamp)
#declare filepath
HIST=$HOME/.bash_history
TEMP=/tmp/tmp_hist
#1. output history in reversed order
#2. remove all repeated entries except first one then store in /tmp if succeed
# First time a specific value of a line ($0) is seen, the value of x[$0] is zero
# The value of zero is inverted with '!' to one
# An statement that evaluates to one causes the default action, which is printed
# Every next time, the value of x[$0] has been incremented. It negated value to zero and doesn't print
# So to keep first command instead using: awk '!x[$0]++' ~/.bash_history
#3. write inversed temporare history ('top' again back to 'bottom') into history file
#4. delete tmp file if everything succeed
tac $HIST | awk '!x[$0]++' > $TEMP && tac $TEMP > $HIST && rm $TEMP
#reverse output (tac) is not available on mac os. Consider install coreutils or using longer command:
#cat $HIST | nl | sort -k2 -k 1,1nr | uniq -f1 | sort -n | cut -f2 > $TEMP && cat $TEMP > $HIST && rm $TEMP
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment