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?

  • Please define ”periodically”, and where would you like to store the output; once sorted. – maulinglawns Aug 23 '18 at 17:48
  • like once a week or something ... in => ~/.bash_history – Ash Aug 23 '18 at 17:54
2
  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.

| improve this answer | |
  • 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. – agc Aug 26 '18 at 17:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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