Skip to content

Instantly share code, notes, and snippets.

@kentfrazier
Created June 4, 2020 17:03
Show Gist options
  • Select an option

  • Save kentfrazier/f285d3f3eaf720c66d77b1dea3f15e89 to your computer and use it in GitHub Desktop.

Select an option

Save kentfrazier/f285d3f3eaf720c66d77b1dea3f15e89 to your computer and use it in GitHub Desktop.
Improved Multiprocess Bash History Management
# --- History File Management --- #
HISTDIR="$HOME/.history"
if [[ ! -d "$HISTDIR" ]]; then
mkdir "$HISTDIR"
chmod 0700 "$HISTDIR"
fi
HISTTIMEFORMAT='%F %T '
HISTFILE="$HISTDIR/$(date -j +'%Y-%m-%dT%H:%M:%S')" # Make process-specific history file
HISTFILESIZE=0 # close any old history files
HISTFILESIZE=4096 # and set a large new size
HISTSIZE=4096
hist-search () {
if [ $# -lt 1 ]; then
echo 'Provide one or more patterns to search for.'
return 1
fi
local cmd="grep -r '$HISTDIR' -e '$1' -H --color=never"
shift
for pattern in "$@"; do
cmd="$cmd | grep -e '$pattern' --color=never"
done
echo "$cmd"
eval "$cmd"
}
hist-clean () {
time_clause="${1:-4w}"
find_cmd="find $HISTDIR -type f -mtime +$time_clause"
echo $find_cmd
$find_cmd -print0 | xargs -0 ls -lt
read -p "Delete these files? " -n 1 -r
echo
if [[ "$REPLY" =~ ^[Yy]$ ]]; then
$find_cmd -delete
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment