How to delete history of last 10 commands in shell?

前端 未结 19 1819
孤城傲影
孤城傲影 2021-01-29 18:47

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         


        
相关标签:
19条回答
  • 2021-01-29 19:40
    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

    0 讨论(0)
  • 2021-01-29 19:42

    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 ;)

    0 讨论(0)
  • 2021-01-29 19:42

    Try the following:

    for i in {511..520}; do history -d $i; echo "history -d $i"; done
    
    0 讨论(0)
  • 2021-01-29 19:48

    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.

    0 讨论(0)
  • 2021-01-29 19:49

    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
    
    0 讨论(0)
  • 2021-01-29 19:54
    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.

    0 讨论(0)
提交回复
热议问题