Automated FTP transfer

The lftp and the ftp commands open an interactive session with the user. We can automate FTP file transfers with a shell script:

#!/bin/bash 

#Automated FTP transfer 
HOST=example.com' 
USER='foo' 
PASSWD='password' 
lftp  -u ${USER}:${PASSWD} $HOST <<EOF 

binary 
cd /home/foo 
put testfile.jpg 

quit 
EOF 

The preceding script has the following structure:

<<EOF 
DATA 
EOF 

This is used to send data through stdin to the lftp command. The Playing with file descriptors and redirection recipe of Chapter 1, Shell Something Out, explains various methods for redirection to stdin.

The -u option logs in to the remote site with our defined USER and PASSWD. The binary command sets the file mode to binary.

Get Linux Shell Scripting Cookbook - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.