マイケルとクリスの答えに基づいて、私は次のことを思いつきました。~/.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
。エラーはありませんが、削除されたものはありません。誰もが理由を説明できますか?