Copy Files and Directories Recursively with SCP

The scp is a command and tool used to copy files and directories between two systems over the network. The scp command is generally used for Linux and network systems but also supports Windows operating systems. One of the most useful features of the scp command is copying files and directories recursively. In this tutorial, we will examine how to copy files and directories recursively by using the scp command.

Copy Recursively From Local To The Remote

The local files and directories can be copied recursively to the remote system. The -r option is used to enable recursive copy for the scp command. In order to copy local files and directories to the remote system, the following scp command syntax can be used.

scp -r LOCAL_PATH REMOTE_PATH
  • LOCAL_PATH is the local path or directory we want to copy to the remote.
  • REMOTE_PATH is the remove path or directory we want to copy the local files and directories.

In the following example we will copy the local directory named Downloads into the remote system recursively. The remote system IP address is 192.168.1.20 and remote path is /mnt/backup .

scp -r /home/ismail/Downloads 192.168.1.20:/mnt/backup

Alternatively, we can use an absolute path for the local directory. In the following example, we will copy the current working directory folder named Downloads to the remote recursively.

scp -r Downloads 192.168.1.20:/mnt/backup

In the following example, we will copy all contents of the parent directory by using a double dot .. to the remote recursively.

scp -r .. 192.168.1.20:/mnt/backup

Copy Recursively From Remote To The Local

The scp command can be used to copy remote files and directories to the local system recursively. The -r option is used copy files and directories recursively. The following syntax can be used copy recursively from remote to the local.

scp -r REMOTE_PATH LOCAL_PATH 
  • LOCAL_PATH is the local path or directory we want to copy to the remote.
  • REMOTE_PATH is the remove path or directory we want to copy the local files and directories.

In the following example we will copy the remote directory /mnt/backup which is located in the 192.168.1.20 system into the /home/ismail/Downloads recursively.

scp -r  192.168.1.20:/mnt/backup /home/ismail/Downloads

Alternatively we can specify the local path by only providing the directory name. The given directory is located in the current working directory.

scp -r  192.168.1.20:/mnt/backup Downloads

Copy Recursively By Preserving Modification, Acces Time and Mode Information

By default directories and files contain modification time, access time, mode information. This meta-information is not copied from the original files by default. We can copy these modification time, access time, and mode information by using the -p option while copying files and directories recursively. The -p option can be used for both from remote to local or local to remove recursive copy operation.

scp -r -p /home/ismail/Downloads 192.168.1.20:/mnt/backup

Leave a Comment