31.01.2017

Basic FTP Commands in Linux

Basic FTP Commands in Linux

FTP (File Transfer Protocol - is a popular network protocol that is used to copy files from one computer to another on the local network or on the Internet. FTP is one of the oldest application protocols that appeared long before HTTP, and even before TCP / IP, in 1971.

The FTP protocol is vulnerable, that is, FTP cannot encrypt its traffic, all transmissions are in plain text, so user names, passwords, commands and data can be read by anyone who can intercept the packet over the network. For secure data transfer SFTP (Secure File Transfer Protocol) is used. Unlike standard FTP, it encrypts both commands and data, protecting passwords and confidential information from open transmission over the network. In terms of functionality, SFTP is similar to FTP, but since it uses a different protocol, standard FTP clients cannot communicate with the SFTP server and vice versa. Next, consider the basic commands for working with an FTP program.

FTP connection

FTP client is included in most Linux distributions. Let's start by launching the program and ftp connection and, of course, consider the basic commands for downloading from ftp server and uploading to ftp, creating directories, deleting files, etc. In this article we describe only the basic commands, and at the end of the article we give a help and manual from the console - you can always find out about the purpose of the command and its syntax, as well as about all available commands on a specific ftp server.

To start FTP connections, just enter the command ftp <server> for example:

ftp test.hostingthutor.com

After pressing enter, the output of the command will be as follows:

Connected to test.hostinghutor.com (114.55.5.11).
220 test.hostinghutor.com FTP Server ready.
Name (test.hostinghutor.com:ftpuser):

Another way to connect is to start ftp from the console, and then connect to the ftp server using the command open:

ftp
ftp> test.hostinghutor.com

In addition, it is possible to connect via ip:

ftp  114.55.5.11

Or in this way ftp user@ftp.site.com, that is:

ftp ftpuser@test.hostinghutor.com

Next, you need to enter the username and password of the ftp connection..

After successful authorization there will be a message of this type:

230 User ftpuser logged in
Remote system type is UNIX.
Using binary mode to transfer files.                                           
ftp>

It can be seen from the message that a binary (binary) type of transfer is used to transfer files. The binary file transfer mode is the transfer of files in the form in which they are stored on the FTP server. Ascii mode (text) is used to transfer only text files. You can enter ascii or binary commands to switch between transfer modes. Binary mode must be used for all non-text file types - images, archives, programs, etc.

Commands for Navigation

So, let's move on to the commands for navigating the directories of the ftp server

pwd - the command will show the current directory on the ftp server:

ftp> pwd
257 "/" is the current directory

ls - the command will show a list of files and directories in the current directory:

ftp> ls
227 Entering Passive Mode.
150 Opening ASCII mode data connection for file list
-rw-r--r--   1 ftpuser  ftpuser  3034978 Jun 31 19:02 file1.tar.gz
-rw-r--r--   1 ftpuser  ftpuser  30842294 Jul 31 20:08 file2.tar.gz
-rw-r--r--   1 ftpuser  ftpuser  67798316 Jul 31 19:46 file3.tar.gz
-rw-r--r--   1 ftpuser  ftpuser  6001252 Jan 17 12:02 file4.zip
-rw-r--r--   1 ftpuser  ftpuser  31386394 Jan 17 11:28 file5.tar.gz
drwxr-xr-x   2 ftpuser  ftpuser         4 Jan 17 20:23 www
-rw-r--r--   1 ftpuser  ftpuser  48546694 Jan 17 11:33 file6.zip
226 Transfer complete

cd <directory name> - command to go to the desired directory:

ftp> cd www
250 CWD command successful

Check with the pwd command:

ftp> pwd
257 "/www" is the current directory

mkdir <directory name> - creating a new directory (folder):

ftp> mkdir tmp
257 "/tmp" - Directory successfully created

rmdir <directory name> - delete directory (folder):

ftp> rmdir tmp
250 RMD command successful

Delete files on ftp server

delete <filename> - deletes a file on a remote ftp server:

ftp> delete test1.sql
250 DELE command successful

Download files from FTP

get - download the file to the local machine. get fileName or get fileName newFileName

ftp> get file.zip
local: file.zip remote: file.zip
227 Entering Passive Mode.
150 Opening BINARY mode data connection for file.zip (486694 bytes)
226 Transfer complete
486694 bytes received in 0.229 secs (6.5e+04 Kbytes/sec)

Download file.zip to the local machine as file2.zip:

ftp> get file.zip file2.zip
local: file2.zip remote: file.zip
227 Entering Passive Mode .
150 Opening BINARY mode data connection for file.zip (486694 bytes)
226 Transfer complete
486694 bytes received in 0.306 secs (9.4e+04 Kbytes/sec)

Using the get command from a remote ftp server, files are copied to the current local directory. To change the current local directory you need to use the lcd command:

lcd <path> - change the current directory on the local machine:

ftp> lcd /root
Local directory now /root

To download several files from a remote ftp server to a local machine, you can use the mget command:

ftp> mget *.sql
mget test2.sql? y
227 Entering Passive Mode.
150 Opening BINARY mode data connection for test2.sql (23957080 bytes)
226 Transfer complete
23957080 bytes received in 0.233 secs (1e+05 Kbytes/sec)
mget test1.sql? y
227 Entering Passive Mode.
150 Opening BINARY mode data connection for test1.sql (11873185 bytes)
226 Transfer complete
11873185 bytes received in 0.135 secs (8.6e+04 Kbytes/sec)

Download of each file must be confirmed (yes / no) y/n.

Another way to download with mget:

ftp> mget test1.sql test2.sql
mget test1.sql? y
227 Entering Passive Mode.
150 Opening BINARY mode data connection for test1.sql (11873185 bytes)
226 Transfer complete
11873185 bytes received in 0.101 secs (1.1e+05 Kbytes/sec)
mget test2.sql? y
227 Entering Passive Mode.
150 Opening BINARY mode data connection for test2.sql (23957080 bytes)
226 Transfer complete
23957080 bytes received in 0.204 secs (1.1e+05 Kbytes/sec)

Upload files to ftp server

put <filename> - command to upload one file to ftp server:

ftp> put test1.sql
local: test1.sql remote: test1.sql
227 Entering Passive Mode.
150 Opening BINARY mode data connection for test1.sql
226 Transfer complete
11873185 bytes sent in 0.129 secs (9e+04 Kbytes/sec)

To upload multiple files at once, you can use the mput command:

ftp> mput test1.sql test2.sql
mput test1.sql? y
227 Entering Passive Mode.
150 Opening BINARY mode data connection for test1.sql
226 Transfer complete
11873185 bytes sent in 0.0964 secs (1.2e+05 Kbytes/sec)
mput test2.sql? y
227 Entering Passive Mode.
150 Opening BINARY mode data connection for test2.sql
226 Transfer complete
23957080 bytes sent in 0.354 secs (6.6e+04 Kbytes/sec)

The upload of each file must be confirmed by y / n (yes / no).

Another way to upload with mput:

ftp> mput *.sql
mput test1.sql? y
227 Entering Passive Mode.
150 Opening BINARY mode data connection for test1.sql
226 Transfer complete
11873185 bytes sent in 0.0985 secs (1.2e+05 Kbytes/sec)
mput test2.sql? y
227 Entering Passive Mode.
150 Opening BINARY mode data connection for test2.sql
226 Transfer complete
23957080 bytes sent in 0.2 secs (1.2e+05 Kbytes/sec)

If large files are uploaded to ftp, it would be nice to watch the upload progress. You can use the hash and tick commands for this.

hash - the command after which ftp will print the "#" character every 1024 bytes of data:

ftp> hash
Hash mark printing on (1024 bytes/hash mark).
put file2.tar.gaz
##########################
226 Transfer complete
785888111 bytes sent in 6.94 secs (1.1e+05 Kbytes/sec)

tick - the command will display a byte counter:

ftp> tick
Hash mark printing off.
Tick counter printing on (10240 bytes/tick increment).
ftp> put file2.tar.gz
local: file2.tar.gz remote: file2.tar.gz
227 Entering Passive Mode.
150 Opening BINARY mode data connection for file2.tar.gz
Bytes transferred: 912706618 //-> counter
226 Transfer complete
912706618 bytes sent in 8.08 secs (1.1e+05 Kbytes/sec)

That's the whole basic set of commands for working with ftp in the console. To view the list of available commands on this FTP server, you can use the help command:

ftp> help
Commands may be abbreviated.  Commands are:

!               debug           mdir            sendport        site
$               dir             mget            put             size
account         disconnect      mkdir           pwd             status
append          exit            mls             quit            struct
ascii           form            mode            quote           system
bell            get             modtime         recv            sunique
binary          glob            mput            reget           tenex
bye             hash            newer           rstatus         tick
case            help            nmap            rhelp           trace
cd              idle            nlist           rename          type
cdup            image           ntrans          reset           user
chmod           lcd             open            restart         umask
close           ls              prompt          rmdir           verbose
cr              macdef          passive         runique         ?
delete          mdelete         proxy           send   

You can also get a short help for each command help <command>:

ftp> help status
status          show current status 

ftp> help quit
quit            terminate ftp session and exit 

ftp> help bye
bye             terminate ftp session and exit

And finally, two commands quit and bye to close the ftp session and exit:

ftp> quit
221 Goodbye. 

Detailed information describing the commands available through man ftp at a command line:

# man ftp
Formatting page, please wait...
FTP(1)                    BSD General Commands Manual                   FTP(1)

NAME
     ftp - Internet file transfer program

SYNOPSIS
     ftp [-Apinegvd] [host]
     pftp [-Apinegvd] [host]
................
...............