Various Linux SCP Examples To Get You Started With Using Secure Copy
A collection of examples illustrating the use of SCP (Secure Copy).
Overview
SCP (Secure Copy) is a quick, easy way to transfer files securely between two Linux machines. If you don't mind using the command line and all you want is a simple way to upload or download files on Linux, this series of SCP examples should be all you'll need to learn how to do it.
Quick Background
SCP actually runs on SSH. Hence, it allows you to transfer files over an encrypted connection. Later versions of SSH now come with a more sophisticated file transfer protocol called SFTP, which allows you to list files, create/delete directories, and perform other functions similar to those found in typical file system protocols.
If you want to learn how to use command-line SFTP instead, read Using SFTP On The Command Line. But if you prefer something simpler and faster, proceed to the examples below.
I will assume you already have a running SSH/SFTP service that you can connect to. Many Linux distros have it installed by default. If your Linux machine doesn't have it, you can install the openSSH server or read the post Setting Up An SFTP Server.
Ready? Let's get this show on the road!
Basic SCP command syntax
In this post, you'll encounter several examples showing different variations of the scp command but this is the basic syntax:
scp [options] [path of source] [path of destination]
Let's now dive into those examples.
1. Uploading file to remote user account's home; same local and remote username
If the username you're logged-into on your local host happens to be the same username you'll be logging into on your remote host, you really don't need to specify a username. Just be sure that username already exists on the remote host. Also, if the remote directory you want to upload the file to happens to be that username's home directory, then you also don't need to specify the remote directory.
The result is probably the simplest linux scp command you can find. Here's your first Linux SCP example:
scp testdoc.txt 192.168.100.101:
Wherein:
testdoc.txt is the file you want to upload
and
192.168.100.101 is the IP address of the remote server. Of course, you can also use a hostname like filetransfers.mydomain.com instead of the numeric IP address.
Don't forget the colon (:).
Here's a screenshot showing the same command. As you can see, the local username is "johnv", which happens to be the same username I want to upload to on the remote host. As the screenshot shows, the SCP service may request the password for the remote username. Be sure you know what it is.
2. Uploading file to remote user account's home; different local and remote usernames
If the username on the remote host differs from the one you're logged into on your local host, you'll need to specify the username (e.g. johnadmin) like this:
scp testdoc.txt johnadmin@192.168.100.101:
3. Uploading to subdirectory under remote user account's home
What if we wanted to upload that file to a subdirectory below the remote user account's home directory? Here's how you do it:
scp testdoc.txt johnv@192.168.100.101:/remotedir1/remotesubdir1
4. Using wildcards
You can also use wildcards. For instance, here's how we would upload all files with the extension .txt to /remotedir1 subdirectory:
scp *.txt johnv@192.168.100.101:/remotedir1
5. Downloading to the current local directory
Let's now throw some downloads into the mix. Here's an easy one. This is how you'd download a file (e.g. textdoc.txt) from the remote account's home directory to the current local directory. The current local directory is represented by the dot (.) at the end of the statement. In case your browser didn't render it clearly, there's a space between txt and the dot.
scp johnv@192.168.100.101:testdoc.txt .
Notice how the remote host/path is now placed right after scp. That's because it's now become the source and the local host/path has now become destination.
6. Downloading and renaming
This is how you would rename a file immediately after it's downloaded.
scp johnv@192.168.100.101:testdoc.txt testdoclocal.txt
So, as soon as testdoc.txt is finished downloading, it will be renamed to testdoclocal.txt.
7. Downloading multiple files
To download multiple files, just separate each file with a comma (,) and enclose with a backward slash and curly brackets like this:
scp johnv@192.168.100.101:~/\{testdoc.txt,triggerconditions2.txt\} .
In this example, we wanted to download the files testdoc.txt and triggerconditions2.txt from the remote home directory to the current local directory. Note that the curly brackets aren't nested inside the two backward slashes. Rather, they always follow the backward slashes, i.e., \{ ... \}
Note also the tilde (~) right after the colon (:). It represents the home directory. Some SSH server implementations require this.
8. Uploading multiple files
To upload multiple files without using a wildcard, just enter each file name right after the scp command, separating each file with a space. This is how we uploaded the files testdoc.txt and triggerconditions2.txt to the subdirectory remotedir1:
scp testdoc.txt triggerconditions2.txt johnv@192.168.100.101:/remotedir1
9. Uploading an entire directory
You can also copy an entire directory from your local machine to the remote server. To do that, just insert the -r option right after the scp command like this:
scp -r ./testdir johnv@192.168.100.101:/remotedir1
In this example, we uploaded the subdirectory named testdir to the subdirectory named remotedir1, wherein testdir is located under the current local directory and remotedir1 is located under the current remote directory.
10. Downloading an entire directory
By now, you should have an idea how to copy an entire directory from the remote host to your local host. Let's say we'd like to do the reverse of that last example. That is, we'd like to copy the entire remotedir1 directory (and its contents) into the local directory testdir. Compare your answer with the one below:
scp -r johnv@192.168.100.101:/remotedir1 ./testdir
Note that the commands in example 9 and 10 copy directories recursively. Meaning, all subdirectories underneath those copied directories will be copied as well.
Do you want more examples? Send us a Twitter tweet, write a comment on our Facebook, Google+, or LinkedIn pages, or simply comment below.
Download a free GUI-based SFTP client
Download AnyClient
Test drive a free SCP server
Download the free, fully-functional evaluation edition of JSCAPE MFT Server, a managed file transfer server that supports, SCP and several other file transfer protocols, including, FTPS, FTP, SFTP, AFTP, AS2, OFTP, TFTP, WebDAV, HTTP/S, and others.
Download Now