Skip to content
Related Articles
Get the best out of our app
GFG App
Open App
geeksforgeeks
Browser
Continue

Related Articles

scp command in Linux with Examples

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

scp (secure copy) command in Linux system is used to copy file(s) between servers in a secure way. The SCP command or secure copy allows the secure transferring of files between the local host and the remote host or between two remote hosts. It uses the same authentication and security as it is used in the Secure Shell (SSH) protocol. SCP is known for its simplicity, security, and pre-installed availability.

 Syntax:

scp [-i identity_file] [-l limit] [-o ssh_option] [-P port] [-S program] [[user@]host1:]file1 … [[user@]host2:]file2

Example:

1) If we want to copy a file from a local machine to a remote machine:

syntax:

scp [file_name]  remoteuser@remotehost:/remote/directory

Here

  • file_name = The name of the file that needs to be copied.
  • remoteuser =The username of the remote host.
  • remotehost = The IP address or hostname of the remote host.
  • /remote/directory = The directory where the file should be copied on the remote machine.

For example: If we want to copy a file name “test.txt” from local system to a 

  • “remoteuser” = “Jayesh” 
  • “remotehost” = “10.143.90.2”
  • “/remote/directory” = “/home/jayesh”

syntax:

scp test.txt jayesh@10.143.90.2:/home/jayesh
copied file from local system to remote system

copied file from local system to remote system

To Verify: Use `ls` command in the location we copied file.

File that we have copied

File that we have copied

2) If we want to copy a file from remote machine to our local machine.

syntax:

scp user@remotehost:/home/user/file_name .

here

  • “user” = username of remote system.
  • “remotehost” = IP address of remote system.
  • “/home/user/file_name” = path of file that has to be copied.
  • “.” = this means that we are copying that file in current location in local system.

For Example: If we have

  • “user” = jayesh
  • “remotehost” = 10.143.90.2
  • “home/user/file_name” = home/jayesh/test1.txt

syntax:

scp jayesh@10.143.90.2:/home/jayesh/test1.txt .
copied file from remote system to local system using scp

copied file from remote system to local system using scp

To verify: use dir (in windows cmd)

test1.txt successfully copied

test1.txt successfully copied

Options in “scp” command in Linux

options  Description
-P port: Specifies the port to connect on the remote host.
-p  Preserves modification times, access times, and modes from the original file.
-q  Disables the progress meter.
-r  Recursively copy entire directories.
-s  Name of program to use for the encrypted connection. The program must understand ssh(1) options.

Examples:

1) `-P`: It is used to specify the port to connect on the remote host. It is useful when our SSH server is listening on a non-standard port.

Syntax: 

scp -P port source_file user@hostname:destination_file

For Example: If we want to copy a file “test2.txt” from local machine to a remote machine with IP address “10.143.90.2” on port 2222 , user = “jayesh” and location = “/home/jayesh/”

syntax:

scp -P 2222 test2.txt jayesh@10.143.90.2:/home/jayesh/
copying file from local system to remote using -P option in scp

copying file from local system to remote using -P option in scp

To Verify: Use `ls` command in remote system in the location we have copied the file.

test2.txt successfully copied

test2.txt successfully copied

2) `-p`: This option is used when we want the original metadata of the file that has been transferred. Basically, it preserves modification time, access time, and modes from the original file.

Syntax: 

scp -p source_file user@hostname:destination_file

For Example: If we want to copy a file “test3.txt” from local machine to a remote machine with IP address “10.143.90.2”, user = “jayesh” and location = “/home/jayesh/”

Syntax:

scp -p test3.txt jayesh@10.143.90.2:/home/jayesh/
copying file from local system to remote using -p option in scp

copying file from local system to remote using -p option in scp

3) `-q`: It disables the progress meter. This option hides the progress of the file transfer on the terminal.

Syntax: 

scp -q source_file user@hostname:destination_file

For Example: If we want to copy a file “test4.txt” from local machine to a remote machine with IP address “10.143.90.2”, user = “jayesh” and location = “/home/jayesh/”

Syntax: 

scp -q test4.txt jayesh@10.143.90.2:/home/jayesh/
As we can see there is no progress shown

As we can see there is no progress shown 

To Verify: Use `ls` command in remote system in the location we have copied the file.

test4.txt successfully copied

test4.txt successfully copied

4) `-r`: This option is used when we want to copy an entire directory and its contents. Which basically means copying entire directory recursively.

Syntax: 

scp -r Directory_name user@hostname:destination_file

For Example: If we want to copy a Directory content name “new” from local machine to a remote machine with IP address “10.143.90.2”, user = “jayesh” and location = “/home/jayesh/new1/”

Syntax: 

scp -r new jayesh@10.143.90.2:/home/jayesh/new1/
copying entire directory and its file recursively using `-r` in scp

copying entire directory and its file recursively using `-r` in scp

To Verify: Use `ls` command in remote system in the location we have copied the file.

new directory successfully copied.

new directory successfully copied.

Frequently Asked Questions on `scp` command in Linux.

1) What is the `scp -r` command in Linux?

This option is used when we want to copy an entire directory and its contents. Which basically means copying the entire directory recursively to the location we want.
For example, refer to the above context.

2)What is `scp` command in Linux.?

It is a command used to copy files or directories between hosts over a network. It does so by using the SSH protocol to encrypt and transfer files.

To see an example, refer to the above context.

3) What is the reverse `scp` command in Linux?

There is no such reverse thing in `scp` command in Linux, it is just copying file from remote location to our local system.

To see an example, refer to the above context.

Conclusion 

SCP (Secure Copy) command in Linux is a simple and secure way of copying files from local system to remote system and from remote system to local system. If we talk about authentication, it uses the same authentication as SSH and also comes pre-installed in our system. SCP command is widely used in our Linux environment, and it also has many options like -P, -p, -q and -r. With the help of syntax and examples discussed above, one can easily use SCP command in Linux.


My Personal Notes arrow_drop_up
Last Updated : 26 Apr, 2023
Like Article
Save Article
Similar Reads