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
).
/var/www/html/dev3/[b-e]
and you want to go/var/www/html/dev3/a
?. You can usepushd
andpopd
, or thecd
itself. – David Martínez Jul 09 '15 at 11:48cd ../../..
is not what you meant, right? – Jodka Lemon Jul 09 '15 at 11:49