SCP Command Syntax Examples in Linux
Publisher: Psychz Networks, December 09,2018scp stands for secure cp (copy), which means you can copy files across ssh connection. It is a very secure way to copy files between computers as SSH (Secure Shell) is a cryptographic network protocol for operating network services securely over an unsecured network.
scp uses by default the port 22, and connect via an encrypted connection or secure shell connection. You can also copy files directly from one remote server to another remote server. scp is very versatile and can be used on Linux, Mac and Windows (using WinSCP).
- Copy from Source to Destination
- Copy from Remote Location
- Copy from user 1 to user2 in same location
- Copy Multiple Files
- Copy any file with specific extension
- WINSCP
- PSCP
We are going to start with the some basic commands and examples here.
The following syntax is used to copy a file from source to a destination.
$ scp source_file_path destination_file_path
Depending on the type of host, the above syntax will also need a file path with full host address, port number, username and password along with the directory path.
So if you want to send file from your local machine to a remote machine the syntax would look like this
$ scp /local_machine_file.txt user@172.212.xxx.xxx:/some/remote/directory
This will be reverse when copying file from remote host to local host.
$ scp user@172.212.xxx.xxx:/some/remote/directory /local_machine_file.txt
If you simply want to download the file to your local machine the simple form of syntax would be
$ scp user@172.212.xxx.xxx:/some/path/file.txt
NOTE: By default scp will always overwrite files (if already exists) on the destination.
scp user1@152.144.xxx.xxx:/home/file.txt user2@172.168.xxx.xxx:/home/
scp user1@152.144.xxx.xxx:/home/file.txt user2@152.144.xxx.xxx:/home/
Now we will see how to Copy a directory ("demo_dir" in this e.g.) from the local host to a remote host's directory ("location" in this e.g.)
$ scp -r demo_dir user1@152.144.xxx.xxx:/remote/directory/location
We will see now how to copy multiple files using scp command
Note: You can copy multiple files at once without having to copy all the files in a folder, or copy multiple files from different folders putting them in a space separated list.
scp file1.txt file2.txt file3.txt user1@172.165.xxx.xxx:/home/user1/
scp /path/to/file1.txt /path/to/file2.txt /path/to/file3.txt user1@172.165.xxx.xxx:/home/user1/
The following syntax will copy all files of a given extension to the remote server. For instance, you want to copy all your text files (txt extension) to a new folder. Here we will use a wildcard asterisk (*) to simplify the task.
scp /home/user/*.txt user1@172.165.xxx.xxx:/home/user1/
Here's an example.
scp /home/user/html/* jane@host.example.com:/home/jane/backup/
Other available options of SCP
If you are working on a Windows powered computer, you can still enjoy scp in various ways. Winscp is a powerful GUI tool available used by a large number of user base that use windows platform.
pscp is a shell command that works almost on Windows Shell the same way that scp works on Linux or Mac OS X