scp Command - Transfer Files to / from Linux Machines
Need to quickly upload, or download a file from your Linux machine? Good news! You already have the software you need. There's an utility called scp that is preinstalled on Windows, MacOS, and Linux.
Prerequisites
All that is required is an SSH daemon running on the Linux machine — the one that you want to copy files from, or to. Which, again, most Linux servers already have that installed. scp uses the same login mechanism as SSH.
So if you can log in through PuTTY, or with a simple ssh [email protected]
command to that Linux machine, you're good to go. You can copy files to / from that machine.
It's worth noting that alternatives like sftp
and rsync
also exist. But if you need to copy just a couple of files, occasionally, scp
will do the job. If you need to copy a large amount of files, on a regular basis, or synchronize directories across different machines, see how to use the rsync command instead (also runs on Windows, but has to be installed).
What Is scp on Linux?
The name "scp" is short for Secure CoPy. Because scp encrypts traffic between the sender and receiver of files. So the security aspect is covered — secure login through SSH, and encryption out-of-the-box.
Now let's see how to use the scp command.
The Syntax of the scp Command
The general syntax of the scp command is:
scp [options] [source] [destination]
Alternatively, you can think of this as:
scp [options] [from] [to]
By passing options like -C
you can change the behavior of scp. -C
would make scp compress the network transfer, to speed it up. We'll look at useful options in a later section.
You can skip specifying options if you don't need any (they're often skipped). But the source and destination are the important parts in an scp command.
source and destination can take complex forms like [email protected]:/home/alex/
. But let's explore these in-depth. As it will be easier to understand by looking at real examples.
How to Copy a File from Your Own Computer to a Remote Linux Machine (scp Local to Remote)
Or, it can also be a virtual machine hosting Linux on your own computer. If it's running the SSH daemon, you can transfer files to it.
scp
commands shown in this blog work on all operating systems: Windows, MacOS, and Linux.To copy a file FROM your own computer TO a remote Linux machine, the syntax of scp looks like this:
scp name_of_local_file username@IP_OF_SERVER:/path/to/remote/directory/
That's if the local file is in your current directory. If it's in a different directory, then you can specify the full path to that local file:
# From Linux, or MacOS computer:
scp /path/to/local/file username@IP_OF_SERVER:/path/to/remote/directory/
# From Windows:
scp C:\path\to\local\file username@IP_OF_SERVER:/path/to/remote/directory/
Here's an example of a real command:
scp lighttpd.img [email protected]:/home/alex/
This does the following:
- Copies a file from your own computer. The file is called
lighttpd.img
and is located in your current directory. - Transfers it over to a server where you log in with a user called
alex
. - The IP address of the remote server is
10.11.12.14
. - The remote directory where you want to copy your file to (on the remote server) is
/home/alex/
.
If the file lighttpd.img
is not in your current directory, then you can also specify the full path to it in a command like:
# Running from a Linux computer:
scp /home/alex/unikernels/lighttpd.img [email protected]:/home/alex/
# Running from a MacOS computer:
scp /Users/alex/unikernels/lighttpd.img [email protected]:/home/alex/
# Running from a Windows computer:
scp C:\Users\Alex\unikernels\lighttpd.img [email protected]:/home/alex/
cmd
and press Enter; Command Prompt will pop up.Now what if you want to do this in the opposite direction? Copy that file from the remote computer to your own computer?
How to Copy a File from a Remote Linux Machine to Your Own Computer (scp Remote to Local)
To copy a file from a remote Linux machine, to your own computer, the syntax of a scp command looks like this:
scp username@IP_OF_SERVER:/path/to/remote/file .
That's a .
dot at the end which symbolizes the current directory. So the remote file from Linux will be copied to your current directory. If you want to drop it in a different local directory, just specify the full path to it:
# On Linux and MacOS computers:
scp username@IP_OF_SERVER:/path/to/remote/file /path/to/local/directory/
# On Windows:
scp username@IP_OF_SERVER:/path/to/remote/file C:\path\to\local\directory\
Here's an example of a real scp command:
scp [email protected]:/home/alex/oldstuff/script.sh .
This would do the following:
- Connect to the remote Linux server with a user called
alex
. - The IP address of the Linux server is
10.11.12.14
. - The remote directory where the file will be copied FROM is
/home/alex/oldstuff/
. - And the file in that directory is called
script.sh
.
So this would retrieve that script.sh
file, and drop it in your current directory, on your local computer.
If you don't want to copy this in your current directory, but rather some other location, then just specify the full path to that location:
# Running from a MacOS computer
scp [email protected]:/home/alex/oldstuff/script.sh /Users/alex/Downloads/
# Running from a Linux computer:
scp [email protected]:/home/alex/oldstuff/script.sh /home/alex/Downloads/
# Running from a Windows computer:
scp [email protected]:/home/alex/oldstuff/script.sh C:\Users\Alex\Downloads\
cmd
and press Enter; Command Prompt will pop up.But what if instead of transferring a single file, you need to transfer multiple files with one scp command?
How to Copy Multiple Files with scp
To scp multiple files from local to remote:
scp file1 file2 file3 [email protected]:/home/alex/
This would copy three files, file1
, file2
, and file3
from your local directory to your remote Linux server.
Or, if you want to scp multiple files with the same extension, from local to remote:
scp *.log [email protected]:/home/alex/
This would send all files that end with .log
from your local directory.
Replace .log
with whatever applies to you.
*
is essentially a wildcard meaning "Anything can go here." So *.log
translates to "This file can have any name, as long as it ends in .log
."
To scp multiple files from remote to local:
scp -O "[email protected]:/home/alex/{file1,file2,file3}" .
This would copy three files from the remote server, from the remote directory /home/alex/
. It would copy file1
, file2
, and file3
from that location into your local directory.
The -O
option is required to tell scp
to use a different protocol (scp instead of sftp protocol). Otherwise, the command wouldn't work. Because sftp would take this line /home/alex/{file1,file2,file3}
and not interpret it to refer to three different files. But scp interprets it correctly.
And speaking of interpretation, note the double quotes "
that were used to wrap this text between them [email protected]:/home/alex/{file1,file2,file3}
. That's required to prevent your own shell from interpreting these special symbols: {
and }
. By wrapping between double quotes you tell the shell (command interpreter) to send these as they are, to the remote server. Otherwise said, you instruct the shell:
Hey, don't try to figure out what I mean by{
and}
. Just send these, exactly in this form, to the remote server; and let it figure out what I mean by that.
To scp multiple files with the same extension, from remote to local:
scp "[email protected]:/home/alex/*.log" .
All files from /home/alex/
that end with .log
will be copied (from the remote Linux machine) to your local computer.
Now what if instead of one file, or multiple files, you want to copy an entire directory between two machines?
How to Transfer an Entire Directory (Recursive scp)
To copy an entire directory all you need to do is add the -r
recursive option to the previous scp
commands.
For example, to copy a directory FROM a remote server TO your own computer (remote to local):
scp -r [email protected]:/home/alex/oldstuff/ .
That's a .
dot at the end representing "current directory".
The entire directory called oldstuff
would be copied from the remote server to your local computer. And you would end up with a directory called oldstuff
in your current local directory.
To copy a directory FROM your local computer TO a remote server (local to remote):
scp -r unikernels [email protected]:/home/alex/
This would copy a directory called unikernels
from your local computer to the /home/alex/
directory on your remote Linux server.
That's if unikernels
is located in your current directory. If it's in some other location, just use the full path to it. Examples for different operating systems:
# Running from a Windows computer:
scp -r C:\Users\Alex\unikernels\ [email protected]:/home/alex/
# Running from a MacOS computer
scp -r /Users/alex/unikernels/ [email protected]:/home/alex/
# Running from a Linux computer:
scp -r /home/alex/unikernels/ [email protected]:/home/alex/
Useful Options for the scp Command
Here are a few important options you can pass to scp:
-C
to compress network traffic. Which could speed up the transfer of large (compressible) files.
-P
(uppercase "P") followed by a port number. If the SSH daemon on the target machine does not use the standard port 22, you can tell scp to use a different port. Let's say you use port 1234 to accept incoming SSH connections on your Linux server. With -P 1234
you can tell scp to use port 1234 to connect to that machine (and copy files from, or to it).
-p
(lowercase "p") tells scp to preserve the modification times, and access times (and file mode bits). Could be useful if you need to preserve the timestamps of your files.
-r
to make scp recursively copy files. Otherwise said, copy an entire directory, and all of its contents (as presented in a previous section).
Always pass these options BEFORE specifying the source and destination. Here are a few examples:
Copy file called lighttpd.img
from local computer to remote server, and use -C
option to compress network traffic:
scp -C lighttpd.img [email protected]:/home/alex/
Copy file from local computer to remote server, and use two options:
-C
to compress network traffic.- And
-P 1234
to use port 1234 for the remote SSH connection (used by scp).
scp -C -P 1234 lighttpd.img [email protected]:/home/alex/
This is useful if the SSH daemon on the remote Linux machine is not using the standard port 22, and uses a different one, like 1234
exemplified here.
We hope this covers everything you wanted to know about the scp
command. And if you want to learn more about how to manage Linux-based operating systems, you can check out this course:

See you in the next blog!