FTP

By | 2005-11-01

The File Transfer Protocol (FTP) is the standard network protocol used for the transfer of computer files between a client and server on a computer network.

Syntax

FTP [-options] [-s:filename] [-w:buffer] [host]

Key

switchdescription
-s:filenameRun a text file containing FTP commands.
hostHost name or IP address of the remote host.
-gDisable filename wildcards.
-nNo auto-login.
-iNo interactive prompts during ftp.
-vHide remote server responses.
-w:bufferSet buffer size to buffer (default=4096)
-dDebug
-aUse any local interface when binding data connection.

FTP Commands

These are commands that can be run while in an FTP session.

commanddescription
append local-file [remote-file]Append a local file to a file on the remote computer.
asciiSet the file transfer type to ASCII, the default. In ASCII text mode, character-set and end-of-line characters are converted as necessary.
bellToggle a bell to ring after each command. By default, the bell is off.
binarySet the file transfer type to binary. Use “Binary” for transferring executable program files or binary data files e.g. Oracle
byeEnd the FTP session and exit ftp
cdChange the working directory on the remote host.
closeEnd the FTP session and return to the cmd prompt.
debugToggle debugging. When debug is on, FTP will display every command.
delete remote-fileDelete file on remote host.
dir [remote-directory] [local-file]List a remote directory’s files and subdirectories. (or save the listing to local-file)
disconnectDisconnect from the remote host, retaining the ftp prompt.
get remote-file [local-file]Copy a remote file to the local PC.
globToggle the use of wildcard characters in local pathnames. By default, globbing is on.
hashToggle printing a hash (#) for each 2K data block transferred. By default, hash mark printing is off.
help [command]Display help for ftp command.
lcd [directory]Change the working directory on the local PC. By default, the working directory is the directory in which ftp was started.
literal argument [ …]Send arguments, as-is, to the remote FTP host.
ls [remote-directory] [local-file]List a remote directory’s files and folders. (short format)
mdelete remote-files [ …]Delete files on remote host.
mdir remote-files [ …] local-fileDisplay a list of a remote directory’s files and subdirectories. (or save the listing to local-file) Mdir allows you to specify multiple files.
mget remote-files [ …]Copy multiple remote files to the local PC.
mkdir directoryCreate a directory on the remote host.
mls remote-files [ …] local-fileList a remote directory’s files and folders. (short format)
mput local-files [ …]Copy multiple local files to the remote host.
open computer [port]Connects to the specified FTP server.
promptToggle prompting. Ftp prompts during multiple file transfers to allow you to selectively retrieve or store files; mget and mput transfer all files if prompting is turned off. By default, prompting is on.
put local-file [remote-file]Copy a local file to the remote host.
pwdPrint Working Directory (current directory on the remote host)
quitEnd the FTP session with the remote host and exit ftp.
quote argument [ …]Send arguments, as-is, to the remote FTP host.
recv remote-file [local-file]Copy a remote file to the local PC.
remotehelp [command]Display help for remote commands.
rename filename newfilenameRename remote files.
rmdir directoryDelete a remote directory.
send local-file [remote-file]Copy a local file to the remote host.
statusDisplay the current status of FTP connections and toggles.
traceToggles packet tracing; trace displays the route of each packet
type [type-name]Set or display the file transfer type “binary” or “ASCII” (the default). If type-name is not specified, the current type is displayed. ASCII should be used when transferring text files. In ASCII text mode, character-set and end-of-line characters are converted as necessary. Use “Binary” for transferring executable files.
user user-name [password] [account]Specifes a user to the remote host.
verboseToggle verbose mode. By default, verbose is on.
! commandRun command on the local PC.
? [command]Display help for ftp command.An example FTP Script to retrieve files in binary and ascii mode

Scripting FTP

It is easily possible to automate an FTP session. However, be warned that credentials are stored in plain text, so it inherently insecure. Here is a sample script:

::GetFiles.ftp
[User_id]
[ftp_password] binary
get /usr/file1.exe
get file2.html
mget *.jpeg ascii
mget *.txt quit

To run the above script:

FTP -s:GetFiles.ftp [hostname]

This will connect as the user:User_id with password:ftp_password

An FTP Script to publish files in binary mode:

::PutFiles.ftp
[User_id]
[ftp_password] binary
mput *.html
cd images
mput *.gif quit

To run the above script:

FTP -s:PutFiles.ftp [hostname]

This will connect as the user:User_id with password:ftp_password

This can be further automated by constructing the FTP file using a series of ECHO commands. Also you may want to put the main FTP command inside a batch script, which also CD’s to the correct local folder before transferring any files.

If you do use this automation method, don’t forget to delete/protect the script file if it contains a valid password.

Author: dwirch

Derek Wirch is a seasoned IT professional with an impressive career dating back to 1986. He brings a wealth of knowledge and hands-on experience that is invaluable to those embarking on their journey in the tech industry.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.