0

I'm using HISTIGNORE to ignore the most used commands and HISTCONTROL=ignoreboth:erasedups to remove duplicates.

Is there a way to periodically sort the contents of the .bash_history in alphabetical order?

2
  • Please define ”periodically”, and where would you like to store the output; once sorted. Commented Aug 23, 2018 at 17:48
  • like once a week or something ... in => ~/.bash_history Commented Aug 23, 2018 at 17:54

1 Answer 1

3
  1. To see a sorted history without changing it, do:

    history | sort -k2 
  2. To sort the history file, do:

    sort -o ~/.bash_history ~/.bash_history 

    Then log out of bash by typing exit, and log back in. The new terminal instance will have an alphabetically sorted history.

  3. For the most cautious possible way to sort the history file, first exit all running instances of bash, (for the current user anyway), then do:

    sort -o ~/.bash_history ~/.bash_history 

    Note: Users generally have no bad results from editing ~/.bash_history while still logged in. But it's fairly certain that exiting all running instance of bash is as safe or safer.

1
  • This answer would be better with a cron job method as well, but I'm undecided on how to handle systems where terminals are left open for long periods of time. Maybe the job could remind them to log out and log back in, but suppose that's not convenient. Better still would be to have a method that doesn't require and exit at all. Commented Aug 26, 2018 at 17:05

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.