ARCHIVED: In Unix, how can I issue batches of non-interactive FTP commands?

This content has been archived, and is no longer maintained by Indiana University. Information here may no longer be accurate, and links may no longer be available or reliable.

In Unix, you can use the ftp command in combination with a brief shell script to automate an FTP session. For example, if your email address were dvader@indiana.edu and you wanted to retrieve a listing of a directory named /pub/docs/plans on a host named deathstar.empire.org, you could use the following script:

  #!/bin/sh
  ftp -n deathstar.org <<EOT | mailx -s "Your Listing" dvader@indiana.edu
  user anonymous dvader@indiana.edu
  cd /pub/docs/plans
  dir
  quit
  EOT

The first line indicates the file is a script. The second line invokes the ftp command and directs the output of the session to the mailx command. If mailx isn't on your system, try using mail or mhmail instead. The third line lists the login and password for an anonymous FTP connection. The following two lines contain the ftp commands that the script will execute on the remote host; you could substitute any valid ftp commands of your own before the word quit. Finally, once the commands have been executed, the output will be mailed in a message to dvader@indiana.edu with the subject Your Listing, as specified in the second line of the script.

To use the script, enter:

  sh script_name

Replace script_name with the name of the file containing the text of the script. If you would like to run the script in the background so that you don't have to wait for it to finish to do other work, enter:

  sh script_name &

You will receive an error message if, in your script, you refer to directories or files that don't actually exist. In the example here, we assumed that dvader already knew that the directory named /pub/docs/plans existed on the remote host.

At Indiana University, for personal or departmental Linux or Unix systems support, see Get help for Linux or Unix at IU.

This is document afqg in the Knowledge Base.
Last modified on 2018-01-18 09:59:28.