Suppose that I am running screen on a remote server with four open screens. Is there a quick way to cd all the screens to the working directory of the currently-open screen?

  • 2
    Here is a work-around: unix.stackexchange.com/questions/41024/… – Hai Vu Nov 19 '12 at 21:12
  • What you're asking is hard to achieve smoothly. So, perhaps you can tell us why you want the behavior and we can give you a good solution to that problem instead. – Omnifarious Nov 19 '12 at 21:27

Here is a work-around: on one tab, record the CWD into a temp file, on the other tabs, cd to the just-saved dir. I would put these two aliases to my .bashrc or .bash_profile:

alias ds='pwd > /tmp/cwd'
alias dr='cd "$(</tmp/cwd)"'

the ds (dir save) command marks the CWD, and the dr (dir recall) command cd to it. You can do something similar for C-shell.


The answer is basically "No". Each of the programs running in each screen are separate processes with their own working directory. It is not possible to change the working directory of another process. A process can only change its own working directory.

That being said, the workaround that Hai Vu pointed you at will work. It basically stores the working directory in a file, then you have to go to each other window and run a different command that reads that file in and sets the current working directory based on it.

There might be some ugly trick you could pull with PROMPT_COMMAND to make bash automatically change the working directory if the contents of the file have changed. That wouldn't take effect until you'd run at least one command though.

Perhaps if you gave the reason you wanted this behavior a solution to that problem could be given instead.

  • Often I will cd into a directory and do a few things there: edit a file or three, read the PHP error log in that directory, run a perl script. This is just to get the initial setup going. Hai Vu's solution is fine for the purpose that I need. – dotancohen Nov 19 '12 at 21:47

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.