Linux file transfer ftp

How to use FTP under Linux to transfer files

FTP is a network protocol used for exchanging files over a TCP/IP network. FTP implements user-based password authentication. FTP also allows anonymous user access, where the password is usually a valid email address. You can access a remote system for exchanging files using the ftp command.

If you do not have the ftp command available on your system, you can install it using the package manager available. For Example, for CentOS/RHEL systems:

FTP Commands

Following are some of the frequently used ftp commands:

Command Description
open opens a connection with another computer on the network.
get transfers a file from the remote system to the local system’s current directory.
put transfers a file from the local system to a directory on the remote system.
mget transfers multiple files from the remote system to the local system’s current directory.
mput transfers multiple files from the local system to a directory on the remote system.
bye/quit enable exiting the FTP environment.
close Terminates a connection with another computer
ascii Sets the mode of file transfer to ASCII
binary Sets the mode of file transfer to binary
cd Changes directory on the remote machine
delete Deletes or removes a file in the current remote directory
help Requests a list of all available FTP commands
lcd Changes directory on your local machine
ls Lists the names of the files in the current remote directory
mkdir Makes a new directory within the current remote directory
pwd Finds out the path name of the current directory on the remote machine
rmdir Removes or deletes a directory in the current remote directory
prompt Prompts you to confirm the transfer of each file before completing the transfer. By default, prompting is set to on.

Note: Note: You can use ? to request for help or additional information about the ftp commands.

FTP Transfer Modes

FTP supports two types of transfer modes:

  1. American Standard Code for Information Interchange (ASCII) mode: transfers plain files such as text files.
  2. Binary mode: Binary mode enables you to transfer binary, image, or any nontext files.
Читайте также:  New window screen linux

Note: In most of the UNIX/Linux distributions the default mode of transfer is ASCII. Therefore, to transfer binary, image, or any nontext files you have to type the bin command to ensure complete data transfer.

Transferring Files Using ASCII Mode

The example Below we will:
1. establish an FTP connection from the host1 system to the host2 system.
2. After the connection is established, we will change the transfer mode to ASCII mode.
3. The we will get the file test1.txt on host2, store the test1.txt file in local directory on host1, and quit the FTP session.

$ ftp host2 Connected to host2. 220 host2 FP server ready. Name (host2:user): user 331 Password required for user. Password: password 230 User user logged in. Remote system type is UNIX. Using binary mode to transfer files.
ftp> ascii 200 Type set to A.
ftp> ls 200 PORT command successful. 150 Opening ASCII mode data connection for file list. test1.txt (directory list truncated) 226 Transfer complete. 133 bytes received in 0.081 seconds (1.61 Kbytes/s) 
ftp> get test1.txt 200 PORT command successful. 150 Opening ASCII mode data connection for test1.txt (57 bytes). 226 Transfer complete. local: test1.txt remote: test1.txt 66 bytes received in 0.042 seconds (1.54 Kbytes/s)
ftp> bye 221-You have transferred 66 bytes in 1 files. 221-Total traffic for this session was 1326 bytes in 4 transfers. 221-Thank you for using the FTP service on host2. 221 Goodbye.

Transferring Files Using Binary Mode

The example Below shows how to transfer a binary file.

$ ftp host2 Connected to host2. 220 host2 FTP server ready. Name (host2:user2): user2 331 Password required for user2. Password: 230 User user2 logged in. Remote system type is UNIX.
ftp> get binary.file 200 PORT command successful. 150 Opening BINARY mode data connection for binary.file (19084 bytes). 226 Transfer complete. local: binary.file remote: binary.file 19084 bytes received in 0.0044 seconds (4212064 Kbytes/s)

Transferring Multiple Files

The example shown below establishes an FTP connection from the host1 system to the host2 system and transfers multiple files by using the prompt, mget, and mput commands.

$ ftp host2 Connected to host2. 220 host2 FTP server ready. Name (host2:user2): user2 331 Password required for user2. Password: 230 User user2 logged in. Remote system type is UNIX. Using binary mode to transfer files.

By default the prompt mode is on, when you type the prompt command, it will disable the prompt mode (interactive mode) and you will not be asked for confirmations before you perform any action like get, put etc.

ftp> prompt Interactive mode off
ftp> mget file.1 file.2 200 PORT command successful. 150 Opening BINARY mode data 226 Transfer complete. 200 PORT command successful. 150 Opening BINARY mode data 226 Transfer complete.
ftp> mput file3 file4 200 PORT command successful. 150 Opening BINARY mode data 226 Transfer complete.

Источник

How to Transfer Files Between Servers in Linux using SCP and FTP

Zaira Hira

Zaira Hira

How to Transfer Files Between Servers in Linux using SCP and FTP

Transferring files between machines is a very common operational task that you'll do all the time as a developer.

Linux provides a number of utilities to transfer files. In this tutorial we will discuss FTP and SCP . Many automated scripts also deploy FTP or SCP to move files.

What is FTP?

FTP is a network protocol used for exchanging files over the network. It uses port 21. FTP enables you to access a remote system for exchanging files using the ftp command.

FTP Syntax

Here, host can either be the hostname or IP address of the remote host.

FTP Commands

FTP commands are similar to Linux commands. We'll discuss some of these.

Command Usage
open Opens a remote connection with another computer.
get Copies a file from the remote system to the local system.
put Copies a file from the local system to a directory on the remote system.
mget Transfers multiple files from the remote system to the local system’s current directory.
mput Transfers multiple files from the local system to a directory on the remote system.
bye/quit Prepares to exit the FTP environment.
close Terminates the FTP connection.
ascii Enables file transfer mode to ASCII
binary Enables file transfer mode to binary.

How to Transfer Files via FTP

FTP offers two transfer modes: ASCII and Binary.

  1. ASCII stands for American Standard Code for Information Interchange. It is used to transfer plain files such as text files.
  2. Binary mode: Binary mode is used to transfer non-text files such as images.

The default transfer mode is ASCII.

Step 1 – Connect to FTP

In the example below, hostA is the remote host. You will be prompted for a username and password.

$ ftp hostA Connected to hostA. 220 hostA FTP server ready. Name (hostA:user): user 331 Password required for user. Password: password 230 User user logged in. Remote system type is LINUX. 

Once the connection is successful, you'll notice the ftp> symbol in the beginning. Now we can run the FTP commands.

Step 2 – Choose file transfer mode

You can choose the mode (binary or ASCII) depending on your file type.

ftp> ascii 200 Type set to A.

Step 3 – Transfer files

We use the get command to transfer the file sample.txt from remote FTP server to local machine.

ftp> get sample.txt 200 PORT command successful. 150 Opening ASCII mode data connection for sample.txt (22 bytes). 226 Transfer complete. local: sample.txt remote: sample.txt 22 bytes received in 0.012 seconds (1.54 Kbytes/s)

Step 4. End the session

ftp> bye 221-You have transferred 22 bytes in 1 files. 221-Total traffic for this session was 126 bytes in 2 transfers. 221-Thank you for using the FTP service on hostA. 221 Goodbye.

How to Transfer Multiple Files via FTP

To transfer files in bulk, there are two commands: mget and mput .

You use mget to download the files, whereas you use mput to upload the files.

ftp> mget sample_file.1 sample_file.2
ftp> mput sample_file.1 sample_file.2

All the steps we just learned can be placed in an executable file and be scheduled. You can find the code for automation here.

What is SCP?

SCP stands for Secure Copy. It uses SSH and port 22. The data transferred through SCP is encrypted and sniffers can't access it. This makes SCP very secure.

  • Transfer files from local machine to remote host.
  • Transfer files from remote host to local machine.

SCP syntax

Let's explore the syntax of SCP.

scp [FLAG] [user@]SOURCE_HOST:]/path/to/file1 [user@]DESTINATION_HOST:]/path/to/file2 
  • [FLAG] specifies the options that can be given to SCP. Here are some details about flags:
  • [user@]SOURCE_HOST is the source machine.
  • [user@]DESTINATION_HOST:] is the destination machine.

Note: To transfer files via SCP, credentials must be known and the user should have the permissions to write.

How to Transfer Files from Local Machine to Remote Host via SCP

To transfer files to a remote host, use the command below:

scp source_file.txt remote_username@10.13.13.11:/path/to/remote/directory

In the command above, source_file.txt is the file to be copied. Remote_username is the username for remote host 10.13.13.11 . After : the destination path is specified.

remote_username@10.13.13.11's password: source_file.txt 100% 0 0.0KB/s 00:00 

The file source_file.txt will now be placed in /path/to/remote/directory .

To copy directories, use the -r flag as demonstrated below.

scp -r /local/directory remote_username@10.13.13.11:/path/to/remote/directory

How to Transfer Files from Remote Host to Local Machine via SCP

To transfer files from a remote host to a local machine, use the command below:

scp remote_username@10.13.13.11:/remote/source_file.txt /path/to/local/directory

Be extra careful when transferring files as SCP overwrites the already existing files.

Wrapping up

In this tutorial, you learned how to transfer files and directories using FTP and SCP via command line.

When automated, these commands serve even greater purposes in data warehousing, ETL (Extract, Transform, Load), reporting, archiving and bulk file processing. Do give these commands a try. Let's connect on Twitter.

Источник

Оцените статью
Adblock
detector