Skip to content

How to use SCP command to transfer files in Linux?

SCP or Secure Copy is one of Linux’s many useful command-line utilities. This one lets you securely copy files and folders between two systems or locations. 

Using SCP, users can copy a file or directory from either a local system to a remote system or vice versa. You can even take it a step further and copy files between two remote systems using a local system. 

During the data transmission, the files and the passwords get encrypted to protect sensitive data against snooping. In this article, we’re going over the SCP command and everything else you need to know to transfer files in a breeze. 

Also read: How to add users to Sudoers in Debian?


SCP syntax

Just like every other command line utility, the SCP command also follows a general syntax that we need to keep in mind when using the tool.

scp -flag [email protected]:/filePath [email protected]:/finalPath

Here’s a proper breakdown of the syntax

  • -flag: This is a control option that you can specify while using the command.
  • [email protected]:/filePath: This is the source username and public IP address, followed by the file path to the file you want to copy. 
  • [email protected]:/finalPath: This is the destination username and public IP followed by the path you want to save the file.

As you can see, there isn’t exactly a lot to the command itself. Here are the control options that you can use with SCP.

  • -P: You can use this to specify the remote host ssh port if it’s anything other than the default port 22.
  • -p: This flag will preserve file modification and access times.
  • -q: Use this to suppress the progress meter and non-error messages.
  • -C: This flag will force SCP to compress the data while being sent to the destination machine. 
  • -r: Tells SCP to copy files recursively. 
  • -3: Routes the traffic through which the command is issued. This flag is only applicable when you’re using a machine to copy files between two remote systems.

Note that since SCP requires SSH for data transfer, it requires an SSH key or password to authenticate and access the remote systems it needs to work with. 

Another important thing to keep in mind is that you need to have at least read permissions on the source file and write permissions on the target machine to copy your files successfully. 

SCP differentiates between local and remote locations using the colon (:). We recommend running the utility inside a screen or tmux session if you’re copying large files. 

Also read: How to install Kali Linux on Virtualbox?


How to copy files with SCP?

Here’s how you can use the SCP command to copy files between systems.

Copy local files to a remote machine

To copy a local file over to a remote machine, you’re going to have to use the following command. 

scp file.extension [email protected]:/destination/directory/path
How to use SCP command to transfer files in Linux?

As explained above, the file.extension here is the full file name of the file you’re looking to copy. [email protected]is the username and public IP address of the remote machine followed by the path to where you want to save the file. 

You can mention a file name after the destination path to save the file under a different name. If you omit it, the file will be saved with its original name. 

The command for copying directories is the same as the one used for copying files. The only difference here is that you’ll have to use the -r flag to recursively copy everything for directories. 

scp -r local/directory/path [email protected]:/destination/directory/path
How to use SCP command to transfer files in Linux?

Also read: How to delete Files in Linux?


Copy files using SCP from remote to local machine

To achieve this your SCP command should look like this.

scp [email protected]:/file/path/file.txt /local/directory
How to use SCP command to transfer files in Linux?

Just as in the previous commands we issued, [email protected]:/file/path/file.txt is the remote machine’s username, followed by its public IP address and then the file path. After this, you need to enter the local machine’s destination path, and you’re good to go. 


Copy files and directories between two remote systems using SCP 

If you’re copying files between two remote systems, you don’t necessarily need to log in to any of the machines concerned. You can mention the SSH hostnames and passwords, and SCP will take care of the rest. 

scp [email protected]:/file/path/file.txt [email protected]:/destination/path

Note that you will be prompted for the SSH passwords for both machines. Data transfer will directly take place from one host to another, but you can route it through the local machine you’re using with the help of the -3 flag

Also read: How to remove a Directory in Linux?

Hello There!

If you like what you read, please support our publication by sharing it with your friends, family and colleagues. We're an ad-supported publication. So, if you're running an Adblocker, we humbly request you to whitelist us.

We may earn a commission if you buy something from a link on this page. Thanks for your support.







>