On a server I work on, I always have to work in a directory, let's call it /foo/bar. In that directory is a list of site directories. Inside each of those directories is a directory for each developer. So, the tree looks like this:


I set up an alias for the root directory in my .bash_profile, fb=/foo/bar, so my workflow looks like this:

$ cd $fb
$ cd site2
$ cd userA

The first and third lines never change--I'm always going to go to the userA directory of whatever site directory I change to.

My question: Is there a clever way I can compress this into a single command that I just pass the "site2" argument without having to build this into a shell script? Or is the shell script the only way?

migrated from serverfault.com Mar 7 '13 at 7:44

This question came from our site for system and network administrators.


You could do something with a bash function

site() {
    cd /foo/bar/site${1}/userA

Put that in your .bashrc and then call it like

site 2

it will take you to /foo/bar/site2/userA

  • 2
    I don't find there's any meaningful difference between a function and a separate script... but this is really the way to do this. Multiple aliases a-la Steve Will's suggestion works too but doesn't require any less work to set up. Personally I use both: lots of discrete cd aliases for specific directories, and functions for patterned directory changes. – SuperMagic Mar 7 '13 at 13:40
  • @SuperMagic: Probably, an alias/function is faster to setup than a script as you probably have a shortcut in your editor to go to that file. Also, those are typically short, and you don't want one million one-liner scripts. It is also easier to copy one .rc file to a new system than to bring over a script folder and changing the PATH. But the other thing you wrote (last sentence) is exactly what I do, and I think it is as good a way as any. – Emanuel Berg Mar 8 '13 at 1:10

Use a bash function like this.

function myCD() {
   local site
   local user
   case $# in
     0)  read -p site: site
         read -p user: user
       echo "don't accept more as 2 arguments" >&2
       return 1
   cd ${fb}/${site}/${user}

Just link them into your home directory:

ln -s /foo/bar/site{$1}/userA ~/workspace/$1

That way, they will all appear local to you. No need to faff about until a new site is added.

Or, do what I do: Mount them onto my local workstation via SSH directly into my home-dir.. IDE's and whatnot being what they are, it is generally more efficient.

mkdir ~/workspace/site1
sshfs -o idmap=user $USER@server:/foo/bar/baz/site1/userA ~/workspace/site1

To unmount,

fusermount -u ~/workspace/site1

From ubuntu help

Pop that into your login script, bingo, all your work-base are belong to us!


You could probably just use an alias like

alias site2="cd /foo/bar/site2/userA"

You also might want to look into the CDPATH environment variable. See the man page for your shell.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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