There are various ways to copy a file or directory from remote to local or local to the remote machine. One of them is by using the SCP command.
SCP stands for Secure copy which is a remote file copy program available in Linux or Unix systems. It copies files between hosts on a network.
This is based on SSH, and uses the same authentication and security as SSH.
[su_note note_color=”#D7E3F4″ text_color=”#728095″ radius=”3″ class=”” id=””] NOTE:SCP asks for a password if it needed for authentication. While copying a file, if the file already exists on the remote system then it will replace the content of that file.
[/su_note]
Syntax of SCP Command
The syntax of scp is similar to cp command which is used locally for copying a file –
The basic SCP syntax is as below (without the square bracket):
scp [option] [user@local_host:/path/to/local/file_or_directory] [user@remote_host:/path/to/remote/dir_or_filename]
If the username is same on both remote and local you can drop the username from the command:
scp [option] [local_host:/path/to/local/file_or_directory] [remote_host:/path/to/remote/dir_or_filename]
Some Examples of using SCP
Copying a file to a host –
scp [option] [user@local_host:/path/to/local/file] [user@remote_host:/path/to/remote/dir]
Copying a directory from the remote host: –
scp -r user@host:/remote/source_dir local_target_dir
If the remote host uses a port other than 22 then it can be specified in the command –
scp -p port_no [user@local_host:/path/to/local/file] local_target_dir
Options used with scp command
Options | Descriptions |
---|---|
-P | Specifies the port to connect on the remote host |
-p | Preserves the modification time, access time and modes from the original file |
-r | Recursively copy entire directories |
-v | Enables verbose mode, This is helpful in debugging connections, authentication and configuration problem |
-T | Disable the strict filename checking |
-4 | Forces scp to use IPv4 addresses |
-6 | Forces scp to use IPv6 addresses only |
-B | Select batch mode (It prevents asking password or passphrases) |
-C | Use this option to enable compression |
-c | Select the cipher to use for encrypting the data transfer |
-F | Specifies an alternative per-user configuration file for SSH |
-i | Select the file from which the identity (private key) for public-key authentication is read |
-l | Limits the used bandwidth specified in Kbit/s |
-o | Use to pass options to SSH in the format used in SSH_config |
Using SCP to Copy Files
While sopying and file or directory you just need to understand which is the source and which is the destination machine.
Accordingly place the host and path information in the SCP command.
SCP From Local to Remote
Below are several examples that shows how to copy files or directories from local to remote Linux using SCP.
Now let’s look at the direction and understand the source and destination.
Local -> Remote
Source is Local machine and Destination is Remote machine
Copy a single file
To copy a file with the name let’s say file.txt to remote system’s /home directory, use the following command in your terminal.
scp /home/file.txt root@example.com:/home
Copying multiple files
Instead of copying the whole directory containing multiple files. You can copy more than one file by using the following command.
scp /home/file1.txt file2.txt file3.txt root@example.com:/home
Copying a folder
Now if you want to copy a directory name dir1 from local to the remote system then use the following command in your terminal.
scp -r /home/dir1 root@example.com:/home/[su_note note_color=”#D7E3F4″ text_color=”#728095″ radius=”3″ class=”” id=””]Important is the -r option.[/su_note]
SCP From Remote to Local
Below are several examples that shows how to copy files or directories from remote to local Linux machine using SCP.
Now let’s look at the direction and understand the source and destination.
Remote -> Local
Source is Remote machine and Destination is Local machine
Copying a single file
Now you want to copy a file name file.txt from home directory of the remote system to local system’s /home directory.
Use the following command in your terminal.
scp root@example.com:/home/file.txt /home/
Copying multiple files
Similarly, if you want to copy more than one file without copying the whole directory from remote to the local machine. Then you should use the following command in your terminal.
scp root@example.com:/home/file1.txt file2.txt file3.txt /home/
Copying a folder
Now if you want to copy a directory name dir1 from the remote system to local, then use the following command in your terminal:
scp -r root@example.com:/home/ /home/dir1[su_note note_color=”#D7E3F4″ text_color=”#728095″ radius=”3″ class=”” id=””]Important is the -r option.[/su_note]
Copying file to a specific port
If you want to use a specific SSH port (if not 22) then use option -P (in uppercase) in your command. Use the following command in your terminal.
scp -P 21 root@example.com:/home/file.txt /home/[su_note note_color=”#D7E3F4″ text_color=”#728095″ radius=”3″ class=”” id=””]Important is the -P option.[/su_note]
Conclusion
At times it may feel like the SCP command is a little hard to pick up.
The more you use it, the more you get familiar with this command.
To know more about scp and the options see the manual page of scp. To display it use the following command in your terminal:
man scp
You can always leave a comment if you need any help from me.