Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Advanced directory switching in bash

Tags:

bash

I know a few advanced ways, to change directories. pushd and popd (directory stack) or cd - (change to last directory).

But I am looking for quick way to achieve the following:

Say, I am in a rather deep dir:

/this/is/a/very/deep/directory/structure/with\ lot\ of\ nasty/names

and I want to switch to

/this/is/another/very/deep/directory/structure/with\ lot\ of\ nasty/names

Is there a cool/quick/geeky way to do it (without the mouse)?

like image 979
Mo. Avatar asked Sep 13 '08 21:09

Mo.


People also ask

How do I change directory in bash?

To change directories, use the command cd followed by the name of the directory (e.g. cd downloads ). Then, you can print your current working directory again to check the new path.

What is cd in bash?

The cd (“change directory”) command is used to change the current working directory in Linux and other Unix-like operating systems. It is one of the most basic and frequently used commands when working on the Linux terminal.

How do I get the current directory in bash?

By default, bash shows just your current directory, not the entire path. To determine the exact location of your current directory within the file system, go to a shell prompt and type the command pwd. This tells you that you are in the user sam's directory, which is in the /home directory.

Can you cd in a bash script?

Trying to use cd inside the shell script does not work because the shell script runs in the subshell and once the script is over it returns to the parent shell, which is why the current directory does not change.


2 Answers

Do you mean that the path names are the same, and only one directory name changes ("a" becomes "another")? In that case:

cd ${PWD/a/another}

will switch to the other directory. $PWD holds your current directory, and ${var/foo/bar} gives you $var with the string 'foo' replaced by 'bar'.

like image 117
dF. Avatar answered Sep 28 '22 18:09

dF.


What about setting up your CDPATH variable?

like image 28
Rob Wells Avatar answered Sep 28 '22 18:09

Rob Wells