I've already shown you one way to upload and download files to and from the server, but it's a bit cumbersome. Here's an example where we're using I/O redirection to feed hello.txt-- the file-- into the ssh command as standard input. And then once we're on the remote machine, we can run the cat command, which will take standard input and write it out to this file-- somefile.txt.
This is quite a bit to write just to upload a file, and there's a much simpler way. We can use a command called scp, which stands for secure copy, and it actually uses ssh under the hood. It lets us, very quickly, upload and download files over the SSH protocol.
You can learn about all the various options of scp by looking at its man page, but I'm going to show you a couple of quick examples here.
Let's say I want to upload the file hello.txt to the remote server. I can use the scp command, and then, as a first argument, provide the path to the filename. So hello.txt, and then, as a second parameter, the machine that I want to secure copy this up to.
So for example, in this case, I can use the vm host that I created in my configuration file earlier, so I don't even need to use email@example.com here. I'm just going to say vm. And then I can use a colon, and then the path where I want to put the file, and as long as this user has write permissions, I can take this file and actually write it anywhere I want.
So for example, let's put it into the home directory, and I could put it into the project folder that we created earlier, into a file called upload.txt. We'll just name it something different. OK, so it takes that file, it gives you a nice progress bar to show how far along it is, and then it uploads it.
And let's take a look and make sure that it actually works, so I'll ssh into the machine, so ssh vm, and we'll change into the project folder and take a look at its contents. And notice, now we have this file called upload.txt right in there. So that was a lot simpler, wasn't it, than typing all that garbage from before. Let's just make sure that it actually uploaded the contents. Great.
Now, scp also works the other way around, so I can secure copy a file from the server down to the client.
So let's say I want to secure copy from the remote machine. This time around, I provide the remote machine as the first argument, so it'll be vm, or I can provide the full ssh options here, but since I've already configured it, I can just type vm, and then the path to the file, so it'll be in the home directory, in the project folder, and this time I'm going to grab the upload.txt file, and I want to download it to this current directory. So I can use the dot forward slash to be specific, and I'll upload it first-- or save it to this local folder in a file called upload.txt. But I don't actually need this dot forward slash, so we'll just do it this way, and there's the file. So that came from the server itself.
So this is a really useful utility for getting files up to the server and down from the server.
One of the useful options you can use with scp is the capital C option. That's going to compress the file before it gets uploaded, and so if you're dealing with really large files, you're going to want to compress them before sending them up to the server. So it will be uncompressed before it gets saved to the remote machine. So dash C is a really neat option.
Now, we can also upload folders. Let's go ahead and create a folder on our local machine. I'm just going to call it project, and we'll move all the hello files into project, and let's just make sure that that worked.
And now if I want to upload the entire project folder, I can use an option dash R. Just like with copy, locally, dash R stands for recursive. So what I want to do is to copy recursively the project folder up to the vm machine, and I'm just going to save it to a folder called newproject.
And then if we ssh into vm and take a look at the home directory now, notice that we have a new folder here called newproject, and if I change into it, our files are inside of it.
So if you're going to be running regular commands like this one, where I'm checking how much disk space I have free on the remote machine, you can just use ssh for that, but when you're working with uploading and downloading files or folders, scp is a much better tool for that job.