How to Use the Command-Line FTP Client in Linux Distributions

By Emmett Dulaney

Knowing how to use FTP from the command line is a good idea when working with Linux systems. If your GUI desktop isn’t working, for example, you may need to download some files to fix the problem; you can do so if you know how to use the command-line FTP client. The command-line FTP client is available in all Linux distributions, and using it isn’t difficult.

The best way to figure out the command-line FTP client is to try it. The command is ftp, and you can try the ftp commands from your Linux system. You don’t even need an Internet connection, because you can use the ftp command to connect to your own Linux system.

Your output from the ftp command may be different from what you see here because some distributions, such as Debian, use a text=mode version of gFTP as the command-line FTP client.

In the following sample FTP session, the command-line FTP client was used to log in and browse the directories on a Linux system. Here’s the listing, illustrating interaction with a typical command-line FTP client:

ftp localhost
Connected to localhost.localdomain.
220 (vsFTPd 2.0.3)
Name (localhost:jdoe): (press Enter.)
331 Please specify the password.
Password: (enter the password for the user.)
230 Login successful.
Remote system type is Unix.
Using binary mode to transfer files.
ftp> help
Commands may be abbreviated. Commands are:
! debug mdir qc send
$ dir mget sendport site
account disconnect mkdir put size
append exit mls pwd status
ascii form mode quit struct
bell get modtime quote system
binary glob mput recv sunique
bye hash newer reget tenex
case help nmap rstatus tick
cd idle nlist rhelp trace
cdup image ntrans rename type
chmod lcd open reset user
close ls prompt restart umask
cr macdef passive rmdir verbose
delete mdelete proxy runique ?
ftp> help mget (to get help on a specific command.)
mget get multiple files
ftp> cd /var (This changes directory to /var.)
250 Directory successfully changed.
ftp> ls (This command lists the contents of the directory.)
227 Entering Passive Mode (127,0,0,1,38,142)
150 Here comes the directory listing.
. . . lines deleted . . .
226 Directory send OK.
ftp> bye(This command ends the session.)

As the listing shows, you can start the command-line FTP client by typing the command ftp hostname, where hostname is the name of the system you want to access. When the FTP client establishes a connection with the FTP server at the remote system, the FTP server prompts you for a username and password. After you supply the information, the FTP client displays the ftp> prompt, and you can begin typing commands to perform specific tasks. If you can’t remember a specific FTP command, type help to view a list of commands. You can get additional help for a specific command by typing help command, where command is the command for which you want help.

Many FTP commands are similar to the Linux commands for navigating the file system. The command cd changes the directory, for example; pwd prints the name of the current working directory; and ls lists the contents of the current directory. Two other common Linux commands are get, which downloads a file from the remote system to your system, and put, which uploads (sends) a file from your system to the remote host.

The table below describes some commonly used FTP commands. You don’t have to type the entire FTP command. For a long command, you have to type only the first few characters — enough to identify the command uniquely. To delete a file, for example, you can type dele; to change the file transfer mode to binary, you can type bin.


Common FTP Commands for Linux Distributions
Command Description
! Executes a shell command on the local system. !ls, for example, lists the contents of the current directory on the local system.
? Displays a list of commands (same as help).
append Appends a local file to a remote file.
ascii Sets the file transfer type to ASCII (or plain text). This command is the default file transfer type.
binary Sets the file transfer type to binary.
bye Ends the FTP session with the remote FTP server and quits the FTP client.
cd Changes the directory on the remote system. cd /pub/Linux, for example, changes the remote directory to /pub/Linux.
chmod Changes the permission settings of a remote file. chmod 644 index.html, for example, changes the permission settings of the index.html file on the remote system.
close Ends the FTP session with the FTP server and returns to the FTP client’s prompt.
delete Deletes a remote file. delete bigimage.jpg, for example, deletes that file on the remote system.
dir Lists the contents of the current directory on the remote system.
disconnect Ends the FTP session and returns to the FTP client’s prompt. (This command is the same as close.)
get Downloads a remote file. get junk.tar.gz junk.tgz, for example, downloads the file junk.tar.gz from the remote system and saves it as the file junk.tgz on the local system.
hash Turns on or off the hash-mark (#) printing that shows the progress of the file transfer. When this feature is turned on, a hash mark prints onscreen for every 1,024 bytes transferred from the remote system. (This feature is the command-line version of a progress bar.)
help Displays a list of commands.
image Same as binary.
lcd Changes the current directory on the local system. lcd/var/ftp/pub, for example, changes the current local directory to /var/ftp/pub.
ls Lists the contents of the current remote directory.
mdelete Deletes multiple files on a remote system. mdelete *.jpg, for example, deletes all remote files with names ending in .jpg in the current directory.
mdir Lists multiple remote files and saves the listing in a specified local file. mdir /usr/share/doc/w* wlist, for example, saves the listing in the local file named wlist.
mget Downloads multiple files. mget *.jpg, for example, downloads all files with names ending in .jpg. If the prompt is turned on, the FTP client asks for confirmation before downloading each file.
mkdir Creates a directory on the remote system. mkdir images, for example, creates a directory named images in the current directory on the remote system.
mls Same as mdir.
mput Uploads multiple files. mput *.jpg, for example, sends all files with names ending in .jpg to the remote system. If the prompt is turned on, the FTP client asks for confirmation before sending each file.
open Opens a connection to the FTP server on the specified host. open, for example, connects to the FTP server on the host
prompt Turns the prompt on or off. When the prompt is on, the FTP client prompts you for confirmation before downloading or uploading each file during a multiple-file transfer.
put Sends a file to the remote system. put index.html, for example, sends the index.html file from the local system to the remote system.
pwd Displays the full pathname of the current directory on the remote system. When you log in as a user, the initial current working directory is your home directory.
quit Same as bye.
recv Same as get.
rename Renames a file on the remote system. rename old.html new.html, for example, renames the file old.html to new.html on the remote system.
rmdir Deletes a directory on the remote system. rmdir images, for example, deletes the images directory in the current directory of the remote system.
send Same as put.
size Shows the size of a remote file. size bigfile.tar.gz, for example, shows the size of that remote file.
status Shows the current status of the FTP client.
user Sends new user information to the FTP server. User jdoe, for example, sends the username jdoe; then the FTP server prompts for the password for that username.

When downloading files from the Internet, you almost always want to transfer the files in binary mode, because the software is usually archived and compressed in binary form. (Its files aren’t plain-text files.) Always use the binary command to set the mode to binary; then use the get command to download the files on your Linux system.

When transferring multiple files with similar names (such as image1.jpg, image2.jpg, and so on), type prompt to turn off prompting. (Otherwise, the FTP client asks you after each file transfer whether you want to transfer the next file.) Then type mget followed by the filename with a wildcard character. To download all files with names starting with image and ending with the .jpg extension, for example, type mget image*.jpg.