How can I ssh directly to a particular directory?

前端 未结 12 2113
情话喂你
情话喂你 2020-12-07 07:03

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          


        
12条回答
  •  半阙折子戏
    2020-12-07 07:34

    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.

提交回复
热议问题