3

I needed to copy multiple folders of same directory using one command. So I did this first,

sftp> mget -r folder1 folder2

This command copied folder1 to the destination server but renamed it to folder2.

I probably should have done something like the following. But, I did not try it

sftp> get -r folder1 && sleep 5 && get -r folder2

I did not try this but I think it would have worked.

Is there any better alternative to this?

fra-san
  • 10,205
  • 2
  • 22
  • 43

2 Answers2

4

The mget subcommand in sftp is an alias to get:

static const struct CMD cmds[] = {
    { "bye",    I_QUIT,     NOARGS  },
    ...
    { "get",    I_GET,      REMOTE  },
    { "mget",   I_GET,      REMOTE  },
    ...
};

The get subcommand has a syntax of:

 get [-afPpr] remote-path [local-path]

If you give get or mget multiple parameters, it treats the second one as the rename destination for the first. As a result, you cannot retrieve multiple directories at once; you'll need to get them separately:

mget -r folder1 
mget -r folder2

The sftp syntax also does not allow for shell-style command chaining, so you cannot use a command like: get -r folder1 && .... There is also no need to try and sleep between get commands, unless you need a delay for other purposes (a watching process locally, to spare the network for a few seconds, etc).

As an alternative to sftp, consider scp:

scp -r user@host:folder1 user@host:folder2 /local/directory

... which will recursively copy the remote folder1 and folder2 directories into the local /local/directory.

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
  • Thank you, yes, I copied both folders separately in the end. I tried scp command but it did not work for me, so get command was the only option for me. – Rakib Fiha Oct 30 '18 at 12:43
  • @RakibFiha, consider the syntax for scp that I just added to my answer as another possibility. – Jeff Schaller Oct 30 '18 at 12:58
4

If your folders are folder1 and folder2, you can get them using one command.

get -r folder[1-2]

get [-afPpr] remote-path [local-path]
Retrieve the remote-path and store it on the local machine. If the local path name is not specified, it is given the same name it has on the remote machine. remote-path may contain glob(7) characters and may match multiple files. If it does and local-path is specified, then local-path must specify a directory.

ctac_
  • 1,960