itchmyback
itchmyback

Reputation: 549

Faster way to Unix cd by changing one directory inside path?

I have multiple directories (eg tom richard harry) that have identical subdirectory and file structure. If I am working on a file inside one directory, is there a fast or easy way to cd to the equivalent path in another directory?

Example

pwd=/mystuff/myproject/tom/hobbies/sports/highschool

cd /mystuff/myproject/richard/hobbies/sports/highschool

I was hoping for some shortcut like cd pwd but change tom > richard in one command.

Upvotes: 3

Views: 432

Answers (6)

zero0
zero0

Reputation: 877

This would work:

cd $(pwd | perl -pi -e 's/tom/richard/g;')

Upvotes: 2

Celada
Celada

Reputation: 22261

Some shells, such as Zsh and ksh offer a special form of the cd builtin:

cd [ -qsLP ] old new

  The second form of cd substitutes the string new for the string old in the
  name of the  current  directory, and tries to change to this new directory.

So if you are using zsh or ksh, then this command should do it:

cd /mystuff/myproject/tom /mystuff/myproject/richard

no matter which subdirectory of /mystuff/myproject/tom you happen to currently be in.

Upvotes: 0

David W.
David W.

Reputation: 107090

It all depends upon your shell...

Most people use BASH -- it's the standard Linux shell, but Kornshell is very similar to BASH, and has the feature you're looking for:

$ cd /mystuff/myproject/tom/hobbies/sports/highschool
$ cd tom richard
$ pwd
/mystuff/myproject/richard/hobbies/sports/highschool

I also like the Kornshell print command and the way variables in Kornshell don't disappear on you in loops (because BASH makes them child processes).

Of course, BASH has features that are missing in Kornshell. One example is setting your prompt. In Bash, I set my prompt as thus:

PS1="\u@\h:\w\n\$ "
  • \u is the user ID
  • \h is the short host name
  • \w is the working directory in relationship to $HOME
  • \n is the newline
  • \$ is a $ if your ID isn't root and # if your ID is root.

The Kornshell equivalent is:

PS1=$(print -n "logname@hostname:";if [[ "${PWD#$HOME}" != "$PWD" ]] then; print -n "~${PWD#$HOME}"; else; print -n "$PWD";fi;print "\n$ ")

As I said, they're mostly equivalent. I can work with either one, but Kornshell has this particular feature and BASH doesn't.

Your alternative is to write a function that will do this for you, or to make an alias to the cd command.

Upvotes: 1

Daenyth
Daenyth

Reputation: 37461

You can use bash's history expansion for this.

^tom^richard - this will rerun the previous command, substituting richard for tom.

Bash History Expansion

Upvotes: 1

sigpwned
sigpwned

Reputation: 7453

If you know what directory you're in (say stored in $dirname variable):

function dirswitch() {
    newdir="$1"    
    cd $(pwd | sed -e "s#/$dirname/#/$newdir/#")
}

This should handle the job in bash. So if you're in dirname=tom and you want to switch to harry:

dirswitch harry

...will do the trick.

Upvotes: 1

WilQu
WilQu

Reputation: 7423

The following should work:

cd ${PWD/tom/richard}

Upvotes: 11

Related Questions