How to implement recursive put in sftp
Command-line sftp in my Ubuntu doesn't have recursive put implemented. I found some debate from 2004 about implementing such feature with -R option switch. So I see some sort of self-made recursion as only option.
Ie.
- iterate through directory listing
- cd into directories
- mkdir them if nonexistent
- put files
I'm planning on doing this with bash, but any other language would suffice.
Rsync or scp is not an option because I don't have shell access开发者_如何学Python to server. Only sftp.
Look at lftp. It's a powerful file transfer client which supports ftp, ftps, http, https, hftp, fish (file transfer over ssh shell session) and sftp. It has ftp-like interactive interface, but also allows to specify all commands at the command line. Look at mput
(non recursive but handles glob patterns) and mirror
(poor man's rsync) commands.
I use it with a server which only handles sftp uploads like this:
lftp -c "open -u $MYUSER,$MYPASSWORD sftp://$TARGET ; mirror -R $SOME_DIRECTORY"
While I think lftp is the best option if it's available, I got stuck on an ancient install of Cent OS and needed to do a recursive put via SFTP only. Here's what I did:
find dir -type d -exec echo 'mkdir {}' \; | sftp user@host
find dir -type f -exec echo 'put {} {}' \; | sftp user@host
So basically make sure all the directories exist and then send the files over.
The GUI FTP client FileZilla also supports SFTP and also supports uploading and downloading while directories.
my ubuntu 12.04 comes with put -r
in sftp
On the command line you can do that by using the putty-tools package.
It comes with a sftp replacement called psftp
.
It supports mput -r
which copies a local directory to the remote recursively.
I guess you can do this with bash but it's going to be a lot of work. Instead, I suggest to have a look at Python and the Chilkat library.
In Java, you can use edtFTPj/PRO, our commercial product, to transfer recursively via SFTP. Alternatively you might want to consider SCP - that generally supports recursion and runs over SSH.
How about sshfs?
Combined, of course, with cp -r
.
Or, failing that, rsync -r
by itself.
After lot's of googling and good answers I used Transmit syncing for the job. Not a very good solution, but does the job.
Here is how --
sftp -r <host>
password: <pass>
cd <remote dir> # moves to remote dest dir
put -r localdir/* # creates dir and copies files over
精彩评论