SSH (Secure Shell) is a protocol for operating network services or connecting to a remote server securely over an unsecured network. SSH comes with SCP (Secure Copy) which is a command-line utility tool. SCP allows you to securely copy files from SSH to the local machine or remote machine.
Using SCP we can do multiple tasks like:
- Copying files/directories from the local system to a remote system.
- Copying files/directories from a remote system to the local system.
- Copying files/directories from a remote system to a remote system using the local system.
In this tutorial, through practical examples, we will show you how to use SSH to connect to remote system & how to use SCP to copy files & directories from local to remote or vice versa.
SSH & SCP Command Syntax
Before we began with the tutorial let us first understand the basic syntax of the commands which we are going to use.
SSH Command Syntax
SCP Command Syntax
scp [options] [user]@[source_ip]:source_file_path [user]@[destination_ip]:destination_file_path
Copy File from Remote/SSH to Local System using SCP
To copy a file from the remote system to local system use source as remote location path and destination as local path as seen in below command:
$ scp remote_username@remote_ip:remote_directory/file1 /local_path/
- remote_username is the username of a remote system.
- remote_ip is the IP address of the remote system.
- remote_directory is the directory on the remote system where the file is located.
- file1 is the file on the remote system which is to be copied.
- local_path is the path on the local system where the file is to be copied.
Copy a Single Local File to Remote System using SCP
To copy a single local file to remote system use below command:
$ scp file1 remote_username@remote_ip:destination_directory
- file1 is the source file name we want to copy.
- destination_directory is the destination path where we want to copy the file. If this is not mentioned file will be copied to the user’s home directory.
Copy Multiple Local Files to Remote System using SCP
To copy a multiple local files to remote system use below command:
$ scp file1 file 2 remote_username@remote_ip:destination_directory
- file1 is the name of the first file.
- file2 is the name of the second file.
Copy Local File with Different Name on Remote System using SCP
If we don’t mention the file name in the destination path then its name will be the same as the source file name. So to copy a file with different name use below command:
$ scp file1 remote_username@remote_ip:/destination_directory/new_file1_name
- file1 is the source file name.
- new_file1_name is the new name of file1 on remote system.
Copy All Files from Local to Remote System using SCP
Sometimes we need to copy all the files in a particular directory to a remote system to do so use below command:
$ scp * remote_username@remote_ip:destination_directory
- * will select all the files from the current directory.
Copy a Local Directory to Remote System using SCP
To copy the whole local directory to remote system we just need to add -r flag to command as seen below:
$ scp -r /source_directory_path/ remote_username@remote_ip:/destination_directory_path/
- -r is the flag for recursive copying
- source_directory_path is the path of source directory to be copied.
- destination_directory_path is the destination path where the source directory is to be copied.