Shweta
Shweta

Reputation: 5456

logging on a remote machine

Is it possible to login to a particular folder of a remote machine, through shell script: instead of ssh and then cd. Can I do it in one command? Is it possible to copy a file on two different folders of a remote machine through scp in one go?

Upvotes: 0

Views: 197

Answers (3)

glenn jackman
glenn jackman

Reputation: 247250

The cleanest way for your first question is Expect:

#!/usr/local/bin/expect
spawn ssh user@host
expect -re {\$ $} ;# adjust to suit your prompt
send "cd some/dir\r"
interact

I'm unclear about your 2nd question: do you have 2 remote files that you want to copy to your local machine, or do you want to copy a file from one remote dir to another remote dir?

scp user@host:/path/to/file1 user@host:/path/to/file2 .
ssh user@host "cp /path/to/file1 /path/to/folder2/"

Upvotes: 1

dogbane
dogbane

Reputation: 274898

You can pass the cp command to ssh as follows:

ssh user@host "cp /path/to/folder1/file /path/to/folder2"

or combine with cd:

ssh user@host "cd /path/to/folder1; cp file /path/to/folder2"

Upvotes: 1

hibbelig
hibbelig

Reputation: 538

I think there is no real solution to this. You can try ssh user@host bash -c "cd /tmp; bash -i" if your shell is bash. That will look a little like it could work, but you will quickly discover it's not really working. (Try to invoke an editor such as vi and you will see.)

You can also put cd /tmp into the ~/.bashrc on the remote end. This way, ssh user@host does what you ask for. But I guess you want to be able to go to different directories, so this won't fly either.

I think the best approach is to make it convenient to enter the cd command, e.g. using aliases. Then it's less typing.

Upvotes: 1

Related Questions