54

I have tried put -r directory/*, which only uploaded the files and not folders. Gave me the error, cannot Couldn't canonicalise.

Any help would be greatly appreciated.

Eugene S
  • 6,709
  • 8
  • 57
  • 91
Chris
  • 651
  • 2
  • 10
  • 16

5 Answers5

229

For people actually wanting a direct answer to this question (instead of being told to use something other than sftp)...

put -r local/path/to/directoryName

The uploaded directory must already exist in the working directory on the server, so you might need to create it first.

mkdir directoryName
Ben Thielker
  • 4,104
  • 4
  • 21
  • 20
  • 3
    when I try put -r somefile I get "invalid flag -r" – Jimi Kimble Aug 22 '14 at 03:40
  • 4
    Thank you - somebody make this the accepted answer. – Nightwolf Nov 07 '14 at 16:30
  • 5
    There should be a note for those poor users like myself who are not fortunate enough to be using a recent version of OpenSSH - recursive get and put were added in 5.4, those on CentOS or similar may be running an earlier version unless they have updated it manually. – totallyNotLizards Feb 10 '15 at 14:49
  • 1
    Scouring for hours on the Internet and this is the only thing that I found that worked. Thank you Ben Thielker! What a time and lifesaver. – Vincent Polisi Nov 23 '15 at 18:14
  • Slightly baffled as to why this answer has so many up votes. In the case of someone who needs to copy a directory with n sub dir and 2k files this answer is not helpful if i need to create each sub directory. This answer is not practical for every case. – Rex Charles Sep 19 '16 at 19:38
  • The server I'm using doesn't allow SSH (only SFTP), so the accepted answer does not work for me (and thus does not actually answer the question...) – Niema Moshiri Apr 11 '19 at 22:16
31

Here you can find detailed explanation as how to copy a directory using scp. In your case, it would be something like:

$ scp -r foo your_username@remotehost.edu:/some/remote/directory/bar

This will copy the directory "foo" from the local host to a remote host's directory "bar". Here -r is -recursively copy entire directories.

You can also use rcp with similar syntax. The only difference between them is that scp uses secure shell and rcp uses remote shell.

BTW The "Couldn't canonicalise" error you mentioned appear when sftp server is unable to access the file/directory mentioned in the command.

UPDATE: For users who want to use put specifically, please refer to Ben Thielker answer here.

Eugene S
  • 6,709
  • 8
  • 57
  • 91
5
sftp> mkdir source
sftp> put -r source
 Uploading source/ to /home/myself/source
 Entering source/
 source/file1
 source/file2

enter image description here

Girdhar Singh Rathore
  • 5,030
  • 7
  • 49
  • 67
0

if you have issues using sftp, you can use ncftp For centos

yum install ncftp

To copy a whole directory recursively

ncftpput -R -v -u username -P 21 ftp.server.dev /remote-path/ /localdirectory
Byron
  • 9
  • 2
-11

Use scp instead. It uses SSH too and can easily handle recursion.

IneQuation
  • 1,244
  • 11
  • 27
  • 4
    `scp` is indeed superior, but not all `sftp` servers support `scp` connections, so `sftp` itself is sometimes required. – Christopher Schultz Mar 04 '16 at 16:56
  • At the moment I'm in a C++ code base with sftp support but not scp support, and so this doesn't work for me either. scp is not always the answer. – PThomasCS Jul 03 '17 at 02:38