2

I generally work within multiple folders (dev1, dev2 and dev3) in the same server. The folder structure is something like:

  • /var/www/html/dev1/a/b/c/d/e/f
  • /var/www/html/dev2/a/b/c/d/e/f
  • /var/www/html/dev3/a/b/c/d/e/f

When I am at a folder, say c or d or e of /var/www/html/dev3/, is there a command that will take me to the /var/www/html/dev3/a?

The same command should take me to /var/www/html/dev1/a if I run the command from folder c or d or e of /var/www/html/dev1/.

Thomas Dickey
  • 76,765
Gokul N K
  • 171

6 Answers6

4

Method #1 - relative cd's

The easiest method to move up a certain depth from where you are is to do this command:

$ pwd
/Users/sammingolelli/adir/dev3/a/b/c/d/e/f

$ cd ../../../../..

$ pwd
/Users/sammingolelli/adir/dev3/a

Method #2 - pushd/popd

Beyond that, you can also use pushd and popd which acts as a stack, maintaining a FIFO or the directories you tell it as you use pushd to go to directories, and then popd to return to the previously pushed directory on the stack.

Example

$ pwd
/Users/sammingolelli/adir/dev3/a
$ dirs
~/adir/dev3/a

$ pushd b/c/d/e/f/
~/adir/dev3/a/b/c/d/e/f ~/adir/dev3/a
$ pwd
/Users/sammingolelli/adir/dev3/a/b/c/d/e/f

$ popd
~/adir/dev3/a
$ pwd
/Users/sammingolelli/adir/dev3/a

Method #3 - aliases

You could also create an alias to do what you want. This is covered in several Q&A's that are already on the site, or see @fastrizwaan's answer.

Method #4 - CDPATH

Take a look at this answer by @manatwork, on this Q&A: Quick directory navigation in the terminal.

More, using a tool?

The above two methods are what most typically use to move around. There are of course additional tools that you can install which act as navigation tools. Many of these tools have been discussed on this site. One that comes to mind acts as a bookmarking tool, which allows you to "bookmark" a specific directory and map it to a short name, so that you can easily change to it from wherever you may be.

autojump

One such tool I discuss in this Q&A titled: How to create a short path?. The tool is called autojump.

others

There are also others, which were suggested in this Q&A titled: Quick directory navigation in the terminal.

slm
  • 369,824
  • 1
    You missed the point of the question. cd ../../../../.. takes you to a from f but to dev? from e, to www from d, etc. Gokul asks to get to /var/www/html/dev?/a no matter where he is underneath. – Gilles 'SO- stop being evil' Jul 09 '15 at 22:34
2

That's a job for a function. Assuming that your shell is bash, you can define a function in your .bashrc. The function will be available the next time you start bash.

cdwww () {
  …
}

What should go as the ? A cd command, surely. The path to the current directory is in the variable PWD, and you can use string processing and other constructs.

For example the following function goes to the directory above the current directory just under /var/www/html, if the current directory is under /var/www/html.

cdwww () {
  if [[ $PWD = /var/www/html/*/* ]]; then
    local d="${PWD#/var/www/html/*/*/}"
    cd "${PWD%"$d"}"
  else
    echo 1>&2 "$0: not under a web root"
    return 3
  fi
}

You can make this function accept a path to a different subdirectory of /var/www/html/dev*/a.

cdwww () {
  if [[ $PWD = /var/www/html/*/* ]]; then
    local d="${PWD#/var/www/html/*/*/}"
    cd "${PWD%"$d"}/$1"
  else
    echo 1>&2 "$0: not under a web root"
    return 3
  fi
}

A different approach would be to set a variable to the target directory whenever you change directories. The following code arranges to execute the chpwd function every time the current directory changes.

cd () { builtin cd "$@" && chpwd; }
pushd () { builtin pushd "$@" && chpwd; }
popd () { builtin popd "$@" && chpwd; }
chpwd () {
  if [[ $PWD = /var/www/html/*/* ]]; then
    local d="${PWD#/var/www/html/*/*/}"
    html_root="${PWD%"$d"}"
  else
    unset html_root
  fi
}

Then you can use cd $html_root (or just $html_root if you've turned on shopt -s autocd).

1

Now I understood your Question:

cd $(pwd|cut -c 1-18)/a

will bring you to /var/www/html/dev1/a when you are below or in dev1 and to /var/www/html/dev2/a when you are below or in dev2 and so on.

cut seems to be a hack here and you have change the 18 when your pathes change but I didn't find a more elegant solution using regex's.

Jodka Lemon
  • 3,173
0

This will get you back to directory depth 5:

cd `pwd | cut -d'/' -f1-6`
MBH
  • 1
0

In zsh:

cd $PWD[1,(rn[5])/]

Would cd to the substring of $PWD that start on the 1st character and ends at the 5th occurrence of a / character, so in:

1   2   3    4    5
/var/www/html/dev1/a/b/c/d/e/f

That would do a cd /var/www/html/dev1/.

You could make a helper function:

cdd() cd $PWD[1,(rn:argv[1]:)/]

To cd within $PWD to a depth given as argument with cdd 5 for instance.

-1

I think you want aliases for directory: put this at the end of your .bashrc if you want it permanently.

alias cd1a='cd /var/www/html/dev1/a/'
alias cd2a='cd /var/www/html/dev2/a/'
alias cd3a='cd /var/www/html/dev3/a/'

alias cd1b='cd /var/www/html/dev1/a/b'
alias cd2b='cd /var/www/html/dev2/a/b'
alias cd3b='cd /var/www/html/dev3/a/b'

like that you can make it for others.

just run cd1a to go to /var/www/html/dev1/a/ from any directory.