Ubuntu – How to save working directory in gnome-terminal

command linegnome-terminalscripts

How can I save the current working directory in different tabs in a gnome-terminal? I am looking for a script or a command, that will save the working directory of each open tab, without having to manually go through all tabs and type pwd in the terminal window..

I am using Ubuntu 14.04, and gnome-terminal version 3.6.2.

I tried to save the current state using gnome-terminal --save-config=conf.cfg

The file conf.cfg will in some cases contain an entry like:
WorkingDirectory=/home/hakon/mydir. However, that directory is not necessarily the current working directory of the terminal window. It seems it is related to the command line option --working-directory.. so it will only represent the working directory if I do not change it after the terminal window has opened..

Best Answer

This can be done by changing the custom command of gnome-terminal to

bash -c 'export PROMPT_COMMAND="echo -ne \033]0;$PWD\007"; exec bash'

then each time you open a new tab the title of the terminal window will be set to the current working directory of the tab.

Then, to save the current working directory of all tabs, you must determine the number of currently opened tabs. This can be done with

gnome-terminal --save-config=config.cfg

and inspecting the saved file config.cfg. Then iterate through all tabs using

xdotool key ctrl+Page_Down

and save the window title (which is equal to the current working directory) with

xprop -id $WINDOWID WM_NAME