Skip to content

Instantly share code, notes, and snippets.

@Jeff-Russ
Last active June 18, 2020 21:01
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 Jeff-Russ/215387f9c06f827f8623d186e308e77b to your computer and use it in GitHub Desktop.
Save Jeff-Russ/215387f9c06f827f8623d186e308e77b to your computer and use it in GitHub Desktop.
Show recent Safari history with timestamps (command line function)

Show Recent Safari History with Timestamps

This creates a command for you to run in the terminal (or similar) zsh shell terminal app. I believe this should work in bash terminals as well. Once you're done setting it up you simply type

Jeff@MBP ~ % safariHist 8 
Thu Jun 18 16:50:59 EDT 2020 | YouTube
Thu Jun 18 16:50:51 EDT 2020 | Browse All of Google's Products & Services - Google
Thu Jun 18 16:50:49 EDT 2020 | Browse All of Google's Products & Services - Google
Thu Jun 18 16:50:47 EDT 2020 | Google - About Google, Our Culture & Company News
Thu Jun 18 16:50:30 EDT 2020 | Google
Thu Jun 18 16:50:30 EDT 2020 | Google
Thu Jun 18 16:50:19 EDT 2020 | Jeff-Russ (Jeffrey Russ)
Thu Jun 18 16:49:49 EDT 2020 | Netflix

The optional argument passed to the safariHist command determines how many entries, i.e. how far back, you want displayed. Without specifying, the previous 100 entries will be shown.

Installing

Paste this into ~/.zshrc or similar. Read comments in code below for more instruction.

function safariHist () {
    # this function requires that the terminal app have full disk access. To do this:
    # System Preferences > Security and Privacy > Privacy (tab) > Full Disk Access 
    # then unlock (bottom left) and select Terminal. If you don't see Terminal use '+' button.
    if (( $# == 0 ))
    then entries=100
    else entries=$1
    fi
    sqlite3 ~/Library/Safari/History.db 'select visit_time,title from history_visits order by visit_time desc;' \
    | while read i; do d="${i%%.*}"; echo "$(date -r $((d+978307200))) | ${i#*|}"; done \
    | head -n $entries
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment