I want to login to a remote Linux machine via SSH and to be in the same directory I was before my last log out.
Since my login shell is bash, I present this solution in bash, but you can adapt it for your favorite shells. This tip relies on the two special files ~/.bash_profile and ~/.bash_logout. When a user logs out of a Linux system, the login shell (bash in this case) executes the ~/.bash_logout file, which is where we save our working directory:
# Contents of .bash_logout rm -f $LASTDIRFILE echo cd $PWD > $LASTDIRFILE
Likewise, we a user logs in, bash execute .bash_profile, so we put the instruction to restore the working directory there:
# Contents of .bash_profile # ... # Restore last directory export LASTDIRFILE=~/.lastdir test -f $LASTDIRFILE && source $LASTDIRFILE
The ~/.bash_logout is a wonderful file for saving your session’s details and its counterpart, ~/.bash_profile, is good for restoring them.