This tutorial explains how to recover files and directories from a remote host to our local device.
SCP or Secure Copy Protocol is an SSH-based protocol that allows file sharing between remote devices. With SCP, you can send a receive file to and from a local device or between remote devices.
Currently, SCP is considered obsolete and has been replaced by SFTP and RSYNC. At the end of the SCP instructions, I added tips for downloading files from remote hosts using SFTP with a syntax similar to SCP.
Copying or downloading a file from remote to local using
SCP SCP syntax is quite simple. Simply invoke SCP followed by the remote username, @, IP address or host, colon, and path to the file. If not specified, the default path is the home directory of the remote user. Next, define the local path where the file will be stored locally.
The scp command was designed to be similar to the cp command. The location of the file copy is specified at the end of the command.
A simple example of scp to copy a remote file to the local device would be:
In my case, I will copy the file named linuxhint from the remote device 192.168.1.100. The linuxhint file is stored in the home directory of the user kali, the user I will authenticate. Therefore, after the colon, I do not specify the path, which is the home directory by default, and simply type the file name (“linuxhint”). Then, I specify the current directory as the local location to store the file by typing a period:
As you can see, the linuxhint file
was copied to the current local directory
. In the
following example, I download the file named linuxhint2 from the remote device to the /home/linuxhint/locdir directory. The linuxhint2 file is stored in the dir directory named inside the remote user’s home directory (kali):
Copy or download
a directory recursively
using SCP
To download or copy directories from remote to local using SCP, you must implement the -r flag (recursively).
With the exception of the -r Added flag As shown in the following example, the syntax for downloading directories is the same as downloading files from remote devices to local:
As you can see, the directory named dir was downloaded with all its contents.
Upload a file or directory from local
to remote using SCP
The previous section of this tutorial explained how to retrieve or download files from a remote device using SCP. This section of the tutorial explains how to upload or send a file to a remote device.
The following example shows how to send or insert a file named linuxhintfile to the default directory on the remote device or to the user’s home. The difference with receiving files is that you must define the file to send or its path right after invoking SCP, as shown in the following screenshot.
Remember, the scp command was designed to maintain the syntax of the cp command. Therefore, the destination directory of the file is always specified at the end of the command, both when sending and retrieving files.
The definition of directories is the same as when receiving or downloading files. The following example emphasized how to send the file named file2 and store it in the linuxhint user home directory. The file will be saved in the dir subdirectory of the remote device, located in the default home directory.
To send directories to apply a similar syntax, simply add the -r flag to send files recursively, as shown in the image below. The linuxhintdir directory is sent to the /tmp directory of the remote device:
Get or download files and directories
using SFTP
Downloading files using SCP is an obsolete method mostly replaced by SFTP (Secure File Transfer Protocol). The current correct ways to transfer files are SFTP or RSYNC. SFTP can be used in interactive mode, but this section shows how to use it with a syntax similar to SCP.
In the following example, the sftp command is used to download the file named linuxhint from the remote host to the local /tmp directory.
Getting directories recursively using SFTP requires the -r flag, just like when using the scp command, as shown in the screenshot below. The linuxhintdir directory is downloaded to the local /tmp directory.
As you can see, the directory was downloaded recursively
.
The SFTP command is mainly used in its interactive mode, which is explained in depth in this tutorial. Also, consider using the rsync command, another updated and secure alternative to the obsolete scp command.
Conclusion Downloading
files from a remote host to a local device using SCP is probably the primary choice for most Linux users, including system administrators. As you can see, scp is a simple command, almost as easy to use as the cp command. We always assume it’s safe since its name says it’s safe. However, it is no longer secure and its developers marked it as obsolete.
The main alternative, the Secure File Transfer Protocol or SFTP protocol, is not as easy to use as the scp command in its interactive mode. That’s one of the reasons behind the popularity of the scp command even after it has become obsolete. However, users should consider adopting SFTP or RSYNC as the primary options.
Thanks for reading this tutorial, follow Linux Hint for additional Linux tips and tutorials.