Linux Commands

How Do I Use SCP to Transfer a Directory in Linux?

This tutorial explains how to easily transfer directories using the Linux scp (Secure Copy Protocol) command.

Despite the Linux scp command being deprecated and replaced by SFTP and RSYNC, its use is widely adopted. Even after being deprecated and replaced, probably SCP is the most common method to transfer files. Yet, its use isn’t professionally recommended. That’s why after the scp instructions to download and upload directories, I added instructions to do the same using the sftp command.

Download and Upload Directories Using scp

Uploading or fetching directories with scp is similar to uploading or downloading regular files. The only difference is the -r flag you need to add for directories to be transferred recursively.

The first example of this tutorial has the following characteristics you need to replace:

  • The username used to log in on the remote device is kali.
  • The remote IP address is 168.1.100.
  • The directory to download is named linuxhintdir.

You will need to replace the username, IP address, and directory names according to your scenario.

The example below invokes the scp command with the -r (Recursive) flag to specify we want to download a directory and not a regular file. The scp command and the -r flag are followed by the username@IP/Host. Then, it is followed by a colon and the path to the directory you want to download. Whatever you want to upload or download a directory, the location where you want to save the directory or regular file is always specified at the end of the command. In this case, the linuxhintdir directory will be stored in the /root directory.

The syntax is:

scp -r <Username>@<IP/Host>:<RemotePathToDir> <LocalPathToStore>

In my case:

scp -r [email protected]:linuxhintdir/ /root/

Note: Files and directories used in this tutorial are empty.

As you can see, the file was transferred correctly.

Uploading a directory using SCP requires a similar syntax. The difference, as I said previously, is you need to type the path where to save the directory at the end of the command.

This is because the scp command was designed to be similar as possible to the cp Linux command.

In the following example, a directory named localdir located in the current directory is copied to the remote host’s default location, the kali user home directory. By default, if you don’t specify a patch after the colon, the default path for files to be copied is the home directory of the user you authenticated.

After invoking SCP, add the -r flag to specify you are transferring a directory and not a regular file. Then, specify the directory you want to send followed by <Username@<IP/Host>:<Path> as shown in the example below:

scp -r localdir [email protected]:

As you can see in the following screenshot taken from the remote host, the directory was copied recursively:

Downloading and Uploading Regular Files Using scp

As said previously, downloading and uploading regular files is almost the same as with directories except for the absence of the -r flag, which isn’t necessary.

To upload a file, use the following syntax:

scp <FileName> <Username>@<IP/Host>:<RemotePathToDir>

Therefore, to upload a file named linuxhintfile to the remote default directory, known as the kali user home directory, I run the following command:

scp linuxhintfile [email protected]:

To download a file, the syntax is the following:

scp <Username>@<IP/Host>:<RemotePathToFile> <LocalPathToStore>

The following example shows how to download a file named linuxhintfile, to store in the home directory of the remote user named kali, and save it in the local user’s Downloads directory.

scp [email protected]:linuxhintfile ~/Downloads

As shown, the file was transferred correctly.

Download and Upload Files and Directories Using sftp

Downloading and uploading files and directories using SFTP can be simple as with SCP.

The following method shows how to download a file located in the remote subdirectory named dir. The file will be locally saved in the /tmp/linuxhint2 directory.

sftp [email protected]:dir/file /tmp/linuxhint2

Downloading directories is the same process. The example below shows how to download the dir directory to the current location specified with a dot:

sftp -r [email protected]:dir/ .

Uploading directories isn’t so simple as with SCP and the syntax changes. This is because you need to connect to the SFTP server to and run the put command to upload the file.

In the example below, the file named linuxhintfile is uploaded to the dir subdirectory on the remote host:

sftp [email protected]:dir <<< $'put linuxhintfile'

Uploading directories also requires implementing the -r flag for recursive transference. However, when uploading, the -r flag must be placed after the put command, as shown in the example below in which the directory linuxhintdir is uploaded to the remote subdirectory named dir.

sftp [email protected]:dir <<< $'put -r linuxhintdir'

As you can see, the directory was uploaded successfully. That’s how you can download and upload files and directories in Linux using scp commands or the more recommended sftp commands.

Conclusion

As you can see, copying directories in Linux remotely using the scp command is pretty simple. The scp command is considered the most user-friendly method to transfer files between devices, and it was once considered secure as its name, Secure Copy Protocol, indicates. Currently, the scp command is obsolete due to vulnerabilities and must not replace safer alternatives like SFTP and RSYNC. The sftp command can also be used in interactive mode, which wasn’t explained in this tutorial, but you can read it at https://linuxhint.com/sftp_linux_command_line/. All systems supporting SCP should support SFTP, so availability should not be a problem. In future tutorials, the RSYNC alternative will be explained, so keep following this blog for additional Linux tips and tutorials.

Thank you for reading this SCP tutorial, I hope it was useful to you.

About the author

David Adams

David Adams is a System Admin and writer that is focused on open source technologies, security software, and computer systems.