Skip to content

Instantly share code, notes, and snippets.

@roosto
Last active September 2, 2020 12:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roosto/af674e123f4cd51be776d9d339a59f2f to your computer and use it in GitHub Desktop.
Save roosto/af674e123f4cd51be776d9d339a59f2f to your computer and use it in GitHub Desktop.
func to use in bash to log invocations of `defaults write`
# to be run as your `PROMPT_COMMAND`, this will log any invocations of `defaults write`
# now you stand a chance of figuring out which time when you pasted something from a
# random website into your terminal months ago is now totally screwing up your mac
function __log_defaults_writes {
local DEFAULTS_WRITES_HISTORY="$HOME/.defaults_writes"
if history 1 | grep -F -q 'defaults write'
then
chmod u+w "$HOME/.defaults_writes" # make log file writeable
echo -en "$(date +%Y%m%d.%T):\t"$'\t' >> "$DEFAULTS_WRITES_HISTORY" # add timestamp
history 1 | perl -pe 's/^\s*\d*\s*//' >> "$DEFAULTS_WRITES_HISTORY" # strip history number preamble
# prevent accidental deletion/truncation of the log file and
# make sure log file is readable only by user, in case sensitive data leaks into it
chmod 400 "$HOME/.defaults_writes"
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment