Commands follows
511 clear
512 history
513 history -d 505
514 history
515 history -d 507 510 513
516 history
517 history -d 509
518 hist
history -d 511;history -d 511;history -d 511;history -d 511;history -d 511;history -d 511;history -d 511;history -d 511;history -d 511;history -d 511;
Brute but functional
Maybe will be useful for someone.
When you login to any user of any console/terminal history of your current session exists only in some "buffer" which flushes to ~/.bash_history on your logout.
So, to keep things secret you can just:
history -r && exit
and you will be logged out with all your session's (and only) history cleared ;)
Try the following:
for i in {511..520}; do history -d $i; echo "history -d $i"; done
I use this (I have bash 4):
histrm() {
local num=${1:- 1}
builtin history -d $(builtin history | sed -rn '$s/^[^[:digit:]]+([[:digit:]]+)[^[:digit:]].*$/\1/p')
(( num-- )) && $FUNCNAME $num
builtin history -w
}
The builtin history
parts as well as the last -w
is because I have in place a variation of the famous tricks to share history across terminals and this function would break without those parts. They ensure a call to the real bash history
builtin (and not to my own wrapper around it), and to write the history to HISTFILE right after the entries were removed.
However this will work as it is with "normal" history configurations.
You should call it with the number of last entries you want to remove, for example:
histrm 10
Will remove the last 10 entries.
With Bash 5 you can now do a range...Hooray!:
history -d 511-520
or counting backwards 10:
history -d -10--1
Excerpt from Bash 5 Man Page:
'history'
Options, if supplied, have the following meanings:
'-d OFFSET' Delete the history entry at position OFFSET. If OFFSET is positive, it should be specified as it appears when the history is displayed. If OFFSET is negative, it is interpreted as relative to one greater than the last history position, so negative indices count back from the end of the history, and an index of '-1' refers to the current 'history -d' command.
'-d START-END' Delete the history entries between positions START and END, inclusive. Positive and negative values for START and END are interpreted as described above.
Here is my solution for Bash 4. It iteratively deletes a single entry or a range of history starting with lowest index.
delHistory () {
count=$(( ${2:-$1} - $1 ))
while [[ $count -ge 0 ]]; do
history -d "$1"
((count--))
done
}
delHistory 511 520
for h in $(seq $(history | tail -1 | awk '{print $1-N}') $(history | tail -1 | awk '{print $1}') | tac); do history -d $h; done; history -d $(history | tail -1 | awk '{print $1}')
If you want to delete 10 lines then just change the value of N to 10.