How Can I Use SCP Command to Transfer Files

In Linux, the scp command is a secure copy utility used to copy files between computers over a network. It uses the Secure Shell (SSH) protocol to transfer files securely, and it is a faster and more secure alternative to the cp command, which does not encrypt the transferred data.

This article will guide you on the “scp” command’s basics, syntax, and practical implementation. The supported content of this guideline is mentioned below:

How Does scp Command Work in Linux?

The “scp” command provides a secure way between two remote devices. The basic syntax of the SCP command:

$ scp [options] source destination

In the above syntax, the “source” argument is the directory or file for copying data, and the “destination” represents the location where the user wants to paste data.

Here are some common “options” for the SCP command:

  • r: Copy a directory and its contents recursively.
  • p: Preserve the source files’ modification times, access times, and modes.
  • q: Represents the quiet mode which does not display progress or error messages.
  • P: Refers to the port number for the connection.

Example 1: Copy a Specific File From Local to a Remote Machine

In this example, copy a file from the local to a remote machine by specifying the file name along with the path directory. For this, the “file.txt” is selected in the local machine and allocates the destination as “rem_folder” in the remote machine having username “roger” and hostname “ubuntu”:

$ scp file.txt roger@ubuntu:rem_folder

The output shows that “file.txt” has been copied to the remote machine after acquiring the authentication of the remote device.

Example 2: Copy a Specific File From a Remote to the Local Machine

Using the scp script, the user can transfer a file from the remote to the local device by giving the path of the destination. To do so, the “test.txt” file is selected from the remote machine. It specifies the destination directory as “office” in the local machine having username and hostname “itslinuxfoss” and “ubuntu” respectively:

$ scp itslinuxfoss@ubuntu:~/test.txt office

The output returns that “test.txt” has been copied to the local machine.

Example 3: Copy a Directory and its Content Recursively

In this example, copy the directory along with its contents from the local to a remote machine. For this, specify the directory “office” in the local machine and mention the destination “rem_folder” in the remote machine where you want to copy the directory mentioned above:

$ scp -r office roger@ubuntu:~/rem_folder

The output displays that the “emp.txt” file in the “office” directory has been copied to the “rem_folder”.

Example 4: Copy a File and Preserve Permission

If you want to copy a file and preserve its modification times, access times, and modes, execute the below script by specifying the file with the location of the source and destination. To do so, the “file.txt” is carried out in the local device and specifies the destination directory “new_folder” containing username and hostname of “roger” and “ubuntu” respectively:

$ scp -p file.txt roger@ubuntu:~/new_folder

The output shows that “file.txt” has been copied with preserving modification and access time.

Example 5: Transfer Files Over the Specific Port

Users can copy or transfer files over a specific port number. For this, the “-P” option is utilized by mentioning the port number along file name and destination as below:

$ scp -P 88 file.txt roger@ubuntu:~/new_folder

The output returns that the “file.txt” has been transferred over port 88.

Example 6: List the Information While Transferring Files

To display detailed information of transferred file between remote devices, the “v” (verbose) option is utilized below:

$ scp -v 88 file.txt roger@ubuntu:~/new_folder

The output shows that detailed information about the “file.txt” has been displayed in the terminal.

Conclusion

Linux offers the “scp” command to copy/transfer files between remote systems. Using this command, you can copy a file from the local to a remote device and vice versa. Additionally, the SCP command provides a secure way to copy a directory and its content recursively. This article has explained the basics of the scp command and all its working through examples.