قالب وردپرس درنا توس
Home / Tips and Tricks / How to Use the FTP Command on Linux

How to Use the FTP Command on Linux

  Stylized Command Prompt for Linux Terminals

The file transfer protocol is older than most of our readers, but it's still going strong. FTP does not provide the security of a modern protocol, but you may still need to use it. That's how it's done.

Warning: Do not use FTP over the Internet

Let's make that clear right from the beginning: File Transfer Protocol (FTP) dates back to the early 1

970s and was written with no regard for security. It does not use encryption for anything. Credentials such as username and password, as well as the data you download or upload, are transmitted in plain text. Everyone on the way can see your secrets. However, FTP is still in use.

When transferring files within your network, you should be on the safe side as long as no one on the network snoops for sensitive documents and makes eavesdropping during transmission. If your files are in no way confidential or confidential, you should use FTP to move them within your internal network. Linux has the standard command line utility ftp to account for this scenario.

But definitely does not use the ftp command to access external resources on the Internet. Use the command-line utility sftp that uses the secure SSH file transfer protocol. We will introduce both programs in this tutorial.

To clarify why you never want to use FTP over the Internet, look at the screenshot below. It shows the FTP password in plain text. Anyone who is on your network or between you and the FTP server on the Internet can easily see that the password is MySecretPassword.

Without encryption, a malicious actor can also modify files that you download or upload during the transfer. [19659005]   Network packet trace with plain-text password

The ftp command

If you have a valid account on an FTP site, you can connect using the following command. In this article, replace the IP address of the commands with the IP address of the FTP server to which you are connecting.


Warning : You should only use the ftp command to connect to servers in a trusted local area network. Use the sftp command below to transfer files over the Internet.

 ftp connection command in a terminal window

The FTP server responds with a welcome message. The wording of the greeting varies from server to server. You will then be asked for the username of the account you are logging in to.

Note that the IP address of the site you are connecting to appears followed by your Linux user name. If your account name on the FTP server matches your Linux user name, just press Enter. This will use your Linux username as the account name on the FTP server. If your Linux user name and FTP account name are different, enter the username of the FTP account and press Enter.

Logging In to the FTP Server

You are prompted to enter your password for the FTP site. Enter your password and press Enter. Your password is not displayed on the screen. When your FTP user account name and password are verified by the FTP server, you are logged in to the FTP server.

The prompt ftp> appears.

 logged into an FTP account in a terminal window

Finding and retrieving files

First, you probably want to get a list of files on the FTP server. That's exactly what the command ls does. Our user sees the file gc.c on the FTP server, and he wants to download it to his own computer. His computer is the "local computer" in the FTP language.

The command to get (or "fetch") a file is get . Our user therefore issues the command get gc.c . You type get a space, and then the name of the file that you want to retrieve.

The FTP server responds by transferring the file to the local computer and confirming the transfer. The size of the file and the time required for the transfer are also displayed.

  Get gc.c 

  FTP file transfer in a terminal window

To retrieve multiple files at once Use the command mget (multiple get). The command mget asks if you want to download the individual files one after the other. Answer by pressing "y" for yes and "n" for no.

This would be boring with a large number of files. Because of this, collections of related files are typically stored on FTP sites as individual tar.gz or tar.bz2 files.

RELATED: How to extract files from a .tar.gz or .tar file. bz2 file under Linux

  mget * .c 

  mget command in a terminal window

Uploading files to the ftp server

Dependent on the permissions, the With your FTP account, you may be able to upload ("set") files to the server. To upload a file, use the command put . In our example, the user uploads a file named Songs.tar.gz to the FTP server.

  Places the songs.tar.gz 

  command in a terminal window [19659005] As you might expect, there is a command to place multiple files on the FTP server at the same time. It is called mput (multiple put). Just as the command mget mput asks for confirmation of "y" or "n" to upload each file one at a time.

The same argument for storing file sets in tar archives applies to dropping files as you would to retrieving files. Our user uploads multiple .odt files with the following command:

  mput * .odt 

  mput command in a terminal window

Creating and modifying directories

If your user account is on the If FTP server allows this, you may be able to create directories. The command to do so is mkdir . To be clear, any directory you create with the command mkdir will be created on the FTP server and not on your local machine.

Use the CD to change the directories on the FTP server. command. When you use the command cd the command prompt ftp> does not change to display your new current directory. The command pwd (print working directory) shows you your current directory.

Our FTP user creates a directory called music, changes to the new directory and confirms where it is by using pwd command command then uploads a file to this directory.

  mkdir music 
  cd music 
  adds songs.tar.gz 

  cd commands for pwd and mkdir into a terminal window

To quickly enter the parent directory of the current directory To get to it, use the command cdup .


  cdup command in a terminal window

access to the local machine

To change the directory on the local machine, you can use the command lcd at the Use Command Prompt ftp> . However, it is easy to lose track of where you are in the local file system. A more convenient way to access the local file system is to use the command! .

The command ! opens a shell window for the local computer. In this shell you can do everything you can do in a standard terminal window. If you type exit you return to the ftp> prompt.

Our user has the command ! and called a shell window on the local machine. You issued a ls command to see what files exist in this directory, and then type the output to return to the ftp> prompt.


! 19659026] ls 

! Shell command in a terminal window

Renaming files

To rename files on the FTP server, use the rename command . Here, our FTP user renames a file with renames and then uses the command ls to list the files in the directory.

  rename songs.tar.gz rock_songs.tar.gz [19659026] ls 

  Rename the command in the terminal window

Delete files

To delete files on the FTP server, use the Command delete . To delete multiple files at the same time, use the command mdelete . You will be prompted for a "y" or "n" to delete each file.

Here our FTP user has listed the files to display their names, and then one has been selected for deletion. They decide to erase them all.

  delete gc.o 
  mdelete * .o 

  ls deletion and deletion commands in a terminal window

Using the sftp Command

] Readers using the IP Addressing System, have determined that the 192.168 address of the FTP server used in the above examples is an internal IP address, also known as a private IP address. As we warned at the beginning of this article, the command ftp should only be used on internal networks.

If you want to connect to a remote server or public FTP server, use the sftp command. Our user will connect to an SFTP account named demo on the publicly available FTP server at test.trebex.net .

When they connect, they are notified Connection has been established. You will also be notified that the authenticity of the host can not be verified. This is normal when you first connect a new host. You press "y" to accept the connection.

Because the user account name ( demo ) was passed on the command line, you are not prompted for the user account name. You will only be prompted for the password. This is entered, verified, and accepted, and they are presented with the command prompt sftp>

  sftp demo@test.rebex.net 

 which connects to a sftp site in a terminal window .

The FTP commands described above work the same way in an SFTP session, with the following exceptions:

  • To delete a file, use rm (FTP uses delete ) [19659084] To delete multiple files, use rm (FTP uses mdelete )
  • To switch to the parent directory, use cd .. (FTP uses cdup )

Our user has used some commands in his SFTP session. You use ls to list the files, and cd to go to the pub directory. You have used the pwd to print the working directory.

 sftp commands in a terminal window

There are other options for transferring files in the Linux world, in particular . scp (secure copy), but we focused on FTP and SFTP here. These two commands are used in the appropriate scenarios to help you and your file storage and recovery needs work well.

Source link