In Unix, how can I issue batches of non-interactive FTP commands?
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:
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_nameReplace 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:
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, to get support for personal or departmental Linux or Unix systems, see At IU, how do I get support for Linux or Unix?
Also see:
- What is anonymous FTP?
- With command line FTP, how can I transfer a group of files without typing out all their names?
- What are some common FTP commands?
- What is FTP, and how do I use it to transfer files?
- Helpful hints for using FTP
Last modified on August 22, 2008.






