Linux scp command - copy file and directory from remote computer

There is a secure copy command in Linux that you can use to copy file or directory from remote computer. This is definitely a good option if you are going to transfer data remotely and need an encryption. The command we are talking about is the Linux scp command. We can run scp command from any Linux distribution's command line terminal.

The Linux scp comes together with ssh server package. So, to use scp command, the remote machine or the Linux server must have ssh server installed and running. If you want to install scp server in Ubuntu, you have to install ssh server package. The install ssh in Ubuntu tutorial describes all about ssh installation if you need help. For just scp installation in Ubuntu, you can see an example below. scp server is already in your system if you are using Slackware because scp included in Slackware default installation.

How to install scp in Ubuntu

As we mention earlier, scp included in the ssh package, so we'll use apt-get to install ssh package:

luzar@ubuntu:~$ sudo apt-get install ssh
[sudo] password for luzar:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following extra packages will be installed:
openssh-server
Suggested packages:
rssh molly-guard openssh-blacklist openssh-blacklist-extra
The following NEW packages will be installed:
openssh-server ssh
0 upgraded, 2 newly installed, 0 to remove and 0 not upgraded.
Need to get 286kB of archives.
After this operation, 827kB of additional disk space will be used.
Do you want to continue [Y/n]? Y
Get:1 http://archive.ubuntu.com intrepid/main openssh-server 1:5.1p1-3ubuntu1 [285kB]
Get:2 http://archive.ubuntu.com intrepid/main ssh 1:5.1p1-3ubuntu1 [1246B]
Fetched 286kB in 9s (29.4kB/s)
Preconfiguring packages ...
Selecting previously deselected package openssh-server.
(Reading database ... 130928 files and directories currently installed.)
Unpacking openssh-server (from .../openssh-server_1%3a5.1p1-3ubuntu1_i386.deb) ...
Selecting previously deselected package ssh.
Unpacking ssh (from .../ssh_1%3a5.1p1-3ubuntu1_all.deb) ...
Processing triggers for ufw ...
Processing triggers for man-db ...
Setting up openssh-server (1:5.1p1-3ubuntu1) ...
Creating SSH2 RSA key; this may take some time ...
Creating SSH2 DSA key; this may take some time ...
* Restarting OpenBSD Secure Shell server sshd [ OK ]

Setting up ssh (1:5.1p1-3ubuntu1) ...

luzar@ubuntu:~$

If somehow your Slackware server doesn't have ssh server, you can install it online as Ubuntu server above using slackpkg command. That's it. Now you have scp in your Ubuntu system. To start using scp to transfer file or directory, let's see the command's usage.

Linux scp command syntax and examples

The Linux scp command is a very useful tool for a remote copy. It is a better option than rcp because it uses encryption just like ssh program. In fact, it uses ssh for data transfer authentication. That means, you certainly need to provide login name and password of the target/destination computer. To start using Linux scp program, let's look at the most important part, the command syntax or command format.

This is the Linux scp command syntax to send file or directory to a remote computer:

scp -r [/path/filename] [login name@ip address] : .

This is the Linux scp command syntax to retrieve file or directory from a remote computer:

scp -r [login name@ip address] : [/path/filename] .

Here are the meaning of scp command options:

  • -r = recursively copy entire directory
  • . = current directory

'/path/filename' is the complete directory path and name where the file resides.

'login name@ip address ' is the target/destination computer. You have to provide your login name and ip address and will be asked for user password.

Dot (.) at the end of the command means the files will be copied to the current directory. Of course you can change the destination to any directory you wish, just type the full path and directory name to replace the dot(.).

You can find more details in the scp manual page by invoking man scp from Linux command line terminal, which is recommended, if you could at least read it once to get a better understand of the command.

Now that you already understand how scp command works, let's see some actual examples taken from Ubuntu client.

Example on how to send file to a remote computer with scp command in Ubuntu Linux using ip address:

luzar@ubuntu:~$ scp UserManual.pdf luzar@192.168.1.6:.
luzar@192.168.1.6's password:
UserManual.pdf 100% 3812KB 3.7MB/s 00:01
luzar@ubuntu:~$

The scp command example above shows I am sending a file named UserManual.pdf from my computer to a remote computer with an ip address 192.168.1.6. The file UserManual.pdf must be in our home directory. If not, please provide a complete directory path. The file will be saved in luzar's home directory. I specified the user name which I want to login with (remember that we need to provide a valid username and password) and enter the password afterward.

Here is the example on how to retrieve file from a remote computer using scp command in Linux. This time we are using remote server's hostname:

luzar@ubuntu:~$ scp luzar@slackware:/home/luzar/netfilter.pdf .
luzar@slackware's password:
netfilter.pdf 100% 367KB 367.2KB/s 00:00
luzar@ubuntu:~$

The scp command example above shows that I was retrieving a file named netfilter.pdf from a remote computer with an ip address 192.168.1.6. Notice that this time I used a full directory path to the file which I want to get.

We can also use scp command to transfer multiple files at once to a remote computer. See the example below:

luzar@ubuntu:~$ scp Interfaces.txt new_file02.txt labu@10.21.36.3:.
Password:
Interfaces.txt 100% 268 0.3KB/s 00:00
new_file02.txt 100% 51 0.1KB/s 00:00
luzar@ubuntu:~$

We can also use wild card with scp command such as * to copy all files. If you feel that copying the whole directory is a better idea, here is an example of scp command used to transfer a directory to a remote computer:

luzar@ubuntu:~$ scp -r folder labu@10.21.36.3:.
Password:
luzar@ubuntu:~$

We use scp -r option in the example above to recursively copying a directory named folder.

You can also use scp command to transfer file or directory to other Linux computer using Windows operating system. Get Putty to login to the Linux server and use scp to copy file or directory to a remote computer (Linux computer) just as examples above.

Finally, if for any reason you need to uninstall scp from you Ubuntu server, you can do so by removing ssh package. You can use apt-get remove ssh command or apt-get purge ssh command. You can also use autoremove option like the example below:

luzar@ubuntu:~$ sudo apt-get autoremove ssh
[sudo] password for luzar:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages will be REMOVED:
ssh
0 upgraded, 0 newly installed, 1 to remove and 0 not upgraded.
After this operation, 45.1kB disk space will be freed.
Do you want to continue [Y/n]? Y
(Reading database ... 130944 files and directories currently installed.)
Removing ssh ...
luzar@ubuntu:~$

That cover almost all about how to use Linux scp command to copy files or directory from remote computer. Now you should understand that scp is about transferring files or directory to or from a remote computer. It is similar to the ftp protocol. In fact, scp is a perfect choice to transfer an important file because it supports data encryption. If you prefer ftp protocol, you can check ftp client guide for ftp commands tutorial, install and configure proftpd in Ubuntu tutorial and install and configure vsftpd in Slackware Linux on how to install ftp server in your Linux.

Back to Linux basic commands main page.

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.