2

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..

3
  • So you want to save a bunch of n tabs once and their current pwds so that every time you open a terminal, it will automatically load n new tabs within the respectively saved directory? Or do you want to continuously save the pwds every time you close the window? And do you just want to save the info somewhere or instead reopen the tabs whenever you launch a terminal window? Please clarify this.
    – Byte Commander
    Apr 6, 2015 at 17:25
  • Thanks, I see there is some missing information in my question... that the format of the saved information should be specified. Maybe, such that it is possible to determine which pwd corresponds to the left-most tab and so on.. But I could start with the case where I issue a command save-pwds from any terminal tab in the terminal window (assuming I only have one window) and just saves a file with one pwd per line in any order.. Apr 6, 2015 at 17:35
  • 1
    Then you might have to reword your question. The command to save the current working directory is pwd >> /home/YOURUSERNAME/workingdirs.txt It will append a line containing the path to the specified file (workingdirs.txt in your home directory). If the file doesn't exist, it will be created. So you can delete it (rm /home/YOURUSERNAME/workingdirs.txt) before running the script to save a new load. Now the only problem is that I could not find a way to access the terminal window's tabs and run that command in each of them instead of in a new one. This approach will break if a tab is busy
    – Byte Commander
    Apr 6, 2015 at 19:26

1 Answer 1

1

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

You must log in to answer this question.

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