Is there a way to get such URL using some sort of commands?
4 Answers
This answer is outdated, and although IMO it is still valuable, please pay attention to the 2022 Answer provided below by @dotancohen.
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 withurl
and end withattributes
. 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" (optiong
);sed "s/\\lastSessionState/\{\"url\":\"# LastSession:\"attributes/"
will replacelastSessionState
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:
-
2I can't find a recovery.js in this folder. There is only a recovery.jsonlz4 which also does not work.– NashNov 30, 2018 at 11:53
2022 Answer
As Bao mentions, the top-voted answer no longer works in modern Firefox because the file it depends on has two format changes:
- It is a compressed file now, so we use
lz4jsoncat
to read it. - The method to identify the current tab and history item in that file is different. Now we must sort the tabs on
lastAccessed
, and then find the correct history item from theindex
field. I preferjq
for that.
This one-liner should do it:
lz4jsoncat ~/.mozilla/firefox/*.default-release/sessionstore-backups/recovery.jsonlz4 | jq -r ".windows[0].tabs | sort_by(.lastAccessed)[-1] | .entries[.index-1] | .url"
To install the prerequisite tools on Ubuntu, run this:
$ sudo aptitude install jq lz4json
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.
-
6Im noy sure myself, but maybe OP meant to ask: how to get URL from Firefox tab from terminal Feb 25, 2017 at 21:48
-
@M.Becerra I would think
from
andterminal
would be closer related such asfrom terminal
. It would also seem likely there would have been a reference ofto Firefox
. But just in case, to load a URL from the terminal, you hover the mouse cursor over the url, right click and clickOpen Link
. This will open in a default browser... not necessarily into a tab, if the browser isn't already opened. It also appears that it would be obvious to copy and paste URL's into the address field of Firefox, the same way you would from a word processor's text. Feb 25, 2017 at 21:58 -
@L.D.James I probably should have asked "How to get the URL from a firefox tab in the terminal using a command(s)?". I'm sorry if the question wasn't clear. Feb 27, 2017 at 17:09
-
@M.Becerra No, this answer captures the semantics of my question but it's not what I was looking for(See my other comment). I'm sorry if the question was ambiguous. Feb 27, 2017 at 17:13
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).