How to delete history of last 10 commands in shell?

前端 未结 19 1823
孤城傲影
孤城傲影 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:31

    My answer is based on previous answers, but with the addition of reversing the sequence so that history items are deleted from most recent to least recent.

    Get your current history (adjust the number of lines you want to see):

    history | tail -n 10
    

    This gives me something like

    1003  25-04-2016 17:54:52 echo "Command 1"
    1004  25-04-2016 17:54:54 echo "Command 2"
    1005  25-04-2016 17:54:57 echo "Command 3"
    1006  25-04-2016 17:54:59 echo "Command 4"
    1007  25-04-2016 17:55:01 echo "Command 5"
    1008  25-04-2016 17:55:03 echo "Command 6"
    1009  25-04-2016 17:55:07 echo "Command 7"
    1010  25-04-2016 17:55:09 echo "Command 8"
    1011  25-04-2016 17:55:11 echo "Command 9"
    1012  25-04-2016 17:55:14 echo "Command 10"
    

    Select the start and end positions for the items you want to delete. I'm going to delete entries 1006 to 1008.

    for h in $(seq 1006 1008); do history -d 1006; done
    

    This will generate history -d commands for 1006, then 1007 becomes 1006 and 1006 is deleted, then 1008 (became 1007) is now 1006 and gets deleted.

    If I also wanted to delete the history delete command then it's a bit more complicated because you need to know the current max history entry.

    You can get this with (there may be a better way):

    history 1 | awk '{print $1}'
    

    Putting it together you can use this to delete a range, and also delete the history delete command:

    for h in $(seq 1006 1008); do history -d 1006; done; history -d $(history 1 | awk '{print $1}')
    

    Wrap this all up in a function to add to your ~/.bashrc:

    histdel(){
      for h in $(seq $1 $2); do
        history -d $1
      done
      history -d $(history 1 | awk '{print $1}')
      }
    

    Example deleting command 4, 5 and 6 (1049-1051) and hiding the evidence:

    [18:21:02 jonathag@gb-slo-svb-0221 ~]$ history 11
     1046  25-04-2016 18:20:47 echo "Command 1"
     1047  25-04-2016 18:20:48 echo "Command 2"
     1048  25-04-2016 18:20:50 echo "Command 3"
     1049  25-04-2016 18:20:51 echo "Command 4"
     1050  25-04-2016 18:20:53 echo "Command 5"
     1051  25-04-2016 18:20:54 echo "Command 6"
     1052  25-04-2016 18:20:56 echo "Command 7"
     1053  25-04-2016 18:20:57 echo "Command 8"
     1054  25-04-2016 18:21:00 echo "Command 9"
     1055  25-04-2016 18:21:02 echo "Command 10"
     1056  25-04-2016 18:21:07 history 11
    [18:21:07 jonathag@gb-slo-svb-0221 ~]$ histdel 1049 1051
    [18:21:23 jonathag@gb-slo-svb-0221 ~]$ history 8
     1046  25-04-2016 18:20:47 echo "Command 1"
     1047  25-04-2016 18:20:48 echo "Command 2"
     1048  25-04-2016 18:20:50 echo "Command 3"
     1049  25-04-2016 18:20:56 echo "Command 7"
     1050  25-04-2016 18:20:57 echo "Command 8"
     1051  25-04-2016 18:21:00 echo "Command 9"
     1052  25-04-2016 18:21:02 echo "Command 10"
     1053  25-04-2016 18:21:07 history 11
    

    The question was actually to delete the last 10 commands from history, so if you want to save a little effort you could use another function to call the histdel function which does the calculations for you.

    histdeln(){
    
      # Get the current history number
      n=$(history 1 | awk '{print $1}')
    
      # Call histdel with the appropriate range
      histdel $(( $n - $1 )) $(( $n - 1 ))
      }
    

    This function takes 1 argument, the number of previous history items to delete. So to delete the last 10 commands from history just use histdeln 10.

提交回复
热议问题