Linux server remembering last used folder

I’m administrating many servers – usually each for different purpose.

When I log to a server very often I need to work at last folder where I was last time – 80% cases. But Linux kicks me to my home directory, which is the last one where I want to be.

Following script will remember where you was and after login it will move you back to that folder. Put it into ~/.bashrc or /etc/bashrc

function mycd() {
 cd $1
 pwd > ~/.pwd
alias switch="cd $(cat ~/.pwd)"
alias cd='mycd'


How it works:

1) function mycd must be defined at bashrc file to so the command cd will apply to your shell. If you will create small script and call it, it will not work.

2) at the function mycd you execute cd and the you store actual path to file ~/.pwd

3) at the start of the bash, you first restore your path by loading content of ~/.pwd and then you replace cd command with mycd.


It’s so simple, but it saves time.


Leave a Reply

Your email address will not be published. Required fields are marked *