Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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:

/foo/bar/
         site1/
               userA/
               userB/
               userC/
         site2/
               userA/
               userB/
               userC/
         site3/
               userA/
         ...

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?

share|improve this question

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

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

add comment

4 Answers 4

up vote 6 down vote accepted

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

share|improve this answer
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
add comment

Use a bash function like this.

function myCD() {
   local site
   local user
   case $# in
     0)  read -p site: site
         read -p user: user
         ;;
     1|2)
        site=${1}
        user=${2:-userA}
        ;;
    *)
       echo "don't accept more as 2 arguments" >&2
       return 1
       ;;
   esac
   cd ${fb}/${site}/${user}
}
share|improve this answer
add comment

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!

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

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.