Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

How can Bash history be automatically cleaned up? For example

  • Remove detectable typos from history (ls-lh does not show up in which, so it is removed, I meant to type ls -lh)
  • Clean excessive spaces cd  / and cd /    should be normalized so they are the same.

Can this be done in bash? Maybe a filter on all commands before they are executed, or before they go into history?

I hope I don't have to wait until it is written to the file before running a cleanup routine. That would mean logging out before cleanup occurs. :(

share|improve this question

3 Answers 3

i would prefer to keep alias to all common mistakes that happen example

alias ls-lh = ls -lh

Use alias to fix missing space typos:

alias cd..='cd ..

Use HISTIGNORE to specify all the commands that you want to ignore from the history. use export HISTIGNORE="$LIST" to list that you want to ignore inside history

There are several other options of interest controlled by the built-in 'shopt' command.

see here

share|improve this answer
1  
+1 for using bash aliases. Also a good tip for keeping the history cleaner is using export HISTCONTROL="ignoredups" to prevent duplicates of commands. –  Herman Torjussen Jan 2 '12 at 23:49

I don't know of any way to customize history beyond the few HIST… variables. You may want to switch to zsh, which is a little smarter (but only a little). In zsh, you can turn on command autocorrection (setopt correct), which will suggest a correction for some typos based on the available command names (e.g. on my system it offers to correct sl to ls, but doesn't do anything about ls-lh). Zsh's duplicate is smarter in that it detects non-consecutive duplicates (under setopt hist_ignore_all_dups). Zsh doesn't do any whitespace normalization in the history, but the completion system is a little smarter than bash's at keeping the amount of whitespace consistent whether you type a full name or hit Tab, which helps.

share|improve this answer

From this blog, this script can remove automatically all commands that are not found by bash.

If you don't already have a bash PROMPT_COMMAND, add this to .bashrc:

PROMPT_COMMAND="mypromptcommand"
function mypromptcommand {
}

That function will now be run every time your prompt is about to appear. Within it, we put this:

local exit_status=$?
# If the exit status was 127, the command was not found. Let's remove it from history
local number=$(history | tail -n 1 | awk '{print $1}')
if [ -n "$number" ]; then
    if [ $exit_status -eq 127 ] && ([ -z $HISTLASTENTRY ] || [ $HISTLASTENTRY -lt $number ]); then
        history -d $number
    else
        HISTLASTENTRY=$number
    fi
fi

The link provides the explanation of how it works

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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