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
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 syntax is as below:
host can either be the hostname or IP address of the remote host.
FTP commands are similar to Linux commands. We'll discuss some of these.
|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.
- ASCII stands for American Standard Code for Information Interchange. It is used to transfer plain files such as text files.
- 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 to download the files, whereas you use
mput to upload the files.
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.
You can use SCP to:
- Transfer files from local machine to remote host.
- Transfer files from remote host to local machine.
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:
|-r||To recursively copy directories.|
|-q||Used to hide the progress meter and any other info other than errors.|
|-C||Used to compress the data when sending it to its destination.|
|-P||Specifies the destination SSH port.|
|-p||Preserves file access times.|
[user@]SOURCE_HOSTis 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
To transfer files to a remote host, use the command below:
scp source_file.txt firstname.lastname@example.org:/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
: the destination path is specified.
email@example.com's password: source_file.txt 100% 0 0.0KB/s 00:00
source_file.txt will now be placed in
To copy directories, use the
-r flag as demonstrated below.
scp -r /local/directory firstname.lastname@example.org:/path/to/remote/directory
How to Transfer Files from Remote Host to Local Machine via
To transfer files from a remote host to a local machine, use the command below:
scp email@example.com:/remote/source_file.txt /path/to/local/directory
Be extra careful when transferring files as SCP overwrites the already existing files.
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.