Last active
April 6, 2022 02:53
-
-
Save bunop/d9d4990c24727e2d9aafa7950e865f91 to your computer and use it in GitHub Desktop.
bash_history advanced cleanup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# http://stackoverflow.com/questions/338285/prevent-duplicates-from-being-saved-in-bash-history | |
# remove duplicates while preserving input order | |
function dedup { | |
#awk '! x[$0]++' $@ | |
tac $@ | awk '! x[$0]++' | tac | |
} | |
# removes $HISTIGNORE commands from input | |
function remove_histignore { | |
if [ -n "$HISTIGNORE" ]; then | |
# replace : with |, then * with .* | |
local IGNORE_PAT=`echo "$HISTIGNORE" | sed s/\:/\|/g | sed s/\*/\.\*/g` | |
# negated grep removes matches | |
grep -vx "$IGNORE_PAT" $@ | |
else | |
cat $@ | |
fi | |
} | |
# clean up the history file by remove duplicates and commands matching | |
# $HISTIGNORE entries | |
function history_cleanup { | |
local HISTFILE_SRC=~/.bash_history | |
local HISTFILE_DST=/tmp/.$USER.bash_history.clean | |
if [ -f $HISTFILE_SRC ]; then | |
\cp $HISTFILE_SRC $HISTFILE_SRC.backup | |
#Via gli spazi alla fine del file | |
sed -i "s/ *$//" $HISTFILE_SRC | |
dedup $HISTFILE_SRC | remove_histignore >| $HISTFILE_DST | |
\mv $HISTFILE_DST $HISTFILE_SRC | |
chmod go-r $HISTFILE_SRC | |
fi | |
} | |
# clean bash history when starting a new interactive shell | |
case "$-" in | |
*i*) history_cleanup | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment