Sunday, November 10, 2013

SFTP Command to transfer files remotely

I was looking for something similar to WinScp for MacBook Pro, but could not find it. There are similar tools available to do the same task but I did'nt want to install an unknown application.

Finally I had to resort to SFTP.

In the following example I am trying to connect to a remote Linux server from my local MacBook-Pro and upload some *.csv files from my local folder to a remote folder.

Steps:
  • Connect to remote server
> sftp remote.secure-wb1-hvbd.com

Connecting to remote.secure-wb1-hvbd.com...
remote.secure-wb1-hvbd.com FTP server ready.
Name: yourusername
Password: 

//// Provide user and password for authentication

  • Navigate to Remote Destination Directory
Connected to remote.secure-wb1-hvbd.com
sftp> cd /var/lib/users/test/destination-dir

  • Use the lcd command to specify the local source directory where file to be uploaded, exists.
sftp> lcd /Users/localuser/workspace/server-data

  • Use the put (single file), mput (multiple files) commands to upload the files to remote directory.
Example :
// Upload all *.csv files using mput command
sftp> mput *.csv

Or 
// Upload a specific file using put command
sftp> put testout.csv

  •  Similarly get and mget can be used to retrieve the files.
  • For larger files, we might encounter the following error [rite failed: Broken pipe]. Use the zip utility to  zip the file first and then do sftp. For Example:
//Zip the file on local server
>gzip testout.csv
 //It will create testout.csv.gz

// Upload on server using SFTP as mentioned above

//Unzip the file on remote server 
>gzip -d testout.csv.gz


No comments:

Post a Comment