Linux ftp command

Updated: 08/16/2021 by Computer Hope
ftp command

The ftp command runs the classical command-line file transfer client, FTP. It's an interactive text user interface for using the ARPANET (Advanced Research Projects Agency Network) standard File Transfer Protocol. It can transfer files to and from a remote network.

This page describes the version of ftp provided by package management on many GNU/Linux operating systems.

Syntax

ftp [-46pinegvd] [host [port]]

Options

-4 Use only IPv4 to contact any host.
-6 Use IPv6 only.
-p Use passive mode for data transfers. Allows use of ftp in environments where a firewall prevents connections from the outside world back to the client machine. Requires that the ftp server support the PASV command. This is the default if invoked as pftp.
-i Turns off interactive prompting during multiple file transfers.
-n Restrains ftp from attempting "auto-login" upon initial connection. If auto-login is enabled, ftp checks the .netrc (see netrc) file in the user's home directory for an entry describing an account on the remote machine. If no entry exists, ftp prompts for the remote machine login name (default is the user identity on the local machine), and, if necessary, prompt for a password and an account with which to log in.
-e Disables command editing and history support, if it was compiled into the ftp executable. Otherwise, does nothing.
-g Disables file name globbing.
-v Verbose option forces ftp to show all responses from the remote server, and report on data transfer statistics.
-d Enables debugging.

The client host and an optional port number with which ftp is to communicate may be specified on the command line. If this is done, ftp immediately attempts to establish a connection to an FTP server on that host; otherwise, ftp enters its command interpreter and await instructions from the user. When ftp is awaiting commands from the user, the promptftp>’ is provided to the user. The following commands are recognized by ftp:

! [command [args]] Invoke an interactive shell on the local machine. If there are arguments, the first is taken to be a command to execute directly, with the rest of the arguments as its arguments.
$ macro-name [args] Execute the macro macro-name that was defined with the macdef command. Arguments are passed to the macro unglobbed.
account [passwd] Supply a supplemental password required by a remote system for access to resources once a login is completed. If no argument is included, the user is prompted for an account password in a non-echoing input mode.
append local-file [remote-file] Append a local file to a file on the remote machine. If remote-file is left unspecified, the local file name is used in naming the remote file after being altered by any ntrans or nmap setting. File transfer uses the current settings for type, format, mode, and structure.
ascii Set the file transfer type to network ASCII (American Standard Code for Information Interchange). This is the default type.
bell Arrange that a bell be sounded after each file transfer command is completed.
binary Set the file transfer type to support binary image transfer.
bye Terminate the FTP session with the remote server and exit ftp. An end of file also terminates the session and exits.
case Toggle remote computer file name case mapping during mget commands. When case is on (default is off), remote computer file names with all letters in uppercase are written in the local directory with the letters mapped to lowercase.
cd remote-directory Change the working directory on the remote machine to remote-directory.
cdup Change the remote machine working directory to the parent of the current remote machine working directory.
chmod mode file-name Change the permission modes of the file file-name on the remote system to mode.
close Terminate the FTP session with the remote server, and return to the command interpreter. Any defined macros are erased.
cr Toggle carriage return stripping during ascii type file retrieval. Records are denoted by a carriage return/linefeed sequence during ascii type file transfer. When cr is on (the default), carriage returns are stripped from this sequence to conform with the UNIX single linefeed record delimiter. Records on non-UNIX remote systems may contain single linefeeds; when an ascii type transfer is made, these linefeeds may be distinguished from a record delimiter only when cr is off.
qc Toggle the printing of control characters in the output of ASCII type commands. When this is turned on, control characters are replaced with a question mark if the output file is the standard output. This is the default when the standard output is a tty.
delete remote-file Delete the file remote-file on the remote machine.
debug [debug-value] Toggle debugging mode. If an optional debug-value is specified, it is used to set the debugging level. When debugging is on, ftp prints each command sent to the remote machine, preceded by the string ‘-->’.
dir [remote-directory] [local-file] Print a listing of the directory contents in the directory, remote-directory, and, optionally, placing the output in local-file. If interactive prompting is on, ftp prompts the user to verify that the last argument is indeed the target local file for receiving dir output. If no directory is specified, the current working directory on the remote machine is used. If no local file is specified, or local-file is -, output comes to the terminal.
disconnect A synonym for close.
form format Set the file transfer form to format. The default format is "file".
get remote-file [local-file] Retrieve the remote-file and store it on the local machine. If the local file name is not specified, it is given the same name it has on the remote machine, subject to alteration by the current case, ntrans, and nmap settings. The current settings for type, form, mode, and structure are used while transferring the file.
glob Toggle file name expansion for mdelete, mget and mput. If globbing is turned off with glob, the file name arguments are taken literally and not expanded. Globbing for mput is done as in csh. For mdelete and mget, each remote file name is expanded separately on the remote machine and the lists are not merged. Expansion of a directory name is likely to be different from expansion of the name of an ordinary file: the exact result depends on the foreign operating system and ftp server, and can be previewed by doing ‘mls remote-files -’.

Note: mget and mput are not meant to transfer entire directory subtrees of files. That can be done by transferring a tar archive of the subtree (in binary mode).
hash Toggle hash-sign ("#") printing for each data block transferred. The size of a data block is 1024 bytes.
help [command] Print an informative message about the meaning of command. If no argument is given, ftp prints a list of the known commands.
idle [seconds] Set the inactivity timer on the remote server to seconds seconds. If seconds is omitted, the current inactivity timer is printed.
ipany Allow the address resolver to return any address family.
ipv4 Restrict the address resolver to look only for IPv4 addresses.
ipv6 Restrict host addressing to IPv6 only.
lcd [directory] Change the working directory on the local machine. If no directory is specified, the user's home directory is used.
ls [remote-directory] [local-file] Print a listing of the contents of a directory on the remote machine. The listing includes any system-dependent information that the server chooses to include; for example, most UNIX systems produce output from the command ‘ls -l’. (See also nlist.) If remote-directory is left unspecified, the current working directory is used. If interactive prompting is on, ftp prompts the user to verify that the last argument is indeed the target local file for receiving ls output. If no local file is specified, or if local-file is ‘-’, the output is sent to the terminal.
macdef macro-name Define a macro. Subsequent lines are stored as the macro macro-name; a null line (consecutive newline characters in a file or carriage returns from the terminal) terminates macro input mode. There is a limit of 16 macros and 4096 total characters in all defined macros. Macros remain defined until a close command is executed. The macro processor interprets '$' and '\' as special characters. A '$' followed by a number (or numbers) is replaced by the corresponding argument on the macro invocation command line. A '$' followed by an 'i' signals that macro processor that the executing macro is to be looped. On the first pass '$i' is replaced by the first argument on the macro invocation command line, on the second pass it's replaced by the second argument, etc. A '\' followed by any character is replaced by that character. Use the '\' to prevent special treatment of the '$'.
mdelete [remote-files] Delete the remote-files on the remote machine.
mdir remote-files local-file Like dir, except multiple remote files may be specified. If interactive prompting is on, ftp prompts the user to verify that the last argument is indeed the target local file for receiving mdir output.
mget remote-files Expand the remote-files on the remote machine and do a get for each file name thus produced. See glob for details on the file name expansion. Resulting file names are processed according to case, ntrans, and nmap settings. Files are transferred into the local working directory, which can be changed with ‘lcd directory’; new local directories can be created with ‘! mkdir directory’.
mkdir directory-name Make a directory on the remote machine.
mls remote-files local-file Like nlist, except multiple remote files may be specified, and the local-file must be specified. If interactive prompting is on, ftp prompts the user to verify that the last argument is indeed the target local file for receiving mls output.
mode [mode-name] Set the file transfer mode to mode-name. The default mode is "stream" mode.
modtime file-name Show the last modification time of the file on the remote machine.
mput local-files Expand wildcards in the list of local files given as arguments and do a put for each file in the resulting list. See glob for details of file name expansion. Resulting file names are processed according to ntrans and nmap settings.
newer file-name [local-file] Get the file only if the modification time of the remote file is more recent that the file on the current system. If the file does not exist on the current system, the remote file is considered newer. Otherwise, this command is identical to get.
nlist [remote-directory] [local-file] Print a list of the files in a directory on the remote machine. If remote-directory is left unspecified, the current working directory is used. If interactive prompting is on, ftp prompts the user to verify that the last argument is indeed the target local file for receiving nlist output. If no local file is specified, or if local-file is -, the output is sent to the terminal.
nmap [inpattern outpattern] Set or unset the file name mapping mechanism. If no arguments are specified, the file name mapping mechanism is unset. If arguments are specified, remote file names are mapped during mput commands and put commands issued without a specified remote target file name. If arguments are specified, local file names are mapped during mget commands and get commands issued without a specified local target file name. This command is useful when connecting to a non-UNIX remote computer with different file naming conventions or practices. The mapping follows the pattern set by inpattern and outpattern. [Inpattern] is a template for incoming file names (which may have already been processed according to the ntrans and case settings). Variable templating is accomplished by including the sequences '$1', '$2', ..., '$9' in inpattern. Use '\' to prevent this special treatment of the '$' character. All other characters are treated literally, and determine the nmap [inpattern] variable values. For example, given inpattern $1.$2 and the remote file name "mydata.data", $1 would have the value "mydata", and $2 would have the value "data". The outpattern determines the resulting mapped file name. The sequences '$1', '$2', ...., '$9' are replaced by any value resulting from the inpattern template. The sequence '$0' is replace by the original file name. Additionally, the sequence ‘[seq1, seq2]’ is replaced by [seq1] if seq1 is not a null string; otherwise it is replaced by seq2. For example, the command

nmap $1.$2.$3 [$1,$2].[$2,file]

would yield the output file name "myfile.data" for input file names "myfile.data" and "myfile.data.old", "myfile.file" for the input file name "myfile", and "myfile.myfile" for the input file name ".myfile". Spaces may be included in outpattern, as in the example: 'nmap $1 sed "s/ *$//" > $1'. Use the '\' character to prevent special treatment of the '$','[','[', and ',' characters.
ntrans [inchars [outchars]] Set or unset the file name character translation mechanism. If no arguments are specified, the file name character translation mechanism is unset. If arguments are specified, characters in remote file names are translated during mput commands and put commands issued without a specified remote target file name. If arguments are specified, characters in local file names are translated during mget commands and get commands issued without a specified local target file name. This command is useful when connecting to a non-UNIX remote computer with different file naming conventions or practices. Characters in a file name matching a character in inchars are replaced with the corresponding character in outchars. If the character's position in inchars is longer than the length of outchars, the character is deleted from the file name.
open host [port] Establish a connection to the specified host FTP server. An optional port number may be supplied, in which case, ftp attempts to contact an FTP server at that port. If the auto-login option is on (default), ftp attempts to automatically log the user into the FTP server (see below).
prompt Toggle interactive prompting. Interactive prompting occurs during multiple file transfers to allow the user to selectively retrieve or store files. If prompting is turned off (default is on), any mget or mput transfers all files, and any mdelete deletes all files.
proxy ftp-command Execute an ftp command on a secondary control connection. This command allows simultaneous connection to two remote ftp servers for transferring files between the two servers. The first proxy command should be an open, to establish the secondary control connection. Enter the command "proxy ?" to see other ftp commands executable on the secondary connection. The following commands behave differently when prefaced by proxy: open does not define new macros during the auto-login process, close does not erase existing macro definitions, get and mget transfer files from the host on the primary control connection to the host on the secondary control connection, and put, mput, and append transfer files from the host on the secondary control connection to the host on the primary control connection. Third party file transfers depend upon support of the ftp protocol PASV command by the server on the secondary control connection.
put local-file [remote-file] Store a local file on the remote machine. If remote-file is left unspecified, the local file name is used after processing according to any ntrans or nmap settings in naming the remote file. File transfer uses the current settings for type, format, mode, and structure.
pwd Print the name of the current working directory on the remote machine.
quit A synonym for bye.
quote arg1 arg2 ... The arguments specified are sent, verbatim, to the remote FTP server.
recv remote-file [local-file] A synonym for get.
reget remote-file [local-file] reget acts like get, except that if local-file exists and is smaller than remote-file, local-file is presumed to be a partially transferred copy of remote-file and the transfer is continued from the apparent point of failure. If local-file does not exist ftp won't fetch the file. This command is useful when transferring large files over networks that are prone to dropping connections.
remotehelp [command-name] Request help from the remote FTP server. If a command-name is specified, it is supplied to the server as well.
remotestatus [file-name] With no arguments, show status of remote machine. If file-name is specified, show status of file-name on remote machine.
rename [from] [to] Rename the file from on the remote machine, to to.
reset Clear reply queue. This command re-synchronizes command/reply sequencing with the remote ftp server. Resynchronization may be necessary following a violation of the ftp protocol by the remote server.
restart marker Restart the immediately following get or put at the indicated marker. On UNIX systems, marker is usually a byte offset into the file.
rmdir directory-name Delete a directory on the remote machine.
runique Toggle storing of files on the local system with unique file names. If a file already exists with a name equal to the target local file name for a get or mget command, a ".1" is appended to the name. If the resulting name matches another existing file, a ".2" is appended to the original name. If this process continues up to ".99", an error message is printed, and the transfer does not take place. The generated unique file name is reported. Note that runique does not affect local files generated from a shell command (see below). The default value is off.
send local-file [remote-file] A synonym for put.
sendport Toggle the use of PORT commands. By default, ftp attempts to use a PORT command when establishing a connection for each data transfer. The use of PORT commands can prevent delays when performing multiple file transfers. If the PORT command fails, ftp uses the default data port. When the use of PORT commands is disabled, no attempt is made to use PORT commands for each data transfer. This is useful for certain FTP implementations which do ignore PORT commands but, incorrectly, indicate they've been accepted.
site arg1 arg2 ... The arguments specified are sent, verbatim, to the remote FTP server as a SITE command.
size file-name Return size of file-name on remote machine.
status Show the current status of ftp.
struct [struct-name] Set the file transfer structure to struct-name. By default, "stream" structure is used.
sunique Toggle storing of files on remote machine under unique file names. The remote ftp server must support the ftp protocol STOU command for successful completion. The remote server reports unique name. Default value is off.
system Show the type of operating system running on the remote machine.
tenex Set the file transfer type to that needed to talk to TENEX machines.
trace Toggle packet tracing.
type [type-name] Set the file transfer type to type-name. If no type is specified, the current type is printed. The default type is network ASCII.
umask [newmask] Set the default umask on the remote server to newmask. If newmask is omitted, the current umask is printed.
user user-name [password] [account] Identify yourself to the remote FTP server. If the password is not specified and the server requires it, ftp prompts the user for it (after disabling local echo). If an account field is not specified, and the FTP server requires it, the user is prompted for it. If an account field is specified, an account command is relayed to the remote server after the login sequence is completed if the remote server did not require it for logging in. Unless ftp is invoked with "auto-login" disabled, this process is done automatically on initial connection to the FTP server.
verbose Toggle verbose mode. In verbose mode, all responses from the FTP server are displayed to the user. Also, if verbose is on, when a file transfer completes, statistics regarding the efficiency of the transfer are reported. By default, verbose is on.
? [command] A synonym for help.

Command arguments which have embedded spaces may be quoted with quote (") marks.

Aborting a file transfer

To abort a file transfer, use the terminal interrupt key (usually Ctrl-C). Sending transfers are immediately halted. Receiving transfers are halted by sending a ftp protocol ABOR command to the remote server, and discarding any further data received. The speed at which this is accomplished depends upon the remote server's support for ABOR processing. If the remote server does not support the ABOR command, an ‘ftp>’ prompt does not appear until the remote server has completed sending the requested file.

The terminal interrupt key sequence is ignored when ftp has completed any local processing and is awaiting a reply from the remote server. A long delay in this mode may result from the ABOR processing described above, or from unexpected behavior by the remote server, including violations of the ftp protocol. If the delay results from unexpected remote server behavior, the local ftp program must be killed manually.

File naming conventions

Files specified as arguments to ftp commands are processed according to the following rules:

1. If the file name ‘-’ is specified, the stdin (for reading) or stdout (for writing) is used.

2. If the first character of the file name is ‘|’, the remainder of the argument is interpreted as a shell command. ftp then forks a shell, using popen with the argument supplied, and reads (writes) from the stdout (stdin). If the shell command includes spaces, the argument must be quoted; (e.g., "" ls -lt""). A particularly useful example of this mechanism is "dir more".

3. Failing the above checks, if "globbing" is enabled, local file names are expanded according to the rules used in the csh; c.f. the glob command. If the ftp command expects a single local file (e.g., put), only the first file name generated by the "globbing" operation is used.

4. For mget commands and get commands with unspecified local file names, the local file name is the remote file name, which may be altered by a case, ntrans, or nmap setting. The resulting file name may then be altered if runique is on.

5. For mput commands and put commands with unspecified remote file names, the remote file name is the local file name, which may be altered by a ntrans or nmap setting. The resulting file name may then be altered by the remote server if sunique is on.

File transfer parameters

The FTP specification specifies many parameters which may affect a file transfer. The type may be one of "ascii", "image" (binary), "ebcdic", and "local byte size" (for PDP-10's and PDP-20's mostly). ftp supports the ascii and image types of file transfer, plus local byte size 8 for tenex mode transfers.

ftp supports only the default values for the remaining file transfer parameters: mode, form, and struct.

Environment

The ftp command makes use of the following environment variables:

HOME The default location of a .netrc file, if one exists.
SHELL The default shell.

Examples

ftp exampleftp.computerhope.com

The command above instructs ftp to attempt a connection to exampleftp.computerhope.com. Below is an example of what would be seen:

Connecting to exampleftp.computerhope.com...
exampleftp.computerhope.com FTP server ready.
Name: yourusername
Password:

If your username and password are valid and entered correctly, you are successfully logged in:

Remote system type is UNIX.
Using ASCII mode to transfer files.
ftp>

Once at the ftp> prompt, you are placed in the default directory for the user you used to log in. The first thing you'd probably want to do is see what directory that is. To see the present working directory, use the pwd command like in Linux:

ftp> pwd
257 "/ftpdefaultdir" is current directory.

The number 257 is a numerical code. All FTP messages have a code number associated with them, and for technical reasons they are included with the messages from the server. The server lets you know you're in the /ftpdefaultdir directory. Let's see what files are in there, using the ls command:

ftp> ls

This produces a file listing, like in Linux. You can change remote directories with cd. To change what directory you're using on your local computer, you can use lcd for "local change directory." Let's say you want to get a file from the server named awesome.jpg, and download it to your local directory /home/myuser/images:

ftp> lcd /home/myuser/images
Local directory now /home/myuser/images
ftp> get awesome.jpg
local: awesome.jpg remote: awesome.jpg
200 PORT command successful.
150 Opening ASCII mode data connection for awesome.jpg (352271 bytes).

Oops! That's not quite right. JPEG (Joint Photographic Experts Group) images are binary files, not ASCII (text) files.

FTP supports two different types of file transfers, ASCII and binary. At login, the server told us it was currently in ASCII mode. Let's change that to binary:

ftp> bin
Using binary mode to transfer files.

bin is short for binary, and either command switches to binary mode. We can now do the same file transfer and the file comes through correctly.

Let's switch to the remote directory all-images.

ftp> cd all-images
250-README for all-images
250-
250-This folder contains all the JPEG images for our project.
250-
250 CWD command successful.

This directory had a "README" message displayed by the FTP server every time you change it to your current directory. The server then lets you know the cd command was successful. Now let's download every JPEG file using a wildcard. We can use the mget command, which allows us to get multiple files with one command:

ftp> mget *.jpg *.jpeg *.JPG *.JPEG

We now get all the jpeg files with the extensions JPG, JPEG, jpg, or jpeg.

If we have any files to upload to the server, we can use the commands put or mput to upload them. When we're done, we can logout using the exit command.

scp — Copy files securely over a network connection.
sftp — Conduct an interactive FTP session over a secure network connection.