Skip to content

Instantly share code, notes, and snippets.

@snowman11784
Created July 13, 2023 09:53
Show Gist options
  • Save snowman11784/e6cb616359e1b4d056a35d8f489f8449 to your computer and use it in GitHub Desktop.
Save snowman11784/e6cb616359e1b4d056a35d8f489f8449 to your computer and use it in GitHub Desktop.
Remove history entries in bulk
#!/usr/bin/env bash
#################################
# Remove history entries in bulk
#################################
run() {
echo "$@"
if [[ ! $DRY_RUN == true ]]; then
"$@"
fi
}
rmhist() {
local lines=($1)
history -r
for line in "${lines[@]}"; do
run history -d $line
if [[ $? -eq 0 ]]; then
run history -w
else
echo "history deletion failed for $line"
continue
fi
done
}
DEBUG=0
VERBOSE=0
unset OPTIND
unset OPTARG
while getopts "dhv" opt; do
case "${opt}" in
d)
DRY_RUN=true
DEBUG=1
;;
h)
usage
exit 1
;;
v)
set -x
VERBOSE=1
;;
esac
done
shift $((OPTIND-1))
HISTFILE=~/.bash_history
rmhist "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment