+++
title = "Count your opened Firefox tabs with bash"
date = "2025-02-26"
updated = "2025-02-26"
[extra]
authors = ["Ydreniv"]
+++

At any point in time, I have many Firefox tabs opened.
I might even have reached a thousand.
This feels like an issue I should address at some point, and I have already
tried.
But today, my goal is easier : count the number of currently opened tabs.
In truth, it’s fairly easy : many extensions provide this information, in more
or less granular details (number of windows, number of tabs in a specific
window…).
However neat this is, I want to get my total number of opened tabs printed in
a Unix shell.
Indeed, I ultimately would like to automatically graph this number over time.

Before I started, I figured I’d need to find a session file, list the tabs,
and get the count.
Sounds fairly easy right ?
Well it was not too hard in the end, but there were a couple bumps.
The first is the format used to store the sessions.
It’s compressed using LZ4, but with headers set by Mozilla, before LZ4 was
fully standardized.
[This StackOverflow](https://superuser.com/a/1363751) answer gives a bunch of
tools to decompress the file.
I’ve chosen to use [andikleen’s tool](https://github.com/andikleen/lz4json.git)
as it worked out of the box easily.
Once in possession of the extracted JSON, I’ve used `jq` to get the tabs’ IDs, and
`wc` to count them.

So with `./lz4jsoncat /home/<user>/snap/firefox/common/.mozilla/firefox/<profile-id>.default-release/sessionstore-backups/recovery.jsonlz4 | jq '.windows[].tabs[].index' | wc -l`,
I’ve gotten my answer in a shell : 138 (yikes !).
I’m using only one Firefox profile, but I suppose you could adapt this shell
pipe to support more.
Anyway, I’ll need to work on a cronjob to do it regularly, and an agent to
collect these data (probably Prometheus).
This would be one of the first metrics I’d collect for my "local computer
monitoring center".

