How to use FTP under Linux to transfer files

Introduction to FTP

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.

# ftp hostname/IP address

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:

# yum install ftp

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:

  1. American Standard Code for Information Interchange (ASCII) mode: transfers plain files such as text files.
  2. 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 off
ftp> 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.
Related Post