sftp: upload all files, directories and sub-directories contained in a folder

29

15

On machine A I have the folder

/home/a/

On machine B I have the folder

/home/b/

I wish transfer all files, directories and sub-directories of /home/a in /home/b with sftp On machine A I tried the commands:

sftp fibon82@machineB.com
put /home/a/* /home/b/

but it doesn't work, i get the error message: "skipping non-regular file /home/a/a1"... [a1 is a sub-directory of a]
How could I modify the put instruction?

Thanks! :)

EDIT:

I solved using scp:

scp -r /home/a/ fibon82@machineB.com:/home/b/

fibon82

Posted 2012-02-08T13:15:37.933

Reputation: 293

@fibon82 For up-to-date manual to OpenSSH sftp, refer to the OpenSSH project.

– Martin Prikryl – 2014-12-22T19:56:11.140

2put -r would have worked too. – jhenninger – 2012-02-08T16:07:40.223

1

Ok but how could I know that for "put command" the option -r is available? If I look here only the flag -P is described... The same in the manual Thanks! :)

– fibon82 – 2012-02-09T00:37:38.160

You should post that as an answer instead. – N.N. – 2012-03-07T20:20:37.800

Yes. Instead of EDITing your question with the answer, you should answer your own question and accept it. – None – 2014-06-05T08:32:26.113

Answers

21

Although not strictly equivalent to sftp, rsync is a very powerful alternative for scp and sftp, especially when updating the copies from machine A to machine B, as it doesn't copy the files that haven't been altered; it's also able to remove files from machine B that have been deleted from machine A (only when it's told to of course).

In your case, the syntax would be

rsync -zrp /home/a/ user@remote.host.com:/home/b/

The -r option is for recursively copying files, -z enables compression during the transfer, and -p preserves the file permissions (file creation, edit, etc.) when copying, which is something that scp doesn't do AFAIK. Many more options are possible; as usual, read the man pages.

Karolos

Posted 2012-02-08T13:15:37.933

Reputation: 2 304

2

Sadly rsync does not speak sftp-Protocol. So if you set up an sftp-chroot using ssh's build in internal-sftp then rsync fails.

– Tino – 2016-05-03T11:54:09.383

Ah thank you! :) A new thing that I learned! – fibon82 – 2012-02-09T00:08:26.543

@fibon82: You're welcome :) – Karolos – 2012-02-09T06:47:52.327

1i love you, i synced 400MB of data in 1 minute by just using your code. I would add you should also use --progress otherwise you'll be staring at nothing without knowing what's happening (and at what speed :) ) – Sandro Antonucci – 2012-12-18T22:18:07.477

21

In sftp this command recursively uploads content of the current directory to the remote current directory:

 put -r .

See man sftp.

Carlos Da Costa

Posted 2012-02-08T13:15:37.933

Reputation: 311

The -r switch is supported since OpenSSH 5.4 only.

– Martin Prikryl – 2014-12-22T19:52:59.827

1Note that the -r switch is client side only (part of sftp command). So the server (here: receiving) side does not need OpenSSH 5.4, only the client needs to support it. – Tino – 2016-05-03T11:57:42.420

10

scp (secure copy) is the Linux de facto for transferring files over a secure tunnel. In your case you would want to use the recursive switch, e.g.:

scp -r /home/a/ user@remote.host.com:/home/b/

deed02392

Posted 2012-02-08T13:15:37.933

Reputation: 2 662

6sftp and scp are actually different protocols, both based on ssh. – paradroid – 2012-02-08T16:20:51.113

1Yes, if the server only allows sftp protocol, this answer does not work. – рüффп – 2013-12-06T14:05:39.820

4

Try using

put -r /home/a/ /home/b/

for more info check out: this

guest_who

Posted 2012-02-08T13:15:37.933

Reputation: 41

1

The -r switch is supported since OpenSSH 5.4 only.

– Martin Prikryl – 2014-12-22T19:52:35.113

Note that the -r switch is client side only (part of sftp command). So the server (here: receiving) side does not need OpenSSH 5.4, only the client needs to support it. And: This should be the accepted answer, as getting (the possibly unsupported) rsync as answer to a question tagged sftp is a bit confusing. – Tino – 2016-05-03T12:00:11.073

As far as I can tell, it copies a/ inside b/, but only if b/a/ already exists. – Eric Duminil – 2019-12-10T14:16:41.160

0

Actually, put -r should work. But the destintion folder needs to be present on your remote host:

sftp> put -r sourcefolder
 Uploading sourcefolder/ to /user/folder
 Couldn't canonicalize: No such file or directory
 ....
sftp> mkdir sourcefolder
sftp> put -r sourcefolder
 Uploading sourcefolder/ to /user/folder/sourcefolder
 Entering sourcefolder/
 sourcefolder/file1
 sourcefolder/file2

Dieter

Posted 2012-02-08T13:15:37.933

Reputation: 1

Have you added reference and proof supporting what you state and confirmed this answer is not already answered in one of the existing answers on the post. Read over "Why do I need 50 reputation to comment" to ensure you understand how you can start commenting.

– Pimp Juice IT – 2017-10-05T14:13:38.943

-1

In my case rsync wasn't possible so I used:

mput -rp /home/a/ /home/b/

jayarjo

Posted 2012-02-08T13:15:37.933

Reputation: 119

There's no mput command in OpenSSH sftp. Maybe you refer to psftp? – Martin Prikryl – 2014-12-22T19:52:11.023