Quite often you will need to transfer files from one location to another, many times large files. File transfer is a term that is generally used to refer to the process of transmitting files from one machine to another machine over a computer network such as LAN, WAN or the even the internet.
There are several different ways to transfer files. Most are synchronous and use different protocols such as FTP or HTTP. The exact method to use will depend on your source machine OS, target system and the type of network. Sharing files is slightly different than transferring files, even though both involves the sub-processes of uploading and downloading.
The following options uses protocols primarily over TCP/IP. It goes without saying that you will need a TCP/IP network connection between the two machines: the source and the target machine.
Secure Copy or scp is the command that can be used to transfer files between two Linux or Unix based systems. This is based on the ssh protocol, and can be used to copy files to a remote system or between two remote systems.
Typically, the syntax of the scp command is similar to the syntax of the cp command. You can use it just as you are copying the files locally, except that you will need to specify the remote systems by host name or IP address.
bash$ scp sourcefile [email protected]:/path/to/targetfile
You can also use this command to transfer files from a remote system to the local system or two remote systems.
bash$ scp [email protected]:/path/to/sourcefile /path/to/localfile
Remote Copy or rcp is another Linux utility that can be used to transfer files between systems. It is based on the rlogin utility and uses it for data transfer as well as authentication. It has a similar syntax to the scp command described in the previous section.
bash$ rcp sourcefile [email protected]:/path/to/targetfile
As it is based on the rlogin utility, it is less secure than the scp utility. It is sends the data un-encrypted over the network, which makes it less secure for most network transfers. You should use scp over rcp if and when possible.
rsync is a Linux utility and protocol that is primarily used to keep two file locations in sync. You can use this for one time file transfer as well, just as the previously mentioned commands. rsync has several options which allows you to optimize the transfer such as encryption and compression.
A generic rsync command syntax to copy a file to a remote system is …
bash$ rsync -avz /path/to/sourcefile [email protected]:/path/to/targetfile
The above command will compress and copy the file to specified location on the target system. You can also see the progress of the copy process by using the -v or -P option in the command line.
If you have an FTP server running on the target system, then you can use an ftp client to transfer files. You can use the command line ftp interface to transfer the files. You can also use a graphical interface such as gFTP or Filezilla to transfer files over FTP.
Command line ftp client is mainly an interactive utility. You will need to use the ftp specific commands such as get and put to transfer files. Although it is possible to write a script to perform the task non-interactively, it is hardly an one liner. You can use other ftp clients such as lftp or ncftp which allows you to perform single commands to transfer files.
Secure File Transfer Protocol or SFTP is a network protocol developed to provide reliable file access and file transfer functionality over data streams. SFTP provides many more functions over the files than just file transfer, which is probably beyond the scope of this post.
Again, as with the ftp, sftp can be used in an interactive mode. But you can also use it from the command line with a similar syntax as scp.
bash$ sftp -v /path/to/localfile [email protected]:/path/to/targetfile
If you are transferring a single file or a directory, then using the scp command is the best option. If you plan to transfer files multiple times over a period of time, then you might want to consider using rsync, which is faster and supports compression.