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?

share|improve this question

10 Answers 10

up vote 262 down vote accepted

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.


Explanation

  -t      Force pseudo-terminal allocation.  This can be used to execute arbitrary screen-based programs on a remote machine, which can be very useful, e.g. when implementing menu services.
          Multiple -t options force tty allocation, even if ssh has no local tty.
  • If you don't use -t then no prompt will appear.
  • If you don't add ; bash then the connection will get closed and return control to your local machine
share|improve this answer
18  
in my case -t the missing part – Mathieu Jan 30 '12 at 9:52
2  
I used "csh" instead of "bash" at the end because my shell was csh. Just a note! – quantum Aug 30 '12 at 2:08
5  
I had to do this: ssh -t xxx.xxx.xxx.xxx "cd /directory_wanted && exec \$SHELL" – guptron May 6 '13 at 21:03
22  
You'll usually want a login shell: ssh -t example.com "cd /foo/bar; exec \$SHELL -l" – christianbundy Apr 28 '14 at 3:54
5  
Just wanted to link change directory automatically on ssh login - Server Fault; and from there paste this command ( which is a version of the one by @christianbundy ): ssh server -t "cd /my/remote/directory; bash --login" – sdaau Jun 22 '14 at 13:23

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".

share|improve this answer
1  
+1, this looks more suitable if you need constant directory instead of making ssh command longer remembering everytime to which directory you are going to move when you connect to X machine – Tebe May 4 '14 at 21:49

I've created a tool to SSH and CD into a server consecutively – aptly named sshcd. For the example you've given, you'd simply use:

sshcd somehost:/some/directory/somewhere/named/Foo

Let me know if you have any questions or problems!

share|improve this answer
    
Would be cool if you could define a new config in ~/.ssh/config and read it from there ;) – Alexar Jan 28 '15 at 4:24
    
@Alexar github.com/fraction/sshcd/issues :) – christianbundy Feb 18 '15 at 23:43

I use the environment variable CDPATH

share|improve this answer
    
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 Mar 9 '09 at 15:09

Another way of going to directly after logging in is create "Alias". When you login into your system just type that alias and you will be in that directory.

Example : Alias = myfolder '/var/www/Folder'

After you log in to your system type that alias (this works from any part of the system)
this command if not in bashrc will work for current session. So you can also add this alias to bashrc to use that in future

$ myfolder => takes you to that folder

share|improve this answer

I'd been trying to work this out between Ubuntu servers for awhile, while using base target hosts setup in ~/.ssh/config. This works:

$ ssh -t targetHost "cd /var/www/target-dir; bash"

where targetHost matches the configs' Host targetHost entry.

share|improve this answer

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

share|improve this answer
3  
This is a partial answer, not a solution. – A.L Oct 16 '14 at 12:10

In my very specific case, I just wanted to execute a command in a remote host, inside a specific directory from a Jenkins slave machine:

ssh myuser@mydomain
cd /home/myuser/somedir 
./commandThatMustBeRunInside_somedir
exit

But my machine couldn't perform the ssh (it couldn't allocate a pseudo-tty I suppose) and kept me giving the following error:

Pseudo-terminal will not be allocated because stdin is not a terminal

I could get around this issue passing "cd to dir + my command" as a parameter of the ssh command (to not have to allocate a Pseudo-terminal) and by passing the option -T to explicitly tell to the ssh command that I didn't need pseudo-terminal allocation.

ssh -T myuser@mydomain "cd /home/myuser/somedir; ./commandThatMustBeRunInside_somedir"
share|improve this answer

Based on additions to @rogeriopvl's answer, I suggest the following:

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

Chaining commands by && will make the next command run only when the previous one was successful (as opposed to using ;, which executes commands sequentially). This is particularly useful when needing to cd to a directory performing the command.

Imagine doing the following:

/home/me$ cd /usr/share/teminal; rm -R *

The directory teminal doesn't exist, which causes you to stay in the home directory and remove all the files in there with the following command.

If you use &&:

/home/me$ cd /usr/share/teminal && rm -R *

The command will fail after not finding the directory.

share|improve this answer

simply modify your home with the command: usermod -d /newhome username

share|improve this answer
    
Not advised. Changing your home directory will probably cause other things to break. – bronson Nov 4 '16 at 18:28

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.