Skip to content

How to use SCP command to transfer files in Linux?

  • by
  • 5 min read

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 sourceUsername@sourceIP:/filePath destinationUsername@destinationIP:/finalPath

Here’s a proper breakdown of the syntax

  • -flag: This is a control option that you can specify while using the command.
  • sourceUsername@sourceIP:/filePath: This is the source username and public IP address, followed by the file path to the file you want to copy. 
  • destinationUsername@destinationIP:/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 remoteUsername@IPaddress:/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. remoteUsername@IPaddress: 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 remoteUsername@IPaddress:/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 remoteUsername@remoteIP:/file/path/file.txt /local/directory
How to use SCP command to transfer files in Linux?

Just as in the previous commands we issued, remoteUsername@remoteIP:/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 sourceName@sourceIP:/file/path/file.txt destinationName@destiantionIP:/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?

Yadullah Abidi

Yadullah Abidi

Yadullah is a Computer Science graduate who writes/edits/shoots/codes all things cybersecurity, gaming, and tech hardware. When he's not, he streams himself racing virtual cars. He's been writing and reporting on tech and cybersecurity with websites like Candid.Technology and MakeUseOf since 2018. You can contact him here: yadullahabidi@pm.me.

>