Linux provides the scp command in order to copy files and folders in a secure way. The scp command can download or upload directories to or from a remote system. The scp command is integrated with the SSH command and subsystem. So in order to use scp command to copy a directory, SSH should be installed to copy the directory.
Download Directory (Recursively)
The most popular operation with scp command is downloading a directory from the remote system to the local system. By default, scp downloads a single file or folder which is specified. In order to download a complete directory, the
-r option should be specified which will download the remote directory recursively. Recursive means downloading everything inside the directory and its contents. In the following example, we will download the remote directory
/home/ahmet/backup to the current working directory by using the current username.
$ scp -r 192.168.122.10:/home/ismail/backup
We can also specify the username explicitly which can be different then the current user.
$ scp -r firstname.lastname@example.org:/home/ismail/backup
By default, the remote directory downloaded into the current working directory. But we can also specify the local path or directory we want to download remote files. In the following example, we will download the remote directory into the local path /mnt/drive .
$ scp -r email@example.com:/home/ismail/backup /mnt/drive
We can also download the remote directory or folder to the current working directory. The sign
. is used to specify the current working directory. In the following example, we set the local download directory as the current working directory.
$ scp -r firstname.lastname@example.org:/home/ismail/backup .
Alternatively, we can download the specified remote directory or folder to the current user home directory. The sign
~ is used to specify the current user home directory.
$ scp -r email@example.com:/home/ismail/backup ~
Upload Directory (Recursively)
The scp command can be also used to upload the local directory to the remote system. Like download operation, in order to upload a specified directory and all its content to the remote system, the recursive option should be provided which is the option
-r. First, the local directory we want to upload will be specified which can be a relative path or absolute path. Then the remote server hostname or IP address and the remote path will be provided.
$ scp -r /var/backup 192.168.122.10:/home/ismail/backup
By default the current username is used for the scp command but we can specify the remote system username like below which is ismail in this case.
$ scp -r /var/backup firstname.lastname@example.org:/home/ismail/backup
Use Different scp Port
If the remote system SSH service is running differently than the default TCP 22 port the
-P option can be used to specify a non-default SSH port like below. In the following example, the scp command uses TCP 2222 as a remote system port number. This option can be used for both download and upload.
$ scp -P 2222 -r email@example.com:/home/ismail/backup /mnt/drive
Passwordless Directory Download or Update
The scp command uses the SSH infrastructure and password authentication done via SSH. By default, the scp command works with SSH username and password but SSH can work also as passwordless. The scp can use this passwordless login or authentication in order to copy directories to or from the remote. The
ssh-copy-id command can be used to enable passwordless usage for the username
ismail for the remote system with IP address 192.168.122.10 like below.
$ ssh-copy-id firstname.lastname@example.org