How to Copy Files with SCP from Local to Remote
Secure Copy Protocol (SCP) leverages the SSH protocol to transfer files between the local and remote systems securely. It is more suitable for operating systems like Linux because of the added encryption of data.
In this guide, we will discuss in detail how to transfer files using SCP from local to remote servers, along with several practical examples. Let's dive in!
An Introduction to SCP
SCP is one of the most reliable ways to transfer files, folders, or directories over the network using the appropriate command line. With SCP, you can copy files:
- From a local system to a remote server.
- From a remote system to your local system.
- Between two remote systems using the local system.
SCP operates over the SSH protocol, which guarantees higher authenticity and security levels to prevent anyone from intercepting your files. In addition, SCP enables fast transfer through command-line use by default. But the biggest advantage of using SCP is that it can automate the transfer of batch files and folders (recursive copy).
How Does SCP Transfer Work?
SCP relies on the default SSH port 22 or 2222 to securely copy files from local to remote servers. But if you haven't already set up the SSH key pair, it requires the remote machine's password to initiate the transfer.
To proceed with SCP copy files, you need to provide the relevant details, including:
- The files or directory you want to copy.
- Local server name or IP address.
- Remote server's name and directory destination to copy the files to.
During the process, it displays a progress bar showing the transfer speed as shown in the picture below:
The full basic SCP syntax to copy files from a local host to a remote server is:
Scp [Options] /path/to/local/file.txt username@remote_host:/path/to/remote/directory
Here is the breakdown of this command:
1. scp: to establish a connection
2. [Options]: provides different permissions depending on what files you want to copy. For instance:
r: to copy the entire directory.
q: known as a quiet mode as it copies the files quietly.
C: compress files during the transfer procedure.
-P [Upper Caps]: Use the specified port for SSH other than 22.
-p [Lower Caps]: Preserve the file modification and access time.
3. /path/to/local: The destination of the file on your local system that you want to copy using SCP.
4. /file.txt: the name of the file.
5. username@remote_host: The name and IP address of the remote system.
6. :/path/to/remote/directory: The destination directory where you want to copy the file. If it's not specified, the file is copied to the remote system's home directory.
Depending on whether you want to copy a single file, multiple files, or an entire directory or directory, tweak the command line accordingly.
How to Copy Files from Local to Remote Via SCP
Now that you understand the basic SCP command, it's fairly straightforward to transfer the files or entire directories to a remote server. Let us discuss each scenario in detail:
Case 1: Transfer a Single File to a Remote Server Using SCP
Transferring a single file to a remote system from your local machine using SCP is very easy. Look at the below example:
scp ~/Documents/backup-folder/report.pdf user@127.0.0.1: ~/Downloads
Here, the "/path/to/local/" is the backup folder in the Documents directory on your local system while "report.pdf" is the name of the file to be copied. This is transferred to the Downloads directory on the remote machine with the name and IP address: "user" and 127.0.0.1.
In short, this command copies the "report.pdf" file from the local to remote using SCP.
How to Rename the Transmitted File
SCP allows you to change the name of the transferred file with a slight modification in the command. To do that, mention the new name of the file at the end of the command.
Below is the example that renames the "report.pdf" to "assignment.pdf":
scp ~/Documents/backup-folder/report.pdf user@127.0.0.1: ~/Downloads/ assignment.pdf
Case 2: Copy Multiple Files to a Remote Server Using SCP
To transfer more than file files using SCP to the local machine, you need to enter the names and oaths of the desired files in the local directory. The below example clarifies it:
scp ~/Documents/file2.txt ~/Documents/file3.txt user@127.0.0.1: ~/Downloads
This command shows copies of the 2 files: file2.txt and file3.txt, from the local system to the Documents directory on the remote system. It also shows that you can move multiple files from different paths on the local to the remote server using SCP.
Case 3: Copy the Entire Directory from Local to Remote Using SCP
With SCP, you can copy a directory and all its files directly. To do that, invoke the scp command with the -r flag which transfers all the files from in the directory to the remote system.
Below is the example of copying the folder from local to remote recursively using SCP:
scp -r documents/pictures user@127.0.0.1: ~/Downloads
The above command moves the Pictures folder, including all its subfolders and content, to the remote Downloads directory.
Other Important Tips to Copy from Local to Remote Using SCP
Tip 1: Preserve File Metadata
Using the -p flag in SCP will retain the metadata such as ownership and creation data of the file. Below is the example to copy file.txt to the remote directory and preserving metadata:
scp -p ~/Documents/file.txt user@127.0.0.1: ~/Downloads
Tip 2: Using Port Another than Default 22
If the SSH on the remote device is listening on some other port instead of default 22, you can override it using the -P flag. Here is the example to execute SCP using port 2322:
scp -P ~/Documents/file2.txt user@127.0.0.1: ~/Downloads
Tip 3: Use Cases for SCP
The SCP command is best suited for quick and efficient file transfers in Linux systems. However, it doesn't provide management tools like listing remote directories, deleting files, or pausing and resuming the transfer.
So, use it when high speed and added privacy are the key objectives. For more advanced functionalities, it's better to try FPT.
Editors' Choice: Copy Files From Local to Remote in Windows/Mac
Do you want to access remote systems and transfer files from local to remote efficiently? Try using AirDroid Remote Support as a one-stop solution to send files to the remote devices – Mac, Windows, iOS, and Android.
It is among the very few programs that ensure seamless integration with both attended and unattended devices to assist in file transfer, and screen sharing, and voice messaging.
FAQs
Q1: Is SCP better than FTP?
When comparing SCP and FTP, the former is faster and more efficient in transferring files. But it doesn't offer advanced functionality like directory management and transfer resumption as does FTP.
Q2: Does SCP work for directories?
Yes, the SCP command can copy the entire directory between a local and a remote server as well as between two remote servers.
Final Remarks
SCP ensures the safe and secure transfer of files from local to a remote server. Its syntax can be executed within seconds, allowing you to copy a single or multiple files to a remote server from your Linux system.
In addition, you can rely on AirDroid Remote Support to access and transfer files to a remote Windows or Mac PC with ease. Its advanced collaboration and remote management tools make it a must-have tool on your system.
Feel free to ask any questions.
Leave a Reply.