Synchronize Directories in Bash Shell

The Problem

I often work in a Linux environment with multiple windows open in a tmux session. One of the pattern I see often is the need to navigate to the same directory for multiple windows. For example, in window 1, I navigated to a directory deep within my project:

$ cd ~/long/path/to/my/directory

Then, on window 2, I want to navigate to the same directory. This often involves either copy the command from window 1 and paste into window 2, or retype the same command again. There must be a better way.

The Solution

To solve this problem, I created two bash aliases: here and there. The here alias marks the current location and the there alias jumps to that location:

alias here='echo cd $PWD >~/.here.sh'
alias there='source ~/.here.sh'

With this pair of commands, from window 1, I would navigate to where I want and issue the here command to mark. Then, on window 2, I issue the there command to navigate to the same directory.

How It Works

The here command records the current directory in a hidden file called ~/.here.sh and the there command just source that script. It’s that simple.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s