Thursday, January 02, 2020

How to upload and download files using sftp in a single command

Usually we log in to an SFTP server, and then upload a file:


$ sftp username@sftpserver
username@sftpserver's password:
Connected to sftpserver.
sftp> put file.tar.gz




Likewise, you first need to log in to the SFTP server, cd to the directory of the file location and download


$ sftp username@sftpserver
hanxuel@ezmovexhd's password:
Connected to ezmovexhd.
sftp> cd pub
sftp> get file.tar.gz




Single Command Upload

You can combine log on and upload with this command:


$ sftp username@sftpserver:/ <<<  $'put file.tar.gz'
username@sftpserver's password:
Connected to sftpserver.
Changing to: /
Uploading file.tar.gz to /put file.tar.gz
put file.tar.gz                                                 100%   64MB   9.1MB/s   00:07






Single Command Download

Likewise, you can combine login and download to a single command


$ sftp username@sftpserver:/file.tar.gz file.tar.gz
Connected to sftpserver.
Fetching /file.tar.gz to file.tar.gz
/file.tar.gz 100% 64MB 45.2MB/s 00:01





Passwordless Login

SFTP is built upon SSH. Therefore, you can use SSH public key authentication instead of typing in your password.


ssh-keygen
ssh-copy-id -i username@sftpserver






No comments:

Post a Comment