Feb 222015

This post is a fast recipe to include a file transfer using the FTP protocol inside a bash script.

There are many hosting service providers that offer FTP as the mechanism to update or retrieve content from their servers. Working with FTP is not an issue, because there is a large number of interactive clients that implement this protocol, including some open source tools with a graphical frontend such as FileZilla.

But, in order to automate some processes that involve transferring files from/to the server (for instance, a daily backup of the content), the transfers must be performed from inside a program, or a shell script.

The following snippet can be used to upload a file to the server from a bash script:

Option “-n” inhibits the use of a .netrc file that might be present in the login directory

Option “-v” requests a verbose output. The responses received from the server are printed to standard output, as well as a report with statistics of the transfer.

Next are the ftp commands sent to the server. In this example those are:

  • ascii: sets ASCII mode transfer. This is useful if text files are being moved between a Windows and a Linux system, because it takes care of the conversion from Windows line delimiters (CRLF) to Linux delimiters (LF) and viceversa. In all other cases, binary mode should be used.
  • prompt: Disables interactive prompting when the mget or mput commands are used to transfer multiple files

 Posted by at 10:15 am

 Leave a Reply