How to ssh and cd to the current directory?

4525 views
Skip to first unread message

Peng Yu

unread,
Oct 10, 2009, 11:03:08 AM10/10/09
to
ssh by default only goes to the home directory ~. I'm wondering if
there is a way to supply a directory (that is /home/name/some_dir in
this case) to it, so that it will go to the directory directly.

$pwd
/home/name/some_dir
$ssh localhost # I want to change the directory to /home/name/some_dir
after sshed to localhost
$pwd
/home/name

Nico Kadel-Garcia

unread,
Oct 10, 2009, 12:44:08 PM10/10/09
to

I don't think there's anything built in, but you could do something
like this:

# ssh targethost '(cd /targetdir; /bin/bash)'

That should get you a working shell in the target directory, and
terminate your connection when you leave that shell.

Dag-Erling Smørgrav

unread,
Oct 10, 2009, 1:21:30 PM10/10/09
to
Nico Kadel-Garcia <nka...@gmail.com> writes:

> Peng Yu <peng...@gmail.com> writes:
> > ssh by default only goes to the home directory ~. I'm wondering if
> > there is a way to supply a directory (that is /home/name/some_dir in
> > this case) to it, so that it will go to the directory directly.
> I don't think there's anything built in, but you could do something
> like this:
>
> # ssh targethost '(cd /targetdir; /bin/bash)'
>
> That should get you a working shell in the target directory, and
> terminate your connection when you leave that shell.

You need to add -t, otherwise your shell will think it's reading a
script from /dev/stdin rather than running an interactive session. You
won't get a prompt, command-line editing etc., and you won't be able to
run any screen-oriented programs (such as top).

DES
--
Dag-Erling Smørgrav - d...@des.no

Peng Yu

unread,
Oct 10, 2009, 1:45:22 PM10/10/09
to
On Oct 10, 12:21 pm, Dag-Erling Smørgrav <d...@des.no> wrote:
> Nico Kadel-Garcia <nka...@gmail.com> writes:

I tried the following command.

ssh targethost '(cd /targetdir; /bin/bash -t)'

It does not give a prompt. When I type 'top', it prints 'TERM
environment variable not set.' then return to the shell in my local
machine.

Would you please help test if you also see the same thing?

Regards,
Peng

Dag-Erling Smørgrav

unread,
Oct 10, 2009, 2:00:31 PM10/10/09
to
Peng Yu <peng...@gmail.com> writes:
> I tried the following command.
>
> ssh targethost '(cd /targetdir; /bin/bash -t)'

ssh -t targethost 'cd /targetdir; /bin/bash'

Unruh

unread,
Oct 10, 2009, 6:17:33 PM10/10/09
to
Peng Yu <peng...@gmail.com> writes:

>On Oct 10, 12:21=A0pm, Dag-Erling Sm=F8rgrav <d...@des.no> wrote:
>> Nico Kadel-Garcia <nka...@gmail.com> writes:
>> > Peng Yu <pengyu...@gmail.com> writes:
>> > > ssh by default only goes to the home directory ~. I'm wondering if
>> > > there is a way to supply a directory (that is /home/name/some_dir in
>> > > this case) to it, so that it will go to the directory directly.
>> > I don't think there's anything built in, but you could do something
>> > like this:
>>
>> > # ssh targethost '(cd /targetdir; /bin/bash)'
>>
>> > That should get you a working shell in the target directory, and
>> > terminate your connection when you leave that shell.
>>
>> You need to add -t, otherwise your shell will think it's reading a

>> script from /dev/stdin rather than running an interactive session. =A0You


>> won't get a prompt, command-line editing etc., and you won't be able to
>> run any screen-oriented programs (such as top).
>>
>> DES
>> --

>> Dag-Erling Sm=F8rgrav - d...@des.no

>I tried the following command.

>ssh targethost '(cd /targetdir; /bin/bash -t)'

>It does not give a prompt. When I type 'top', it prints 'TERM
>environment variable not set.' then return to the shell in my local
>machine.

>Would you please help test if you also see the same thing?

Uh, -t is the argument to ssh, not to bash.
ssh -t target 'cd /targetdir; /bin/bash '
for example.

>Regards,
>Peng

stardu...@gmail.com

unread,
Aug 6, 2013, 7:39:28 AM8/6/13
to
ssh machine 'cd /path/to/working/directory && mycommand' works for me on Fedora 13, but I have to admit, that I have 'Defaults ! requiretty' on both machines in /etc/sudoers and I am not sure whether it can influence that behaviour.
Reply all
Reply to author
Forward
0 new messages