Output URL of open firefox tabs in terminal

I would like to find out the URLs of the currently opened firefox tabs with a terminal command. Is there any possibility?

This way I would be able to write them into a text file and look at them lateron; and safe resources (I often have many open tabs). I know that there is an add-on for firefox, but I would be more confortable writing my own script.


The currently open URLs of the opened Firefox tabs are stored in sessionstore.js file which is located somewhere in $HOME/.mozilla/firefox/XXXXXXXX.default directory.

So, you can start from something like this:

cat $HOME/.mozilla/firefox/*default/sessionstore.js | sed "s/{/\n/g" | egrep -o '"url".*"scroll"' | cut -d\" -f4

Using cat we can display that file, and with the help of sed, egrep and cut we select only the URLs of the opened Firefox tabs from that file.


That information is stored in $HOME/.mozilla/firefox/*default/sessionstore.js and its format is json.

The following example was made to work with PHP. It walks all firefox windows, all tabs and gets the relevant information which is the last entry inside of "entries".

If we could use xpath to parse it, it would be something like: /windows/*/tabs/last()/url (my xpath knowledge is rusty).

cat $HOME/.mozilla/firefox/*default/sessionstore.js | php -r '
$json=json_decode(fgets(STDIN), TRUE);
foreach($json["windows"] as $w)
foreach($w["tabs"] as $t)
echo end($t["entries"])["url"]."\n";'

Or, with perl (but first, sudo apt-get install libjson-pp-perl):

cat $HOME/.mozilla/firefox/*default/sessionstore.js | perl -e '
use JSON qw( decode_json );
my $json = decode_json(<STDIN>);
foreach my $w ( @{$json->{"windows"}} ) {
    foreach my $t ( @{$w->{"tabs"}} ) {
        print $t->{"entries"}[-1]->{"url"}."\n";
    }
}'