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?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityI'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?
To see a sorted history without changing it, do:
history | sort -k2
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.
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.
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.