Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
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
    
Thank you Hai Vu. Please post that as an answer and I will accept it. –  dotancohen Nov 19 '12 at 21:46
add comment

2 Answers

up vote 3 down vote accepted

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.

share|improve this answer
    
Thank you Hai Vu! –  dotancohen Nov 19 '12 at 21:49
add comment

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.

share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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