How to Automate FTP with a Shell Script

July 4, 2009 · 0 comments

in How To

Here is an example shell script:

ftp -in <ftp-server> << EOF > ~/log.txt 2>&1
user <username> <password>
cd <directory>
get <file>
bye

Another way to send the username and password is:

ftp -in <ftp-server> << EOF > ~/log.txt 2>&1
quote USER <username>
quote PASS <password>
cd <directory>
get <file>
bye

The ftp -i option turns off interactive file prompting during multiple file transfers. The -n option prevents “auto-login” upon initial connection, which could involve prompting for a username and password.

<< EOF is a syntax called a here-document. It uses I/O redirection to feed a list of commands to an interactive program such as ftp until reaching the specified delimiter, end-of-file in this example. For more information, visit the Wikipedia article and the Advanced Bash Scripting Guide chapter at The Linux Documentation Project.

> redirects stdout to the specified file. The output of the program gets written to the specified file.

2>&1 redirects stderr to stdout. In this case, both the error messages and the output of the program get written to the specified file.

The rest are ftp commands. Please check the man pages for the ftp utility. Here is information about the ftp requests for USER and PASS sent via the quote command.

Leave a Comment

Next post: