0

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 Sep 8 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 Sep 8 at 16:11
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.

| improve this answer | |
  • 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? – Rory Cornish Sep 8 at 18:36
  • The command in the answer lists tabs, not windows. See the update to list the current working directories... – Stephen Kitt Sep 8 at 21:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.