views:

315

answers:

4

I often have to login to one of several servers and go to one of several directories on those machines. Currently I do something of this sort:

localhost ~]$ ssh somehost

Welcome to somehost!

somehost ~]$ cd /some/directory/somewhere/named/Foo
somehost Foo]$ 

I have scripts that can determine which host and which directory I need to get into but I cannot figure out a way to do this:

localhost ~]$ go_to_dir Foo

Welcome to somehost!

somehost Foo]$

Is there an easy, clever or any way to do this?

+1  A: 

SSH itself provides a means of communication, it does now know anything about directories. Since you can specify which remote command to execute (this is - by default - your shell), I'd start there.

Jan Jungnickel
+11  A: 

You can do the following:

ssh -t xxx.xxx.xxx.xxx "cd /directory_wanted ; bash"

This way, you will get a shell right on the directory_wanted.

rogeriopvl
Executing the shell at the end was the final piece of the puzzle. Thank you.
Frosty
A: 

I use the environment variable CDPATH

Eddy
Useful only if I was interested in one directory per machine. I need a way to pass information to the remote machine. Roger's solution does just that.
Frosty
+1  A: 

You could add

cd /some/directory/somewhere/named/Foo

to your .bashrc file (or .profile or whatever you call it) at the other host. That way, no matter what you do or where you ssh from, whenever you log onto that server, it will cd to the proper directory for you, and all you have to do is use ssh like normal.

Of curse, rogeriopvl's solution works too, but it's a tad bit more verbose, and you have to remember to do it every time (unless you make an alias) so it seems a bit less "fun".

Chris Lutz