Linux 如何删除shell中最后10个命令的历史记录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14750650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to delete history of last 10 commands in shell?
提问by Justin John
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 history
519 history -d 511
520 history
I can delete single one by history -d 511
, but how to delete last 10 commands and in between 10 commands history using single command in shell?
我可以删除单个history -d 511
命令,但是如何在 shell 中使用单个命令删除最后 10 个命令和 10 个命令之间的历史记录?
Can we write a bash script and execute for deletion of history?
我们可以编写一个bash脚本并执行删除历史记录吗?
回答by WheretheresaWill
Have you tried editing the history file directly:
您是否尝试过直接编辑历史文件:
~/.bash_history
回答by Tyler Jandreau
edit:
编辑:
Changed the braced iterators, good call. Also, call this function with a reverse iterator.
改变了支撑迭代器,很好的调用。此外,使用反向迭代器调用此函数。
You can probably do something like this:
你可能可以做这样的事情:
#!/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 `;
do
history -d $i
done
history -w
Where you will evoke like this:
你会像这样唤起:
./nameOfYourScript 563 514
Notice I haven't put any error checking in for the bounds. I'll leave that as an exercise to the reader.
请注意,我没有对边界进行任何错误检查。我将把它留给读者作为练习。
see also this question
另见这个问题
回答by Barmar
for x in `seq `
do
history -d
done
回答by Ghassan Zein
First, type: history
and write down the sequence of line numbers you want to remove.
首先,键入:history
并写下要删除的行号序列。
To clear lines from let's say line 1800 to 1815 write the following in terminal:
要清除从 1800 行到 1815 行的行,请在终端中写入以下内容:
$ for line in $(seq 1800 1815) ; do history -d 1800; done
If you want to delete the history for the deletion command, add +1 for 1815 = 1816 and history for that sequence + the deletion command will be deleted.
如果要删除删除命令的历史记录,则为 1815 = 1816 添加 +1,该序列的历史记录 + 删除命令将被删除。
For example :
例如 :
$ for line in $(seq 1800 1816) ; do history -d 1800; done
回答by JGC
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.
选择要删除的项目的开始和结束位置。我要删除条目 1006 到 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.
这将生成history -d
1006 的命令,然后 1007 变成 1006 并且 1006 被删除,然后 1008(变成 1007)现在是 1006 并被删除。
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.
如果我还想删除 history delete 命令,那就有点复杂了,因为您需要知道当前的最大历史条目。
You can get this with (there may be a better way):
你可以用(可能有更好的方法):
history 1 | awk '{print }'
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 }')
Wrap this all up in a function to add to your ~/.bashrc
:
将所有这些都包装在一个函数中以添加到您的~/.bashrc
:
histdel(){
for h in $(seq ); do
history -d
done
history -d $(history 1 | awk '{print }')
}
Example deleting command 4, 5 and 6 (1049-1051) and hiding the evidence:
删除命令 4、5 和 6 (1049-1051) 并隐藏证据的示例:
[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.
问题实际上是从历史记录中删除最后 10 个命令,因此如果您想节省一点精力,您可以使用另一个函数来调用histdel
为您进行计算的函数。
histdeln(){
# Get the current history number
n=$(history 1 | awk '{print }')
# Call histdel with the appropriate range
histdel $(( $n - )) $(( $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
.
此函数接受 1 个参数,即要删除的先前历史记录项的数量。因此,要从历史记录中删除最后 10 个命令,只需使用histdeln 10
.
回答by user6280539
history -c will clear all histories.
history -c 将清除所有历史记录。
回答by Wasef Anwar
for h in $(seq $(history | tail -1 | awk '{print -N}') $(history | tail -1 | awk '{print }') | tac); do history -d $h; done; history -d $(history | tail -1 | awk '{print }')
If you want to delete 10 lines then just change the value of N to 10.
如果要删除 10 行,只需将 N 的值更改为 10。
回答by steveo'america
A simple function can kill all by number (though it barfs on errors)
一个简单的函数可以按数字杀死所有人(尽管它会导致错误)
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`
回答by Bryant Bernstein
Combining answers from above:
结合上面的答案:
history -w
vi ~/.bash_history
history -r
history -w
vi ~/.bash_history
history -r
回答by Alex
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 ;)
也许会对某人有用。
当您登录当前会话的任何控制台/终端历史记录的任何用户时,仅存在于某些“缓冲区”中,该缓冲区在您注销时刷新到 ~/.bash_history 。
因此,为了保密,您可以:history -r && exit
并且您将被注销,并清除所有会话的(且唯一的)历史记录;)