Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Specifically I'm wondering if, while working on a project, if I do cd it can take me to the root of the project and if I'm anywhere else then I get the default behaviour.

share|improve this question
What do you mean by "project"? I the following alias for mercurial projects: alias cdroot='cd $(hg root)' – lazy1 May 29 '12 at 23:47
You can use pushd instead of cd when you first leave the project root, then use popd to return when you're ready. – Marty Fried May 30 '12 at 0:30
up vote 3 down vote accepted
function projectcd() {
  if [[ ! -z $arg ]] ; then
    cd $arg
  elif [[ $(pwd) == /home/user/project/root/* ]] ; then
    cd /home/user/project/root/
alias cd=projectcd
share|improve this answer
You could simplify this a bit and avoid the alias by just calling the function cd. If you do so, you can refer to the built in cd as builtin cd. And you'll have to in the function definition. – Scott Severance May 30 '12 at 10:31
@ScottSeverance Where I was headed was to make projectcd take two parameters. $1 would be the project root directory and $2 the optional argument from cd. Then the alias would set the project root directory (and could be changed easily) without needing to modify the function. Nevertheless, as written, I agree with you. – walrii May 30 '12 at 16:21

Use CDPATH to define the base directory for cd command

[ramesh@dev-db ~]# pwd

[ramesh@dev-db ~]# cd mail
-bash: cd: mail: No such file or directory
[Note: This is looking for mail directory under current directory]

[ramesh@dev-db ~]# export CDPATH=/etc
[ramesh@dev-db ~]# cd mail
[Note: This is looking for mail under /etc and not under current directory]

[ramesh@dev-db /etc/mail]# pwd
share|improve this answer
That's cool, but if i do just cd it still sends me to the home directory – geermc4 May 29 '12 at 23:34
@geermc4 You can change your HOME env var. – kev May 29 '12 at 23:40

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.