Michael 과 Chris의 답변을 바탕으로 다음을 생각해 냈습니다. ~/.bashrc
파일에 파일 을 추가 한 다음 . ~/.bashrc
또는 로로드하십시오 source ~/.bashrc
.
:<<COMMENT
Deletes all lines from the history that match a search string, with a
prompt. The history file is then reloaded into memory.
Examples
hxf "rm -rf"
hxf ^source
See:
- https://unix.stackexchange.com/questions/57924/how-to-delete-commands-in-history-matching-a-given-string
COMMENT
#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
}
history -d X
. 방금했기 때문에이 질문을 보았습니다history | grep search_str | sort -nr | awk '{print $1}' | while read i; do history -d $i; done
. 오류는 없지만 아무것도 삭제되지 않았습니다. 아무도 이유를 설명 할 수 있습니까?