How To Use SCP Command for File Transfer in Linux?

by robin

SCP is the network protocol that helps copy the folders or the files between the Linux system on a network. SCP protects the data while copying across the ssh connection. It works by encrypting the files and the passwords. Thus, the information is still encrypted even when there is a traffic interruption.

With the use of SCP, you can copy a directory or a file from your local system to a remote system or between two remote systems to any local system. Both the passwords and files are encrypted when transferring the data with the SCP command. So anyone who is stooping in the traffic will not get anything.

While if you are unaware of the SCP command, then the guide here will provide you with precise insights.

SCP Command Syntax

scp [option] [user_name@source_host:path/to/source/file] [user_name@target_host:target/path]

Here,

  • Option- SCP options like the ssh configuration, cipher, ssh port, recursive copy, etc.
  • [user@] SRC_HOST :]file1- source file
  • [user@] DEST_HOST :]file2- destination file

The local file specification must happen with an absolute or relative path. While in the case of remote files, the name must be specific to a host or the user.

scp command

SCP command provides multiple options that help control every aspect of the behavior. The most used option here includes.

  • -P – It specifies the ssh port of the remote
  • -p – it is responsible for preserving the files modification and for providing access on time
  • -q – one can use the option if they wish to surprise the progress meter or to avoid any non-error messages
  • -c – the option will force the SCP command to compress the data as sent to the destination machine
  • -r – the option works well for informing the SCP to copy the directories

Here if you omit

  • The user_name of the host, then the command will default to the current user
  • The path/to/source, then the program will look for the file locally.

When working with remote files, it is always important to specify the host and the user specifications. So make sure to use an account that can easily access the files you wish to copy on the source system. Additionally, you must prefer an account with proper access to the directory where the files are on the destination system.

Remember, the SCP command will not check the destination location before writing. So if any file in the destination has the same name, it will save the information under the same pretext.

Important Information About SCP Command

The SCP command relies entirely on ssh for transferring the data. And this means it requires an ssh key or a password to authenticate the remote system.

SCP uses the colon as the medium to distinguish between the local and the remote locations to copy the files. You need to have at least read permissions on the source files and the write permission available on the target system. You need to be careful when copying files with the same name or location. It is because the SCP can overwrite files without any warning. Besides, when transferring large files, you must run the SCP command inside a tmud session or screen.

Also Read: How to Unstage File on Git Without losing changes?

Copy Directories and File Between Systems using SCP Command

Copy a File to a Remote System

You need to run the command if you wish to copy a file from a local area to the remote system.

scp file.txt remote_username@10.10.0.2:/remote/directory

  • Here, file.txt will be the name of the file that you wish to copy. 
  • Remote_username will be the remote server user
  • 10.10.0.2 will be the server IP address
  • /remote/directory will be the part of the directory that you wish to copy

The system will copy the file to the remote user’s home directory when you don’t specify the remote directory. You must provide the user password, and only then the transfer process will start.

When you avoid the file name from the destination, the file copy happens with the original name. Now if you wish to save the file under a different name, you need to specify the new file name.

scp file.txt remote_username@10.10.0.2:/remote/directory/newfilename.txt

If you see ssh on the remote host listing to a port other than the default 22, then you can specify the port using the -P argument.

scp -P 2322 file.txt remote_username@10.10.0.2:/remote/directory

The command helpful for copying a directory is much more similar to the one used to copy the files. The only difference is that you must use the -r Flag for recursive.

To copy a directory from a local to the remote system, you will have to use the -r function.

scp -r /local/directory remote_username@10.10.0.2:/remote/directory

Copy a Remote File to a Local System

To copy your file from the remote system, you must use the location as a source and the local location as the destination.

scp remote_username@10.10.0.2:/remote/file.txt /local/directory

Copy the File Between Two Remote System

When using the SCP command, you do not have to log in to one of the servers to transfer the file from one remote machine to another. You can simply use the following command from the remote host to the directory.

scp user1@host1.com:/files/file.txt user2@host2.com:/files

Now you need to enter the passwords for both remotes account. The data will now transfer directly from one remote host to another.

While rooting the traffic to the machine then the command assurance is there, and you can use the -3 option.

scp -3 user1@host1.com:/files/file.txt user2@host2.com:/files

Copy a File with SCP command and Limit Bandwidth

There is also another helpful option available for you which limits the bandwidth used by the SCP command. It works by adding a -1 parameter. It is beneficial when copying larger files to prevent SCP from draining the bandwidth. However, when limiting the bandwidth, it is essential to specify the number of Kilobits/s.

Remember your one byte= 8 bits. So if you wish to restrict the bandwidth of SCP to 100 KB/s, then you have to use the command below.

scp -l 800 Desktop/sample_example.txt root@147.182.143.27:/home/remote_dir

Also Read: How to Extract/ Unzip tar.gz file? – Unzip Command

Copy a File with SCP Faster

If you wish to speed up the file transfer process from one server to another, then using the -c option will help. It will work well for compressing the files that are to be transferred. Once the file reaches the location, it can return to its standard size.

scp -C Desktop/sample_example.txt root@147.182.143.27:/home/remote_dir

Frequently Asked Questions

Why use the SCP command in Linux?

The SCP command will help copy the file or a directory between the local and the remote system. It can also help copy the files between the two remote systems. You can make use of the command from a remote system easily. It makes use of ssh for the data transfer ensuring proper safety.

Is there any alternative command for SCP?

Rsync is an excellent alternative for the command. The use is exactly the same. However, rsync is all about performance. It will do complex calculations locally to send as little data as possible over the network.

Is it possible to pass the password to the SCP command?

The user can copy a directory or file from a local system to a remote one. You can do it either from a local system, a remote system, or between two remotes from the device. So, you can make use of the SCP command. But you must provide a remote user password before transferring the file.

Conclusion

The SCP command can be pretty helpful for completing the job of securing the transmission of the file. It is beneficial when you copy the protocols of all essential files. It will also follow the regular command line and the ssh functionality. Thus, it helps create a seamless command to manage the files between the Linux machines.

The guide specified the SCP command and detailed information about the same. Make sure that you follow the guide to get the right results.

You may also like

Leave a Comment