Skip to content

Instantly share code, notes, and snippets.

@mrcnski
Last active January 17, 2024 18:13
Show Gist options
  • Save mrcnski/f66b99eb202b94f3143fbea1b0f07f87 to your computer and use it in GitHub Desktop.
Save mrcnski/f66b99eb202b94f3143fbea1b0f07f87 to your computer and use it in GitHub Desktop.
Clean Bash History
#!/bin/bash
#
# Cleans up garbage lines and duplicate entries.
#
# DEPENDENCIES:
#
# - rg: cargo install ripgrep
# - unique: go install github.com/ro-tex/unique@latest
# - sponge: brew install moreutils
#
# INSTALLATION:
#
# - Makefile entry:
#
# clean-history:
# ./clean-history.sh "${HOME}/.zsh_history"
#
# - crontab entry:
#
# */2 * * * * source $HOME/.profile && /usr/bin/make -C $HOME/Home/ clean-history
#
# NOTE:
#
# - When cleaning duplicate entries the most recent one is kept, to preserve
# order when searching backwards.
# - This breaks in `make` due to the `#`, hence the stand-alone script.
# - Using `>` would clear the output file first, hence the `sponge`s.
FILE=$1
tail -r "$FILE" | unique -t | rg -v '^\#' | tail -r | sponge "$FILE"
@mrcnski
Copy link
Author

mrcnski commented Mar 31, 2023

Thanks again @ro-tex! unique had appeared in my GitHub feed exactly when I needed it. 👀

@ro-tex
Copy link

ro-tex commented Apr 17, 2023

I'm so happy you find it useful!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment