- How to use FTP under Linux to transfer files
- FTP Commands
- FTP Transfer Modes
- Transferring Files Using ASCII Mode
- Transferring Files Using Binary Mode
- Transferring Multiple Files
- Linux ftp command
- Syntax
- Options
- Aborting a file transfer
- File naming conventions
- File transfer parameters
- Environment
- Examples
- Related commands
How to use FTP under Linux to transfer files
FTP is a network protocol used for exchanging files over a TCP/IP network. FTP implements user-based password authentication. FTP also allows anonymous user access, where the password is usually a valid email address. You can access a remote system for exchanging files using the ftp command.
If you do not have the ftp command available on your system, you can install it using the package manager available. For Example, for CentOS/RHEL systems:
FTP Commands
Following are some of the frequently used ftp commands:
Command | Description |
---|---|
open | opens a connection with another computer on the network. |
get | transfers a file from the remote system to the local system’s current directory. |
put | transfers a file from the local system to a directory on the remote system. |
mget | transfers multiple files from the remote system to the local system’s current directory. |
mput | transfers multiple files from the local system to a directory on the remote system. |
bye/quit | enable exiting the FTP environment. |
close | Terminates a connection with another computer |
ascii | Sets the mode of file transfer to ASCII |
binary | Sets the mode of file transfer to binary |
cd | Changes directory on the remote machine |
delete | Deletes or removes a file in the current remote directory |
help | Requests a list of all available FTP commands |
lcd | Changes directory on your local machine |
ls | Lists the names of the files in the current remote directory |
mkdir | Makes a new directory within the current remote directory |
pwd | Finds out the path name of the current directory on the remote machine |
rmdir | Removes or deletes a directory in the current remote directory |
prompt | Prompts you to confirm the transfer of each file before completing the transfer. By default, prompting is set to on. |
Note: Note: You can use ? to request for help or additional information about the ftp commands.
FTP Transfer Modes
FTP supports two types of transfer modes:
- American Standard Code for Information Interchange (ASCII) mode: transfers plain files such as text files.
- Binary mode: Binary mode enables you to transfer binary, image, or any nontext files.
Note: In most of the UNIX/Linux distributions the default mode of transfer is ASCII. Therefore, to transfer binary, image, or any nontext files you have to type the bin command to ensure complete data transfer.
Transferring Files Using ASCII Mode
The example Below we will:
1. establish an FTP connection from the host1 system to the host2 system.
2. After the connection is established, we will change the transfer mode to ASCII mode.
3. The we will get the file test1.txt on host2, store the test1.txt file in local directory on host1, and quit the FTP session.
$ ftp host2 Connected to host2. 220 host2 FP server ready. Name (host2:user): user 331 Password required for user. Password: password 230 User user logged in. Remote system type is UNIX. Using binary mode to transfer files.
ftp> ascii 200 Type set to A.
ftp> ls 200 PORT command successful. 150 Opening ASCII mode data connection for file list. test1.txt (directory list truncated) 226 Transfer complete. 133 bytes received in 0.081 seconds (1.61 Kbytes/s)ftp> get test1.txt 200 PORT command successful. 150 Opening ASCII mode data connection for test1.txt (57 bytes). 226 Transfer complete. local: test1.txt remote: test1.txt 66 bytes received in 0.042 seconds (1.54 Kbytes/s)ftp> bye 221-You have transferred 66 bytes in 1 files. 221-Total traffic for this session was 1326 bytes in 4 transfers. 221-Thank you for using the FTP service on host2. 221 Goodbye.Transferring Files Using Binary Mode
The example Below shows how to transfer a binary file.
$ ftp host2 Connected to host2. 220 host2 FTP server ready. Name (host2:user2): user2 331 Password required for user2. Password: 230 User user2 logged in. Remote system type is UNIX.ftp> get binary.file 200 PORT command successful. 150 Opening BINARY mode data connection for binary.file (19084 bytes). 226 Transfer complete. local: binary.file remote: binary.file 19084 bytes received in 0.0044 seconds (4212064 Kbytes/s)Transferring Multiple Files
The example shown below establishes an FTP connection from the host1 system to the host2 system and transfers multiple files by using the prompt, mget, and mput commands.
$ ftp host2 Connected to host2. 220 host2 FTP server ready. Name (host2:user2): user2 331 Password required for user2. Password: 230 User user2 logged in. Remote system type is UNIX. Using binary mode to transfer files.By default the prompt mode is on, when you type the prompt command, it will disable the prompt mode (interactive mode) and you will not be asked for confirmations before you perform any action like get, put etc.
ftp> prompt Interactive mode offftp> mget file.1 file.2 200 PORT command successful. 150 Opening BINARY mode data 226 Transfer complete. 200 PORT command successful. 150 Opening BINARY mode data 226 Transfer complete.ftp> mput file3 file4 200 PORT command successful. 150 Opening BINARY mode data 226 Transfer complete.Linux 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 prompt ‘ftp>’ is provided to the user. The following commands are recognized by ftp:
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:
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:
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
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:
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.
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.
Related commands
scp — Copy files securely over a network connection.
sftp — Conduct an interactive FTP session over a secure network connection.