Checking Firefox bookmarks on OS X from a shell prompt

I needed to be able to retrieve a Firefox bookmark for an account on a MacBook Pro laptop running OS X Yosemite (10.10.5). I wanted to be able to view the bookmarks from a command line interface (CLI), i.e., a shell prompt that I would get in a Terminal window. On an OS X system, the bookmarks and other account-unique information is stored beneath the profile directory for the account, which you can find beneath the ~/Library/Application Support/Firefox/Profiles directory.
$ ls ~/Library/Application\ Support/Firefox/Profiles
bgq13udo.default
$

Within that directory is a places.sqlite SQLite database. OS X comes with the sqlite3 program for creating, viewing, and editing SQLite databases - see Using SQLite for information on how to use the program.

You can see the tables within the database by issuing the command .tables. One of the tables is moz_bookmarks.

$ sqlite3 ~/Library/Application\ Support/Firefox/Profiles/bgq13udo.default/places.sqlite
SQLite version 3.8.5 2014-08-15 22:37:57
Enter ".help" for usage hints.
sqlite> .tables
moz_anno_attributes  moz_favicons         moz_items_annos    
moz_annos            moz_historyvisits    moz_keywords       
moz_bookmarks        moz_hosts            moz_places         
moz_bookmarks_roots  moz_inputhistory   
sqlite>

You can see the layout of that table by using the .schema command.

sqlite> .schema moz_bookmarks
CREATE TABLE moz_bookmarks (  id INTEGER PRIMARY KEY, type INTEGER, fk INTEGER DEFAULT NULL, parent INTEGER, position INTEGER, title LONGVARCHAR, keyword_id INT
EGER, folder_type TEXT, dateAdded INTEGER, lastModified INTEGER, guid TEXT);
CREATE INDEX moz_bookmarks_itemindex ON moz_bookmarks (fk, type);
CREATE INDEX moz_bookmarks_parentindex ON moz_bookmarks (parent, position);
CREATE INDEX moz_bookmarks_itemlastmodifiedindex ON moz_bookmarks (fk, lastModified);
CREATE UNIQUE INDEX moz_bookmarks_guid_uniqueindex ON moz_bookmarks (guid);
sqlite>

You can join that table with the moz_places table to see a list of your bookmarks using the SQL command below:

SELECT h.url, b.title FROM moz_places h JOIN moz_bookmarks b on h.id = b.fk;

In this case I needed just the URL for one particular bookmark with a title that contained the word "portal", which I was able to find with the SQL command below:

sqlite> SELECT h.url, b.title FROM moz_places h JOIN moz_bookmarks b on h.id = b.fk WHERE b.title LIKE "%portal%";
https://codexcmportal/|Code X Configuration Management Portal
sqlite>

The syntax of the command to use to search for a particular bit of text in a record in a table is as follows:

SELECT * FROM [table] WHERE [field] LIKE '%stringtosearchfor%';

The SQL LIKE operator can be used to locate records in a table that match a given string. The percent sign can be used as a wildcard character, i.e., in this case the % before "portal" represents any number of characters that might appear before the word and the % after "portal" represents any number of characters that may appear afterwards, allowing me to find the bookmark URL I needed when I knew that "portal" appeared in it, but couldn't recall it exactly.

Another alternative for viewing the database tables on an Apple system is to use DB Browser for SQLite on OS X. The software is available for Microsoft Windows systems as well at DB Browser for SQLite.

References:

  1. Firefox Bookmarks SQLite structure
    Asked: January 21, 2009
    Stack Overflow

 

TechRabbit ad 300x250 newegg.com

Justdeals Daily Electronics Deals1x1 px