How to get the URL from a firefox tab from the terminal?

There are few files who contain information about your sessions:

  • ~/.mozilla/firefox/*.default/sessionstore-backups/recovery.js which contains information about the current session, also contains information for the closed tabs and the previous session. Every 15 seconds Firefox creates a backup in this file. This file is not available when Firefox is closed.

  • ~/.mozilla/firefox/*.default/sessionstore.js which contains information about the last session, when the Firefox browser is closed. This file is not available when Firefox is open.

  • ~/.mozilla/firefox/*.default/sessionstore-backups/previous.js which contains information about the previous session.

The analysis of the content of recovery.js shows that, for each tab, only the entries of the current URL contain string attributes.


I. When Firefox is open:

1.A. If you want to get all URLs of the open tabs from the current session you can use this command:

cat $HOME/.mozilla/firefox/*.default/sessionstore-backups/recovery.js | \
sed "s/\\_closedTabs.*//" | \
sed "s/{/\n{/g" | \
egrep -o 'url.*attributes' | \
cut -d\" -f3

* Please note that, you must copy/paste all lines together into a terminal window and press Enter.

Where:

  • cat $HOME/.mozilla/firefox/*.default/sessionstore-backups/recovery.js will print the content of this file;
  • sed "s/\\_closedTabs.*//" will remove everything after the string _closedTabs;
  • sed "s/{/\n{/g" | \ will put a newline before each {;
  • egrep -o 'url.*attributes' will filter only those parts of the lines who begin with url and end with attributes. Without -o option, the entire lines who contain the string will be filtered;
  • cut -d\" -f3 will use " as delimiter and will filter only the 3th column.

In my case the output of the command is:

https://askubuntu.com/
https://www.mozilla.org/en-US/

1.B. If you want to get the data for the current and for the previous session at once you could use this:

printf "\n# CurrentSession:\n"; \
cat $HOME/.mozilla/firefox/*.default/sessionstore-backups/recovery.js | \
sed "s/\\_closedTabs/\{\"url\":\"# ClosedTabs:\"attributes/g" | \
sed "s/\\lastSessionState/\{\"url\":\"# LastSession:\"attributes/" | \
sed "s/{/\n{/g" | \
egrep -o 'url":"*.*attributes*' | \
cut -d\" -f3 | \
sed "s/#/\n#/" \
; echo

Where:

  • printf "\n# CurrentSession:\n"; will print # CurrentSession: between two newlines;
  • sed "s/\\_closedTabs/\{\"url\":\"# ClosedTabs:\"attributes/g" will replace the string _closedTabs with "url":"# ClosedTabs:"attributes in the entire "file" (option g);
  • sed "s/\\lastSessionState/\{\"url\":\"# LastSession:\"attributes/" will replace lastSessionState with "url":"# LastSession:"attributes;
  • sed "s/#/\n#/" will put a newline before each #.
  • ; echo will add a blank line at the bottom.

In my case the output of the command is:

# CurrentSession:
https://askubuntu.com/
https://www.mozilla.org/en-US/

# ClosedTabs:
https://www.yahoo.com/

# LastSession:
https://askubuntu.com/
https://www.abv.bg/

# ClosedTabs:
https://www.google.com/gmail/about/
https://www.yahoo.com/

2.A. If you want to get and the history, you could use:

cat $HOME/.mozilla/firefox/*.default/sessionstore-backups/recovery.js | sed "s/\\_closedTabs.*//" | sed "s/{/\n{/g" | egrep 'url":"http*' | cut -d\" -f4

In my case the output of the command is:

https://askubuntu.com/
https://www.google.bg/search?client=ubuntu&channel=fs&q=firefox&ie=utf-8&oe=utf-8&gfe_rd=cr&ei=pTKyWIitGqTs8wewj4KgDQ
https://www.mozilla.org/bg/firefox/new/
https://www.mozilla.org/en-US/

2.B. You can put a separator between the data for each tab:

cat $HOME/.mozilla/firefox/*.default/sessionstore-backups/recovery.js | \
sed "s/\\_closedTabs.*//" | \
sed "s/{/\n{/g" | \
sed "s/entries/url\":\"# TAB:/g" | \
egrep 'url":"*' | \
cut -d\" -f4 | \
sed "s/#/\n#/"

In my case the output of the command is:

# TAB:
about:startpage
https://askubuntu.com/

# TAB:
https://www.google.bg/search?client=ubuntu&channel=fs&q=firefox&ie=utf-8&oe=utf-8&gfe_rd=cr&ei=pTKyWIitGqTs8wewj4KgDQ
https://www.mozilla.org/bg/firefox/new/
https://www.mozilla.org/en-US/

3. 1.B. and 2.B. together:

printf "\n# CurrentSession:\n"; \
cat $HOME/.mozilla/firefox/*.default/sessionstore-backups/recovery.js | \
sed "s/\\_closedTabs/\{\"url\":\"# ClosedTabs:/g" | \
sed "s/\\lastSessionState/\{\"url\":\"# LastSession:/" | \
sed "s/entries/url\":\"# TAB:/g" | \
sed "s/{/\n{/g" | \
egrep 'url":"*' | \
cut -d\" -f4 | \
sed "s/#/\n#/" \
; echo

In my case the output of the command is:

# CurrentSession:

# TAB:
https://host.bg/
https://admin.host.bg/

# TAB:
https://www.mediawiki.org/wiki/MediaWiki

# TAB:
https://en.wikipedia.org/wiki/Main_Page

# ClosedTabs:

# TAB:
about:startpage
https://www.yahoo.com/

# LastSession:

# TAB:
about:startpage
https://askubuntu.com/

# ClosedTabs:

# TAB:
https://www.mozilla.org/en-US/
https://www.google.com/gmail/about/

II. When Firefox is closed:

When Firefox is closed, you can get the data for the last session. The approach is the same as explained above but instead of recovery.js you have to use sessionstore.js (or previous.js):

cat $HOME/.mozilla/firefox/*.default/sessionstore.js \
...

References:

  • Similar question in AskUbuntu;
  • Sed - An Introduction and Tutorial by Bruce Barnett;
  • man sed;
  • man egrep;
  • man cut.

Drag/Drop

Drag it and drop it. Drag it from the icon to the left of the address field. In most browsers you may see an i for the icon.

Copy/Paste

You can also use copy and paste. Copy the text from the address field. Paste it into the terminal.


The accepted answer does not work now, firefox has removed that file.

So here's my solution(s) of it:

Method-1: use keyboard automation tools, such as xdotool, to post keys to copy the url into the clipboard.

This method has some drawbacks, that is, it will change the current focus of the cursor (you need to move to the address bar using Ctrl-L first)

Method-2: use Grease Monkey, bind key F12 to GM_setClipboard(document.location); use keyboard automation tools, send key F12 to firefox; watch the clipboard, save it (maybe even backup and restore the clipboard).