如何删除shell中最后10条命令的历史logging?
命令如下
511 clear 512 history 513 history -d 505 514 history 515 history -d 507 510 513 516 history 517 history -d 509 518 history 519 history -d 511 520 history  我可以通过history -d 511删除单个history -d 511 ,但是如何删除最后10个命令以及在10个命令历史logging中使用shell中的单个命令? 
我们可以编写一个bash脚本并执行删除历史吗?
你有没有尝试直接编辑历史文件:
 ~/.bash_history 
我的答案是基于以前的答案,但增加了倒序的顺序,使历史项目从最近到最近的删除。
获取您的当前历史logging(调整您想要查看的行数):
 history | tail -n 10 
这给了我一些东西
 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要删除的项目的开始和结束位置。 我要删除条目1006到1008。
 for h in $(seq 1006 1008 | tac); do history -d $h; done 
 这将生成1008的history -d命令,1007然后1006。 
如果我还想删除历史删除命令,那么它会更复杂一点,因为您需要知道当前的最大历史logging条目。
你可以得到这个(可能有更好的方法):
 history | tail -1 | awk '{print $1}' 
把它放在一起可以使用这个来删除一个范围,也可以删除历史删除命令:
 for h in $(seq 1006 1008 | tac); do history -d $h; done; history -d $(history | tail -1 | awk '{print $1}') 
 把这个全部包装到一个函数中,添加到你的~/.bashrc : 
 histdel(){ for h in $(seq $1 $2 | tac); do history -d $h done history -d $(history | tail -1 | awk '{print $1}') } 
删除命令4,5和6(1049-1051)并隐藏证据的示例:
 [18:21:02 jonathag@gb-slo-svb-0221 ~]$ history | tail -n 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 | tail -n 11 [18:21:07 jonathag@gb-slo-svb-0221 ~]$ histdel 1049 1051 [18:21:23 jonathag@gb-slo-svb-0221 ~]$ history | tail -n 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 | tail -n 11 
 问题实际上是从历史logging中删除最近的10条命令,所以如果你想节省一点努力,你可以使用另一个函数来调用为你计算的histdel函数。 
 histdeln(){ # Get the current history number n=$(history | tail -1 | awk '{print $1}') # Call histdel with the appropriate range histdel $(( $n - $1 )) $(( $n - 1 )) } 
 这个函数需要1个参数,以前的历史项目的数量删除。 所以要删除历史上的最后10个命令,只需使用histdeln 10 。 
 for x in `seq $1 $2` do history -d $1 done 
编辑:
改变了支撑迭代器,很好的调用。 另外,用一个反向迭代器调用这个函数。
你可以做这样的事情:
 #!/bin/bash HISTFILE=~/.bash_history # if you are running it in a # non interactive shell history would not work else set -o history for i in `seq $1 $2`; do history -d $i done history -w 
你会在这里唤起你的想法:
 ./nameOfYourScript 563 514 
注意我没有把任何错误检查的边界。 我会把这个作为练习留给读者。
也请看这个问题
 首先,input: history并记下要删除的行号的顺序。 
要清除线路,让我们来说1800行到1815年在terminal写下面的内容:
 $ for line in $(seq 1800 1815) ; do history -d 1800; done 
如果要删除删除命令的历史logging,请为1815 = 1816添加+1,并为该序列添加历史logging+删除命令将被删除。
例如 :
 $ for line in $(seq 1800 1816) ; do history -d 1800; done 
历史-c将清除所有的历史。
结合上面的答案:
 history -w vi ~/.bash_history history -r 
 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}') 
如果要删除10行,只需将N的值更改为10。
一个简单的函数可以杀死所有的数字(虽然它错误)
 kill_hist() { for i in $(echo $@ | sed -e 's/ /\n/g;' | sort -rn | sed -e 's/\n/ /;') do history -d $i; done } kill_hist `seq 511 520` # or kill a few ranges kill_hist `seq 1001 1010` `seq 1200 1201` 
 也许会对某人有用。 
 当您login到任何控制台/terminal的用户时,当前会话的历史logging仅存在于某个“缓冲区”中,该缓冲区会在您登出时刷新到〜/ .bash_history。 
 所以,为了保密,你可以: 
 history -r && exit 
 并且您将会清除所有会话的历史logging(仅限于);)