gaitat
gaitat

Reputation: 12642

cd alias for moving between parallel directory structures

Let's say I have the following directory structures (variable is the z or y or x or w directories):

/a/b/c/d/e/z/f/g/h/i/j/k
/a/b/c/d/e/y/f/g/h/i/j/k
/a/b/c/d/e/x/f/g/h/i/j/k
/a/b/c/d/e/w/f/g/h/i/j/k

How would I write an alias for the cd command (in bash) so that when I am in

/a/b/c/d/e/w/f/g/h or in
/a/b/c/d/e/w/f/g/h/i/j or in
/a/b/c/d/e/w/f/g/h/i/j/k

and type:

cd z

it would respectively take me in

/a/b/c/d/e/z/f/g/h or in
/a/b/c/d/e/z/f/g/h/i/j or in
/a/b/c/d/e/z/f/g/h/i/j/k

if I type:

cd y

it would respectively take me in

/a/b/c/d/e/y/f/g/h or in
/a/b/c/d/e/y/f/g/h/i/j or in
/a/b/c/d/e/y/f/g/h/i/j/k

of course if I type:

cd w

it should leave me where I am (already there).

Upvotes: 0

Views: 148

Answers (1)

umläute
umläute

Reputation: 31374

it's probably easiest to use a function:

mycd() {
  cd "/a/b/c/d/e/$1/${PWD#/a/b/c/d/e/*/}"
}

put that into your ~/.bashrc (or another file that you source with something like . mycd.bash), and use mycd x

Upvotes: 2

Related Questions