在Linux/Unix中,如何删除shell历史记录中某个命令

时间:2019-11-20 08:53:14  来源:igfitidea点击:

如何删除执行过的某个命令?
如何删除bash历史记录history中的某个命令?
如何从bash历史记录文件中删除命令?

可以使用history命令清除所有历史记录或选定的命令行。

如何查看带行号的命令历史记录?

执行history命令:

$ history

默认情况下,命令历史记录存储在~/.bash_history文件中。
可以通过$HISTFILE进行修改。

echo "$HISTFILE"
printf "Bash history file - %s\n" $HISTFILE

在Linux中如何删除历史记录的某条命令

删除123行的命令:

history -d offset
history -d number
history -d 123

验证一下:

$ history

如何删除所有历史记录?

删除bash的所有命令记录

$ history -c

将以上命令添加到~/.bash_logout文件中, 可以在每次注销时进行清理:

$ cat /dev/null > ~/.bash_history && history -c

设置命令的历史记录数

可以在~/.bashrc中设置,历史命令的记录数:

## 设置历史文件中包含的最大行数
HISTFILESIZE=5000000
 
## 设置要在命令历史记录中记住的命令数
HISTSIZE=10000
 
## 追加历史记录
shopt -s histappend
 
######
# Controlling how commands are saved on the history file ##
# ignoreboth means:                       ##
# a) Command which begin with a space character are not saved in the history list               ##
# b) Command matching the previous history entry  to  not  be  saved (avoid duplicate commands) ##
######
HISTCONTROL=ignoreboth

history命令帮助文档

查看history帮助:

$ help history
history: history [-c] [-d offset] [n] or history -anrw [filename] or history -ps arg [arg...]
    Display or manipulate the history list.
 
    Display the history list with line numbers, prefixing each modified
    entry with a '*'.  An argument of N lists only the last N entries.
 
    Options:
      -c	clear the history list by deleting all of the entries
      -d offset	delete the history entry at offset OFFSET.
 
      -a	append history lines from this session to the history file
      -n	read all history lines not already read from the history file
      -r	read the history file and append the contents to the history
    	list
      -w	write the current history to the history file
    	and append them to the history list
 
      -p	perform history expansion on each ARG and display the result
    	without storing it in the history list
      -s	append the ARGs to the history list as a single entry
 
    If FILENAME is given, it is used as the history file.  Otherwise,
    if $HISTFILE has a value, that is used, else ~/.bash_history.
 
    If the $HISTTIMEFORMAT variable is set and not null, its value is used
    as a format string for strftime(3) to print the time stamp associated
    with each displayed history entry.  No time stamps are printed otherwise.
 
    Exit Status:
    Returns success unless an invalid option is given or an error occurs.