When I have a gnome terminal open, is there a command that will allow me to programmatically enquire of this gnome terminal:

  1. How many tabs the gnome terminal has open on it?

  2. What is the pwd of each of these tabs?

I lose my virtual machine in the evening, so I want to write a script to store the above information, and then programmatically reopen all the gnome terminals the next day, upon log in.

  • Processes have working directories. A tab on a GUI window is associated with at least one process. When it's a terminal emulator, it's a minimum of two, the terminal emulator on the front-end of the pseudo-terminal and whatever processes are on the back end. There could be more than one process on the back end. Which process's working directory do you want?
    – JdeBP
    Commented Sep 8, 2020 at 16:10
  • Related questions, suffering much the same problems as this one, are of course unix.stackexchange.com/q/278652/5132 and unix.stackexchange.com/q/379690/5132 .
    – JdeBP
    Commented Sep 8, 2020 at 16:11

1 Answer 1


You can enumerate the open windows and tabs using dbus; going straight for tabs:

dbus-send --session --dest="org.gnome.Terminal" --print-reply /org/gnome/Terminal org.freedesktop.DBus.ObjectManager.GetManagedObjects | grep -c /screen/

will tell you how many are open.

If your shell cooperates with VTE, GNOME Terminal will knows about the last current directory for each running shell in your tab (not the current directory of the leaf process running in each tab, if it’s not the shell); but I don’t think there’s a way to retrieve that over the bus.

What you can do is list the current working directories of all the “leaf” processes whose parent is GNOME Terminal:

pstree -T -p $(pgrep gnome-terminal) |
sed -E 'sX^.*\(([0-9]+)\)$X/proc/\1/cwdX' |
xargs readlink -f

This won’t tell you which tab is where but I’m not sure that’s significant (as far as I can tell, there’s no way to determine what each tab is running). You can use variations of this to find out what is running too, not just the current working directories.

  • Thanks. I can actually list the windows. What I cant do yet is get them to tell me how many tabs they have and what are the pwds of these tabs. I can programatically gnome terminals easily with as many tabs as wanted, all in the directories wanted, and even on the desired virtual desktop. But getting that information from an open gnome terminal is the bit i cant do yet. The terminal itself must know all this information, and surely there are commands to access it? Commented Sep 8, 2020 at 18:36
  • The command in the answer lists tabs, not windows. See the update to list the current working directories... Commented Sep 8, 2020 at 21:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .