Upload and Download files on Linux Shell using FTP command

Linux offers a powerful tool for file transfer on the shell: the FTP command. FTP (File Transfer Protocol) is a standard network protocol for transferring files between a client and server on a computer network. This article shows how to use the Linux FTP command to upload and download files using the Linux command-line interface, also known as shell or bash.

Understanding the Basics

What is FTP?

FTP is a file transfer protocol; it operates on a client-server model. The client initiates a connection to the server, and once the connection is established, files can be uploaded or downloaded. FTP uses two channels: the command channel (for controlling the conversation) and the data channel (for transmitting file content).

Installing FTP client

Most Linux distributions come with an FTP client pre-installed. If not, it can be installed using the package manager. For example, on Debian-based systems like Ubuntu, you can install it with:

sudo apt-get install ftp

On CentOS, AlmaLinux and Rocky Linux, you can install FTP with yum or dnf:

dnf install ftp

Connecting to an FTP Server

To start an FTP session, open your terminal and type:

ftp [hostname]

Replace [hostname] with the domain name or IP address of the FTP server. You'll be prompted to enter your username and password.

Once connected, you can navigate directories using standard shell commands:

  • ls - List files in the current directory.
  • cd - Change directory.

Uploading Files

To upload a file to the FTP server, use the put command:

put [local_file] [remote_file]

Replace [local_file] with the name of the file on your local machine, and [remote_file] with the desired name on the server.

Example:

put myfile.txt upload/myfile.txt

This command uploads myfile.txt from your local machine to the upload directory on the server.

Downloading Files

To download a file, use the get command:

get [remote_file] [local_file]

Replace [remote_file] with the name of the file on the server, and [local_file] with the desired name on your local machine.

Example:

get download/myfile.txt myfile.txt

This downloads myfile.txt from the download directory of the server to your local machine.

Advanced Commands

  • mget and mput - Transfer multiple files.
  • binary - Switch to binary mode for transferring binary files.
  • ascii - Switch to ASCII mode for text files.

Closing the Connection

To exit the FTP session, type:

bye

Security Considerations

FTP transmits data in plain text, including credentials. For secure file transfer, consider using SFTP (SSH File Transfer Protocol) or FTPS (FTP Secure), which is FTP over a secure TLS connection.

Frequently Asked Questions

What is the FTP command in Linux?

Answer: The FTP (File Transfer Protocol) command in Linux is a standard network protocol used to transfer files between a client and a server over a network. It allows users to upload and download files, navigate directories, and perform other file management tasks through the command line.

How do I install the FTP command on Linux?

Answer: On most Linux distributions, the FTP client is pre-installed. If it's not available, you can install it using your distribution's package manager. For instance, on Debian-based systems like Ubuntu, use sudo apt-get install ftp.

How do I connect to an FTP server using the command line?

Answer: Use the command ftp [hostname], replacing [hostname] with the server's domain name or IP address. After this, you will be prompted to enter your username and password to establish the connection.

Can I transfer multiple files at once using FTP?

Answer: Yes, you can use the mget command to download multiple files and the mput command to upload multiple files. These commands allow you to transfer several files simultaneously.

How do I switch between ASCII and binary transfer modes in FTP?

Answer: Use the command ascii to switch to ASCII mode for transferring text files and binary for transferring binary files like images or videos. It's important to use the correct mode to prevent file corruption.

Is it safe to transfer files using FTP?

Answer: FTP transfers data, including login credentials, in plain text, which can be intercepted and read by unauthorized parties. For secure file transfer, consider using SFTP (SSH File Transfer Protocol) or FTPS (FTP Secure), which provide encrypted connections.

How do I navigate directories in an FTP session?

Answer: Use the command cd [directory] to change to a specific directory and ls to list the files in the current directory. These commands help you navigate the file system of the FTP server.

How can I resume a broken file transfer in FTP?

Answer: FTP does not natively support resuming broken file transfers. For this functionality, you would need to use more advanced protocols like SFTP or FTP clients with resume capabilities.

What is the difference between put and get commands in FTP?

Answer: The put command is used to upload a file from your local machine to the FTP server, whereas the get command is used to download a file from the FTP server to your local machine.

How do I exit an FTP session?

Answer: To exit an FTP session, type the command bye or quit. This will close the connection to the FTP server and return you to your local shell prompt.

Can I change file permissions via FTP?

Answer: Some FTP servers allow you to change file permissions using the chmod command, but this is not universally supported. It depends on the server's configuration and your permissions as a user.

Conclusion

The FTP command in Linux is an easy-to-use tool for file transfer. However, always be mindful of security implications when transmitting sensitive data, as FTP protocol is unencrypted, so it is better to use FTPS or SFTP if you transfer files that may contain sensitive data over the internet.

Leave a Comment