Keep Track of Defaults Write Commands Used in Mac OS X Automatically

Apr 3, 2013 - 11 Comments

defaults write

If you like to tweak Mac OS X with a lot of defaults write commands from the terminal, you already know how hard it can be to keep track of them. Sure you can query command history for specific command syntax, and you can always use grep to find executed defaults commands, but there’s a better way to keep track of them all, and that’s by keeping an automatically updated text file that stores a list of all used defaults tweaks. This makes it very easy to keep track of which defaults commands have been activated or disabled on a specific Mac.

This tutorial walks you through how to automatically track every defaults write and all other defaults command strings entered on a Mac by generating a text file that specifically tracks those command executions. This is hugely useful for those who tinker defaults commands often and either forget what is enabled or disabled, or just want a running tally of all defaults changes on a Mac.


The other advantage to this trick is that it keeps the list completely separated from general command history, meaning if command history is cleared the defaults list will continue to be around for future reference.

How to Track All defaults Commands Used on Mac OS, Automatically

We’re assuming you’re familiar with the Terminal app so open that up to get started.

Use a command line text editor that you are comfortable with, we’ll stick with nano since it is simple and fairly user friendly:

nano ~/.bash_profile

Paste in the following string on a new line within .bash_profile

PROMPT_COMMAND='echo "$(history 1 | grep "defaults")" | sed '/^$/d' >> ~/Documents/defaults.txt'

Note the standard location for the defaults list file is the user home directory ~/Documents/ folder in a file called “defaults.txt”, feel free to change that if desired.

Save the document by hitting Control+O then exit out of nano by hitting Control+X

The document called ‘defaults.txt’ will be generated the first time the string ‘defaults’ has been detected in command execution. Each new defaults entry will be added to a numerical list added on it’s own line.

This is perhaps best enabled fresh after a restore or right away on a new Mac, that way the defaults.txt file will contain the full list of all defaults commands ever used on the given Mac.

After it’s been around long enough to track a few defaults commands, opening the file will look something like this:

used defaults write commands list

If you use cat to view the file, you may see something like this:

501 cat ~/Documents/defaults.txt
502 defaults read com.apple.Finder
503 defaults write com.apple.dock springboard-rows -int 4
504 defaults write com.apple.dock springboard-columns -int 4;killall Dock
505 defaults read /Library/Preferences/SystemConfiguration/com.apple.airport.preferences RememberedNetworks
506 defaults write com.apple.systemsound "com.apple.sound.uiaudio.enabled" -int 1
507 defaults read com.apple.systemsound
508 defaults write com.apple.systemsound "com.apple.sound.uiaudio.enabled" -int 0
509 tail -f ~/Documents/defaults.txt

As mentioned, it will grab anything with ‘defaults’ in the command syntax, which includes using cat, tail, nano, and anything else on the defaults.txt file itself. Additionally, it will keep track of not only the changes made with defaults write, but also anytime a defaults command has been read with defaults read, or removed with defaults delete commands.

How to Limit the Defaults Tracker to “defaults write” Only

If you’d rather exclusively see ‘defaults write’ strings, use the following in .bash_profile instead:

PROMPT_COMMAND='echo "$(history 1 | grep "defaults write")" | sed '/^$/d' >> ~/Documents/defaults-write.txt'

Whichever you use, the resulting file is a generic text document, and it can also be opened in nano, vi, TextEdit, TextWrangler, BBedit, emacs, or whichever the preferred client is. This makes it easy to not only keep track of for systems administration purposes, but also for sharing lists with friends and colleagues.

Thanks to Mike for leaving this excellent trick in our comments.

.

Related articles:

Posted by: Paul Horowitz in Command Line, Mac OS, Tips & Tricks

11 Comments

» Comments RSS Feed

  1. I coulnd’t get the described solution to work for me on ZSH. So here is a combined solution from the post and the comments that works for me:

    inside .zshrc
    “`
    defaults() {
    if [[ $1 == ‘write’ ]]; then
    echo “defaults $@” >> ~/Dropbox/OSx/settings/defaults
    sort -uo ~/Dropbox/OSx/settings/defaults ~/Dropbox/OSx/settings/defaults
    fi

    /usr/bin/defaults $@
    }
    “`

  2. Mark says:

    I like this tip and took it a small step further by having it managed across multiple Macs via Dropbox:

    PROMPT_COMMAND=’echo $(history 1) | grep “defaults write” | sed -E “s/^[[:digit:]]+ //” >> ~/Dropbox/App/defaults-write.txt && sort -uo ~/Dropbox/App/defaults-write.txt ~/Dropbox/App/defaults-write.txt’

  3. Jerry Leichter says:

    BTW, following up on the previous note: Since what is written to the defaults.txt file is now entirely controlled by the function, you can change it. For example, if you instead use:

    echo `date` “$@” >> ~/Documents/defaults.txt

    then a date/time stamp will appear at the beginning of every line. (The back ticks “`” – *don’t* use single quote; this is the character on the key at the upper left on most keyboards – causes the stuff inside to be interpreted as a command on its own, and whatever it write to the screen will instead get substituted into the “echo” command line.)

    Unlike the hint’s change, this change will work even for “defaults” commands inside of scripts.

    — Jerry

  4. Jerry Leichter says:

    This approach is overkill. The shell – the program that interprets the command line – allows you to define your own commands, or redefine existing ones. Instead of the hint’s proposal – which looks back at the last line in the history and saves it if it was a “defaults” command – you can instead add the following to your .bash_profile:

    function defaults ()
    { echo “$@” >> ~/Documents/defaults.txt
    /usr/bin/defaults “$@”
    }

    “function” introduces a function definition. The “$@” is a special “magic” construct that passes along all the parameters that appeared on the command line after the “defaults” command; the “echo” line appends (“>>”) to the named file. Then we execute the actual command. We can’t just use “defaults” here or we’d just execute the same function again; so instead we explicitly name the actual command by full path. I determined this path by typing:

    whereis defaults

    to a command line prompt.

    — Jerry

  5. jeff says:

    might be useful to change

    nano .bash_profile
    to
    nano ~/.bash_profile
    ?

    i’m almost never in the gui user’s home.

  6. Mike Steebs says:

    Great tip. Must have on first boot!

  7. Lexxie says:

    I made a spelling mistake. Works!

  8. Lexxie says:

    the .bash_profile line for all history, does indeed create a defaults.txt file in Dosuments, but does not write anything in it.

Leave a Reply

 

Shop on Amazon.com and help support OSXDaily!

Subscribe to OSXDaily

Subscribe to RSS Subscribe to Twitter Feed Follow on Facebook Subscribe to eMail Updates

Tips & Tricks

News

iPhone / iPad

Mac

Troubleshooting

Shop on Amazon to help support this site