How to use FTP command in Linux to transfer files

FTP (File Transfer Protocol) is a standard network protocol used to transfer files to and from a remote network.

In this tutorial, we will show you how to use the ftp command on linux with practical examples.

In most cases, you will use a desktop FTP client to connect to a remote server and upload or download files. However, the command ftp useful when you are working on a server without a GUI and you want to transfer files via FTP or from a remote server.

When transferring data via ftp, both connections are not encrypted. For secure data transfer, use the SFTP protocol.

To be able to copy files, you must have at least read permissions on the source file and write permissions on the target system.

When transferring large files, it is recommended to run FTP commands in a screen or tmux session.

The directory from which you run the command ftp to your local working directory.

  1. Open FTP connections to a remote system using the command ftp with a remote server, type the IP address or domain name:
    ftp 192.168.42.77

  2. If a connection is established, a confirmation message will appear and you will be prompted for an FTP username, in this example the FTP username andreyex:
    220---------- Welcome to Pure-FTPd [privsep] [TLS] ----------
    220-You are user number 1 of 50 allowed.
    220-Local time is now 21:35. Server port: 21.
    220-This is a private system - No anonymous login
    220-IPv6 connections are also welcome on this server.
    220 You will be disconnected after 15 minutes of inactivity.
    Name (192.168.42.77:localuser): andreyex

    Depending on the FTP service running on the remote server, a different confirmation message may appear.

  3. After entering your username, you will be prompted for a password:
    Password: 

  4. If the password is correct, the remote server will display a confirmation message and a prompt ftp>
    230 OK. Current restricted directory is /
    Remote system type is UNIX.
    Using binary mode to transfer files.
    ftp>

    Copy

If the FTP server you are accessing accepts anonymous FTP accounts and you want to log in as an anonymous user, Use anonymous as username and your email address as password.

Many FTP commands are similar or identical to the commands you enter at the Linux shell command line.

Following are some of the most common FTP commands

  • help or ? – list of all available FTP commands
  • cd – change directory on the remote computer
  • lcd – change directory on local computer
  • ls – list of filenames and directories in the current remote directory
  • mkdir – create a new directory in the current remote directory
  • pwd – print the current working directory on the remote computer
  • delete – delete a file in the current remote directory
  • rmdir– delete a directory in the current remote directory
  • get – copy one file from remote computer to local computer
  • mget – copy multiple files from a remote computer to a local computer
  • put – copy one file from local computer to remote
  • mput – copy one file from local computer to remote

After logging in, the current working directory will be displayed – this is the home directory of remote users.

When uploading files using the command ftp, the files will be uploaded to the directory from which you typed the command ftp

If you want to upload files to a different local directory, switch to it using the command lcd

Let’s say we want to download files to a directory ~/ftp_downloads :

lcd ~/ftp_downloads

To download one file from a remote server, use the command get … For example, to download a file backup.zip , use the following command:

get backup.zip

The output should look something like this:

200 PORT command successful
150-Connecting to port 60609
150 6516.9 kbytes to download
226-File successfully transferred
226 2.356 seconds (measured here), 2.70 Mbytes per second
6673256 bytes received in 2.55 seconds (2.49 Mbytes/s)

To upload multiple files at once, use the command mget… You can specify a list of individual filenames or use wildcards.

mget backup1.zip backup2.zip

When uploading multiple files, you will be prompted for confirmation for each file.

mget backup1.zip? y
200 PORT command successful
150 Connecting to port 52231
226-File successfully transferred
226 0.000 seconds (measured here), 31.51 Kbytes per second
14 bytes received in 0.00058 seconds (23.6 kbytes/s)
mget backup2.zip? y
200 PORT command successful
150-Connecting to port 59179
150 7.2 kbytes to download
226-File successfully transferred
226 0.000 seconds (measured here), 16.68 Mbytes per second
7415 bytes received in 0.011 seconds (661 kbytes/s)

As soon as you finish downloading files from the remote FTP – the server closes the connection with bye or quit

quit

221-Goodbye. You uploaded 0 and downloaded 6544 kbytes.
221 Logout.

To upload a file from a local directory to a remote FTP server, use the command put:

put image.jpg

The output should look something like this:

200 PORT command successful
150 Connecting to port 34583
226-File successfully transferred
226 0.849 seconds (measured here), 111.48 Kbytes per second
96936 bytes sent in 0.421 seconds (225 kbytes/s)

If you want to download a file that is not in your current working directory, use an absolute file path.

To upload multiple files from a local directory to a remote FTP server, use the command mput:

mput image_andreyex.jpg image2.jpg

mput image_andreyex.jpg? y
200 PORT command successful
150 Connecting to port 41075
226-File successfully transferred
226 1.439 seconds (measured here), 114.32 Kbytes per second
164204 bytes sent in 0.76 seconds (216 kbytes/s)
mput image2.jpg? y
200 PORT command successful
150 Connecting to port 40759
226-File successfully transferred
226 1.727 seconds (measured here), 111.75 Kbytes per second
195836 bytes sent in 1.25 seconds (141 kbytes/s)

When uploading multiple files, you will be prompted for confirmation for each file you want to upload.

As soon as you finish uploading files to the remote FTP server, the server closes the connection with bye or quit

In this article, you learned how to use the ftp command to upload and transfer files to a remote FTP server.

Please disable your ad blocker or whitelist this site!

Related Posts