Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

During my multitasking days, sometimes I will be booted off of an SSH session for one reason or another (idle timeouts, etc), while I am working on another task. When I return to the terminal, I will find myself back at localhost, needing to SSH back to the server. Once back at the server, I'm always in my ~ home directory by default and I need to navigate back to the directory I was at last (if I can remember it) in order to continue working.

Is it possible to automatically return to the last directory I was working in when I login?

I was thinking a homebrew solution would be aliasing cd to change directories but also save that directory into some environment variable, and then changing to that directory in my .bash_profile.

But is there any sort of functionality like this already in Linux?

share|improve this question

Instead of solving the cd persistence problem, you should consider solutions to resume your shell session.

  • tmux
    • Inside SSH, run tmux to start a new session
    • If disconnected, SSH in again, and run tmux attach to resume the session
    • Inside a tmux session, you can press CtrlB, then D to detach
  • screen
    • Inside SSH, run screen to start a new session
    • If disconnected, SSH in again, and run screen -r to resume the session
    • Inside a screen session, you can press CtrlA, then D to detach
  • mosh
    • mosh is an SSH replacement that runs over UDP and is designed to be resistant to network disruptions
share|improve this answer
+1 for adding "mosh". Looks very interesting. "byobu" could be added on top of screen. – initall Sep 8 '13 at 17:46
screen is interesting. I noticed that if I run a long process in a remote SSH window, then close the session, reopen it and go screen -r, it goes right back to where I left off, as if the process was still running. Is this sort of like a NOHUP thing? I can execute a long process and then don't worry about leaving the terminal open? – Jake Wilson Sep 8 '13 at 19:42
@jakobud exactly. Things inside screen keep running after you disconnect. – Grant Sep 8 '13 at 19:48

Your Answer


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.