6

Since I'm saving history from different sessions this is an issue where erasedups can't help because I'm using the following:

PROMPT_COMMAND="$PROMPT_COMMAND;history -a"

Is there an easy way to delete duplicates in history?

9

It is possible to remove duplicated lines which are already in .bash_history by running

nl ~/.bash_history | sort -k 2  -k 1,1nr| uniq -f 1 | sort -n | cut -f 2 > unduped_history

followed by

cp unduped_history ~/.bash_history

I would also recommend to put the following in your ~/.bashrc:

export HISTCONTROL=ignoreboth:erasedups
  • 1
    Why ignoreboth? That activates both ignoredups and ignorespace and the latter does not seem relevant here. Also, your command will simply empty .bash_history, you want cp unduped_history ~/.bash_history or cat unduped_history > ~/.bash_history. – terdon Feb 27 '14 at 17:31
  • I agree that ignorespace could be omitted. I missed to type cp by accident. I added the the extra option -k 1,1nr to the first sort to take the last occurence of a duplicated commands. – NamelessDeveloper Feb 27 '14 at 17:49
  • Great, now I can upvote that really cool nl trick with a clear conscience :) – terdon Feb 27 '14 at 17:50

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.