Shell script to transfer files using sftp

This tutorial aims to detail step by step shell script to transfer files using sftp on UNIX systems.


In this tutorial we want to transfer a .jpeg file from a directory on a local computer to a directory on a remote server.

We have to use SFTP to secure the file and transfer mode.

copy multiple files

xargs is a command on Unix and most Unix-like operating systems used to build and execute commands from standard input. It converts input from standard input into arguments to a command.

Using sftp

sftp is mostly for interactive operations, similar to ftp, which performs all operations over an encrypted ssh transport. It may also use many features of ssh, such as public key authentication and compression. sftp connects and logs into the specified host, then enters an interactive command mode. You need to specify host you want to connect to:


you will be prompted for username and passsword, and the interactive session will begin..

Although it can be used in scripts, the scp is much more easy to use:

scp /path/to/localfile user@host:/path/to/dest

you will be prompted for password.

Both scp and sftp use ssh as underlying protocol.

The best way to setup them to run from scripts is to setup passwordless authentication using keys.

Setup passwordless authentication using keys

In this example we will setup SSH password-less automatic login from server as user tecmint to with user yoctobe.

Step 1: Create Authentication SSH-Kegen Keys on – (

First login into server with user tecmint and generate a pair of public keys using following command:

[ ~]$ ssh-keygen -t rsa

Step 2: Create .ssh Directory on –

Use SSH from server to connect server using yoctobe as user and create .ssh directory under it, using following command:

[tecmint@tecmint ~]$ ssh yoctobe@ mkdir -p .ssh

Step 3: Upload Generated Public Keys to –

Use SSH from server and upload new generated public key ( on server yoctobe‘s .ssh directory as a file name authorized_keys.

Step 4: Set Permissions on –

Due to different SSH versions on servers, we need to set permissions on .ssh directory and authorized_keys file

[tecmint@tecmint ~]$ ssh yoctobe@ "chmod 700 .ssh; chmod 640 .ssh/authorized_keys"

Step 5: Login from to Server without Password

From now onwards you can log into as yoctobe user from server as tecmint user without password.

[tecmint@tecmint ~]$ ssh yoctobe@

After you setup keys, you can run

scp -i private-key-file /path/to/local/file user@host:/path/to/remote

sftp -oIdentityFile=private-key-file -b batch-file user@host

If you want to authenticate with password, you may try the expect package. The simplest script may look like this:

spawn sftp -b batch-file user@host
expect "*?assword:*"
send "pasword\n"



Leave a Reply