Struggling With the Linux Secure Copy Command? Use This scp Example
The scp command lets you copy a file efficiently between two different hosts. The syntax for scp reuses the syntax of cp, so it should feel familiar to most Linux users.
The tricky bit with the scp command is that either source or destination may be remote i.e. a computer connected to another network, different from your local one. When specifying a file for copying, you’ll need to include details about the remote host as well. This includes its IP address and username.
Uploading a File Using the scp Command
The general scp syntax is:
scp source destination
Notice how this is essentially the same syntax as the cp command.
The simplest scp example is the one you’ll probably use the most: uploading a file from your local machine to a remote server. In this case, the source part of the command is straightforward and the destination is more complicated:
scp index.html bobby@example.org:/var/www/html/
This will copy a local file named index.html to the remote host. You can specify this in the same way you’d specify a file for any other command. It can be an absolute or relative reference to the file. So, index.html, ../index.html, and /home/bobby/index.html are different ways you might use to specify a local file.
The destination in the example is bobby@example.org:/var/www/html/. This means that:
- The user bobby will be the owner of the new file on the remote server. That user will need to exist, and you should be able to access it.
- The hostname of the server we’ll upload the file to is example.org. We’ve specified the domain here, but you can also use an IP address instead.
- On that server, scp will upload the file to the /var/www/html/ directory.
Once you enter that command, your terminal will typically prompt you for the password of the user you specified on the remote machine.
Downloading an Entire Directory
Here’s a slightly more complicated variant that downloads an entire directory:
scp -rpC bobby@example.org:/tmp/docs /home/bobby
First, you should notice that the source is now a remote machine, whilst the destination is local. Apart from the order switch, these source and destination details should be familiar. But this example also introduces three useful flags:
- -r: Recursively downloads an entire directory. It will transfer all the files inside /tmp/docs. This is very useful for making quick backups or transferring large numbers of files, particularly if you don’t have login access to the remote host.
- -p: Preserves file timestamps and modes of original files. Your local copies will have the same creation times or write permissions as their originals.
- -C: Enables compression. If you’re transferring a lot of files, this can speed things up, especially if you’re using a slow connection.
The scp Command Is Almost as Easy as cp
Basic usage of the scp command is easy: it’s just the same as cp. The primary difference being that scp needs a few more details for the remote server. However, as with cp, there are useful flags to extend the base functionality. These include compression and recursive copy to provide for multiple-file downloads.
Mastering the scp command can come in handy if you've been asked to manage a Linux server. Doing backups and remote data transfer is a daily task for someone who's into server administration.
source https://www.makeuseof.com/scp-example/
Post a Comment for "Struggling With the Linux Secure Copy Command? Use This scp Example"
Comment when there are difficulties