从历史记录中删除给定命令的所有出现

从历史记录中删除给定命令的所有出现

我正在尝试删除历史记录中的特定条目。澄清——我的意思是删除所有包含给定命令/或文本的事件。我使用了 Aliteralmind 的 hxf 脚本:

:<

Examples
    hxf "rm -rf"


#The unalias prevents odd errors when calling". ~/.bashrc" (May result in
#"not found" errors. That's okay).
unalias hxf

hxf()  {
read -r -p "About to delete all items from history that match \"$1\".      
Are you sure? [y/N] " response
response=${response,,}    # tolower
if [[ $response =~ ^(yes|y)$ ]]
then
    #Delete all matched items from the file, and duplicate it to a temp
    #location.
    echo -e "grep -v \"$1\" \"$HISTFILE\" > /tmp/history"
    grep -v "$1" "$HISTFILE" > /tmp/history

    #Clear all items in the current sessions history (in memory). This
    #empties out $HISTFILE.
    echo "history -c"
    history -c

    #Overwrite the actual history file with the temp one.
    echo -e "mv /tmp/history \"$HISTFILE\""
    mv /tmp/history "$HISTFILE"

    #Now reload it.
    echo -e "history -r \"$HISTFILE\""
    history -r "$HISTFILE"     #Alternative: exec bash
else
    echo "Cancelled."
fi
}

(从https://unix.stackexchange.com/a/178956/115826

但是运行 hxf 脚本后,历史记录给了我:(例如)

401 254: 263: 287: 288: 293: cd ~
402 203: 243: 290: 309: 315: 332: 370: 377: 389: 400: ls
403 200: 207: 255: 263: 302: 325: 359: 378: 385: 393: 397: 399: nedit

如何修改 hxf() 脚本以消除显示每次命令出现的索引列表的历史记录?运行 hxf() 后我得到了上面的结果。在运行它之前,历史给出了:

401 cd ~
402 ls
403 nedit

条目列表很快就会变得很长并且难以阅读。我需要像运行 hxf 之前一样显示历史输出,唯一的区别是我用 hxf() 删除的命令现在已删除。

答案1

如果您询问如何从历史记录中删除某个命令:

历史-d(数字)

从运行“历史记录”中获取数字

相关内容